我正在使用以下 automator apple 脚本,以便当我将文件拖到停靠图标中时,它会在 vim 中打开该文件:
on run {input, parameters}
set filename to POSIX path of input
set cmd to "clear && 'vim' '" & filename & "' && exit"
tell application "iTerm"
set newWindow to (create window with default profile)
tell current session of newWindow
write text cmd
end tell
end tell
end run
但是,我还希望允许单击图标本身来打开 vim
而无需任何文件,即运行 $ vim
。我如何更改上面的脚本以便:
- 如果传递了文件名,我会使用该文件打开 vim,
vim filename
- 如果没有传递文件名(双击图标即可),则直接打开vim,用
vim
?
最佳答案
以下示例 AppleScript 代码将按照您的要求进行操作;但是,请记住,输入
是一个列表,并且目前编码它需要一个单个项目列表,这意味着您只将一个文件拖放到应用的Dock Tile上:
on run {input, parameters}
if not input is equal to {} then
set filename to POSIX path of first item of input
set cmd to "clear && 'vim' '" & filename & "' && exit"
else
set cmd to "clear && 'vim' '" & "' && exit"
end if
tell application "iTerm"
set newWindow to (create window with default profile)
tell current session of newWindow
write text cmd
end tell
end tell
end run
注意:示例 AppleScript 代码就是这样,不包含任何错误处理视情况而定。用户有责任添加任何适当、需要或想要的错误处理。看看try 声明和error AppleScript Language Guide 中的声明 。另请参阅Working with Errors 。此外,使用 delay在适当的情况下,事件之间可能需要命令,例如延迟 0.5
,并适当设置延迟的值。
关于vim - Applescript(如果未添加文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61980158/