0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ローカルファイルの画像をコンテキストメニューからMarkdownリンクに変換する

Last updated at Posted at 2020-04-15

TyporaというMarkdownエディターに出会ってからドキュメントはmarkdownで書くようになりました。
Typoraではローカルの画像をエディタ上に貼り付けてそのまま表示することができます。

■こんな感じに↓
image.png

ローカルにある画像を![名前](画像リンク)で指定すればTyporaでは表示されます。
手順としては画像をTyporaにD&Dすることでいい感じに上のような表示になります。

ただ、Windowsのコンテキストメニューから画像を![名前](画像リンク)の形式にしてくれないかと考えました。
今回はコンテキストメニューへのリンク追加と、上のフォーマットに変換するプログラムを用意して実現します。

コンテキストメニューへの追加

そもそもコンテキストメニューってどんなやつかというのは下のやつです。
画像を右クリックしたときに出てくるメニューですね。ここはユーザーによって項目が異なります。
image.png

ここに項目を追加します。

レジストリエディタに設定追加

コンピュータ > HKEY_CLASSES_ROOT > * > shellを開く
image.png

キーの追加:ここは自由に名前をつける(今回はMarkdownリンク(&Q))
image.png

この時点でコンテキストメニューに上の内容が表示されるようになります。
image.png

キーの追加:command
image.png

項目の編集:"プログラム名(後述)のexeのパス" "%1"
image.png

とりあえずこの時点で一度レジストリの設定は終わりです。
次に変換用のプログラムの方を用意します。

Markdownリンク用の変換ツールの用意

ここは好きな言語で用意すれば良いと思いますが、サンプルとしてC#で今回作りました。
ポイントは、引数としてコンテキストメニューで押されたファイルのパスをmainメソッドのargs[]で受け取っているので
このファイルパスに対してゴニョゴニョして最終的にmarkdown形式の文字列としてクリップボードにコピーするイメージです。
C#ベースのコンソールアプリケーションですがウィンドウを表面に出したくないので、出力の種類は「Windows アプリケーション」にしています。
image.png

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Collections;
using System.Drawing.Imaging;

namespace filepathMarkdownConvert
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            string markdownLink = System.String.Empty;
            try
            {
                string srcPath = args[0].ToString();
                string fileName = Path.GetFileName(srcPath);

                string format = GetImageFormat(Path.GetExtension(srcPath));
                markdownLink = fileName + "](" + srcPath + ")";
                // 画像形式とそれ以外でフォーマットを変える
                markdownLink = (format != null) ? "![" + markdownLink : "[" + markdownLink;
                Clipboard.SetDataObject(markdownLink, true);
            }
            catch
            {
                // not
            }
        }

        /// <summary>
        /// イメージのファイルフォーマットを拡張子から判断し取得する [C#]:humming bird http://yas-hummingbird.blogspot.com/2009/02/c_06.html
        /// </summary>
        /// <param name="ext"></param>
        /// <returns></returns>
        public static string GetImageFormat(string ext)
        {
            foreach (ImageCodecInfo ici in ImageCodecInfo.GetImageDecoders())
            {
                foreach (string s in GetFileNameExtensions(ici))
                {
                    if (s.ToUpper() == ext.ToUpper())
                    {
                        return ici.FormatDescription;
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// イメージのファイルフォーマットを拡張子から判断し取得する [C#]:humming bird http://yas-hummingbird.blogspot.com/2009/02/c_06.html
        /// </summary>
        /// <param name="ici"></param>
        /// <returns></returns>
        public static IEnumerable GetFileNameExtensions(ImageCodecInfo ici)
        {
            foreach (string s in ici.FilenameExtension.Split(';'))
            {
                yield return s.Substring(s.IndexOf('.'));
            }
        }
    }
}

作成してビルドしたアプリケーションを任意の場所に配置してそのファイルパスをコピーしておきます
image.png

レジストリエディタの変換ツールのフルパスの部分にプログラムのパスを代入します
image.png

これで準備OKです

結果

34b58d1b6246270be536902682588eef.gif
わかりにくいですが、コンテキストメニューから追加したMarkdownリンクを選択すると
上で作成した変換ツールがバックグラウンドで起動してクリップボードに内容をコピーしています。

とりあえずこれでTyporaに画像をD&Dしなくても良くなりました。個人的には満足です。
※もしかしたらもっといい方法があるかもしれないです

コンテキストメニューに任意の項目を追加してアプリ実行ができるというのは収穫でした。

参考

右クリックメニューにアイテムを追加する - Windows 10
イメージのファイルフォーマットを拡張子から判断し取得する [C#]:humming bird

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?