
如題
我就是想在mac上點擊兩下就用vim開啟檔案。
無奈在mac裏vim不是一個Application,所以要使用automator來幫忙了
程式碼#
先上Apple Script的內容
1
2
3
4
5
6
7
8
9
10
11
12
| on run {input, parameters}
set myPath to POSIX path of input
set cmd to ":e " & myPath
tell application "iTerm"
set newWindow to (create window with profile "vim")
tell newWindow
tell current session
write text cmd
end tell
end tell
end tell
end run
|
前置步驟:(沒有做的話程式碼要另外打開vim)
- 在iTerm新增一個profile,不用Login Shell改用Command
- 直接呼叫Vim出來(跳過啟動Shell)
然後就是寫Automator
步驟如下
- 在Automator新增Application
- 在Action>Library>Utilities中,將"Run AppleScript"拉去右邊
- 貼上上面的程式碼
- 儲存成Application
之後右鍵打開任何檔案的時候,選擇這個應用程式就好了。
或是直接右鍵檔案,Info中有預設打開的程式,全部替換掉也可。

本程式碼在iTerm2下,macOS 10.15.6下測試過。
1
| set myPath to POSIX path of input
|
召喚輸入的路徑,存成mypath
1
| set cmd to ":e " & myPath
|
設定傳入vim的指令,:e path
可以打開路徑檔案。
1
2
3
4
5
6
7
8
| tell application "iTerm"
set newWindow to (create window with profile "vim")
tell newWindow
tell current session
write text cmd
end tell
end tell
end tell
|
呼叫iTerm,並且新增Vim profile。
如果沒有Vim profile的話記得要多加一行打開它。
1
2
3
| tell current session
write text cmd
end tell
|
呼叫當前的Session,也就是Vim,傳入上面已經設好的cmd指令
這樣就大功告成了。
Reference#
- https://stackoverflow.com/questions/39430002/applescript-expected-end-of-line-but-found-identifier-for-terminal-app
- https://thepugautomatic.com/2015/02/open-in-iterm-vim-from-finder/
- https://www.iterm2.com/documentation-scripting.html