多年来,我一直在使用以下AppleScript在iTerm2中打开vim中的txt文件,但是从iTerm 2.9.2(又名iTerm3)开始,它就坏了。谁能建议如何更新这个AppleScript,让它再次工作?
on run {input, parameters}
if (count of input) > 0 then
tell application "System Events"
set runs to false
try
set p to application process "iTerm"
set runs to true
end try
end tell
tell application "iTerm"
activate
if (count of terminals) = 0 then
set t to (make new terminal)
else
set t to current terminal
end if
tell t
tell (make new session at the end of sessions)
exec command ("vim "" & POSIX path of first item of input as text) & """
end tell
if not runs then
terminate first session
end if
end tell
end tell
end if
end run
我最初从 http://earthwithsun.com/questions/283418/how-can-i-make-terminal-vim-my-default-editor-application-in-mac-os-x 复制了脚本,但我没有任何AppleScript经验。
任何帮助非常感谢!乙
使用@dusty的链接,我建议您将"运行AppleScript操作"的整个代码部分更改为以下内容:
on run {input, parameters}
tell application "iTerm"
activate
if (count of windows) = 0 then
set t to (create window with default profile)
else
set t to current window
end if
tell t
tell current session
write text ("vim "" & POSIX path of first item of input as text) & """
end tell
end tell
end tell
end run
它似乎在我的机器上工作,但我从不使用 vim,所以我不确定这是否会准确地给你你想要的东西。
祝你好运
新的Applescript语法不像以前那样将terminal
作为顶级对象。 它已被更改,以反映整个操作系统中用于其他可脚本应用程序的更多常见模式:应用程序的顶级对象称为窗口,而不是终端。
所以层次结构现在是这样的:
- 包含一个或多个的应用程序
- 包含一个或多个窗口
- 包含
- 会期
- 包含
- 包含一个或多个窗口
https://iterm2.com/applescript.html 已使用新语法的示例进行了更新。
我采用了Craig Smith的代码片段(很好的答案!),如果已经有一个打开的窗口,我稍微调整了一下以打开一个新选项卡。这样,如果您已经在iTerm中打开了vim之类的东西,则不会遇到任何麻烦。此外,此代码将目录更改为文件的目录,以使 NerdTREE 和模糊查找器(如 fzf.vim)满意。
on run {input, parameters}
set filename to POSIX path of input
set cmd to "clear; pushd " & quote & "$(dirname " & filename & ")" & quote & " > /dev/null; nvim " & quote & "$(basename " & filename & ")" & quote & "; popd > /dev/null"
tell application "iTerm"
activate
if (count of windows) = 0 then
set t to (create window with default profile)
else
set t to current window
tell t
create tab with default profile
end tell
end if
tell t
tell current session
write text cmd
end tell
end tell
end tell
end run
我从以前的 anwers 中汲取灵感并做了一些扩展。我的解决方案接受多个输入文件(例如,来自一个选择)。如果存在活动窗口,脚本将在任何选项卡中搜索打开的 vim 会话,并打开该 vim 实例中的文件。如果没有,它将创建一个新的 iTerm 选项卡或窗口。
on run {input, parameters}
set vimCommand to "nvim -p "
tell application "iTerm"
activate
if (count of windows) = 0 then
set w to (create window with default profile)
else
set w to current window
try
# raises error when all windows are minimized
tell w
# look for tab with open vim session
repeat with t in tabs
tell t
if name of current session contains "vim" then
# open files in current vim session
set esc to character id 27
tell current session
write text (esc & esc & ":silent! tablast")
repeat with filename in input
set filePath to quoted form of POSIX path of filename
write text (":execute 'tabedit '.fnameescape(" & filePath & ")")
end repeat
select
end tell
select
return
end if
end tell
end repeat
# no existing session
create tab with default profile
end tell
on error msg
set w to (create window with default profile)
end try
end if
# open in new tab or window
tell w
tell current session
set launchPaths to ""
repeat with filename in input
set filePath to quoted form of POSIX path of filename
set launchPaths to launchPaths & " " & filePath
end repeat
write text (vimCommand & launchPaths)
end tell
end tell
end tell
end run
Automator 中的类似内容:
on run {input, parameters}
set vim_par to POSIX path of input
set cmd to "/usr/local/bin/vim " & quote & vim_par & quote
tell application "iTerm"
tell current window
create tab with default profile command cmd
end tell
end tell
end run
另存为.app,然后通过它打开文件 - 可能,将其.app默认的"打开方式"应用程序。