Googleしながらあちこちのコードを参考にさせていただきました。しかし、自分のやりたいことをそのままできるようなコードがなかったので、自作しました。細かい部分で先人のお力をいただいていますので、その辺りのコードはそのままとなっています。特に引数を展開するところと、hasNoWindowsのところです。iTerm周りは、自分のやりたいことを実現するために、ほぼ全てのコードを書きました。
ファイルをダブル・クリックしたときの動作は次のようになっています。iTermが起動していない時は、iTermを起動して、そのウィンドウでnvimを起動して、ダブル・クリックしたファイルを開きます。iTermが起動していてもウィンドウがない時は、ウィンドウを開いて、そのウィンドウでnvimを起動して、ダブル・クリックしたファイルを開きます。iTermのウィンドウがある時には、タブを開いて、新しいタブにnvimを起動して、ダブル・クリックしたファイルを開きます。
作成方法は、次の通りです。
AutomatorでApplicationを新規作成して、Run AppleScriptで以下のコードを貼り付ける。
on run {input, parameters}
set filepaths to ""
if input is not {} then
repeat with currentFile in input
set filepaths to filepaths & quoted form of POSIX path of currentFile & " "
end repeat
end if
# display alert filepaths
if application "iTerm" is not running then
activate application "iTerm"
else
tell application "iTerm"
set hasNoWindows to ((count of windows) is 0)
# display alert hasNoWindows
if hasNoWindows then
create window with default profile
else
tell current window
create tab with default profile
end tell
end if
end tell
end if
tell application "iTerm"
tell current session of current window
write text "nvim -p " & filepaths
end tell
end tell
end run
好きな名前をつけてApplicationsフォルダへ保存します。
ダブル・クリックで開きたいファイルを選択してCmd+iで情報を開いて、開くプログラムのところに先ほど作成したアプリケーションを選択します。