LoginSignup
1
1

More than 3 years have passed since last update.

VIMのデスクトップアプリを作る

Posted at

はじめに

 ダブルクリックもしくはドラッグアンドドロップで起動するVimデスクトップアプリを作る。applescriptを用いた方法なので、Mac限定の方法です。

[1]https://qiita.com/evistream/items/fbda12bbc1742dcb3bdb, Mac上でVimをGUIアプリのように起動する - Qiita
[2]https://qiita.com/akiko-pusu/items/8a34c50daffbf59a1165, Macのファインダーから右クリックでファイルをvimで開く - Qiita

環境

macos: 10.15.7
applescript: 2.7 (version 2.11)

準備

applescriptについて

 Macを操縦することに特化したプログラミング言語で、普段Mac上で行っている作業は大体applescriptで自動化できる。

アプレットについて

applescriptで記述したスクリプトの保存形式の一つに、アプリケーションとして保存するものがあり、そうして作ったアプリケーションをアプレットと呼ぶことがある。拡張子はアプリケーションを意味する.appとなる。この形式だと、アイコンをダブルクリックでスクリプト実行できる。

on run
 -- vimを開く処理
end run

[3]https://superuser.com/questions/195633/applescript-to-open-a-new-terminal-window-in-current-space, osx snow leopard - Applescript to open a NEW terminal window in current space - Super User
[4]https://maku77.github.io/mac/applescript/terminal.html, AppleScript でターミナルを操作する | まくまくMacノート
[5]https://superuser.com/questions/139352/mac-os-x-how-to-open-vim-in-terminal-when-double-click-on-a-file, macos - Mac OS X: how to open vim in terminal when double click on a file - Super User

ドロップレットについて

 ドラッグアンドドロップしたファイルに対して処理を施すことができるようにしたものをドロップレットと呼ぶ。

on open
 -- ドラッグアンドドロップした時に行う処理
end open

 ドラッグアンドドロップしたファイルは"リスト"として渡される。たとえば変数theListにファイルのリストを代入し、リストの1番目にだけ処理するときは以下のようにする。

on open theList
 -- item 1 of theListとすればリストの1番目のファイルを取り出せる。
end open

[6]http://tonbi.jp/AppleScript/Introduction/09/, 鳶嶋工房 / AppleScript / 入門 / ハンドラで受け止める

[7]ドラッグ&ドロップでファイルを渡すバッチをMacで作るには? | アカネtech,https://tech.akanequest.jp/command_dragdrop/

ダブルクリックで開くVim アプレットを作る

on run
    tell application "Terminal"
        activate
        tell window 1
            do script "vim"
        end tell
    end tell
end run

 簡単にするため上記のコードは、すでにターミナルのウィンドウが開かれている場合は考慮していない。

ドロップレットを作る。

・tgtはtargetの意味。
・cmdはcommandの意味。

on open theList 
    set tgt to (POSIX path of item 1 of theList)
    set cmd to "vim " & tgt

    tell application "Terminal"
        activate
        do script with command cmd
    end tell
end open

ふたつを合わせる。

on run
    tell application "Terminal"
        activate
        tell window 1
            do script "vim"
        end tell
    end tell
end run

on open theList 
    set tgt to (POSIX path of item 1 of theList)
    set cmd to "vim " & tgt

    tell application "Terminal"
        activate
        do script with command cmd
    end tell
end open

 これを、ファイルフォーマットを"アプリケーション"にして保存して完成。

*ダブルクリックで開く様子
ダブルクリック_モザイク_トリム_サイズダウン.gif

*ドラッグアンドドロップで開く様子
D&G_モザイク_トリム_サイズダウン.gif

アイコンを変更する

 アイコンをVimに変更する。
 画像は縦横比に注意する。
 wikipediaに掲載されているものが良さそうなので今回はそれを用いた。

デスクトップアプリ 2021-03-05 12.13.28.png

[8]https://support.apple.com/ja-jp/guide/mac-help/mchlp2313/mac, Macでファイルやフォルダのアイコンを変更する - Apple サポート
[9]https://www.kaoriya.net/blog/2013/12/06/, 正しいVimのロゴ — KaoriYa
[10]https://ja.wikipedia.org/wiki/Vim, Vim - Wikipedia

考察

applescriptを使えばドラッグ&ドロップで起動する簡単なアプリケーションを手軽に作ることができる。

1
1
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
1
1