我有以下正在使用的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 filename
打开vimvim
打开vim?以下示例 AppleScript 代码将按照您的要求进行;但是,请记住input
是一个list,并且按照目前的[[code]],它期望一个single item list,这意味着您仅拖放了一个file进入应用程序的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 statement和AppleScript Language Guide statement。另请参阅Working with Errors。另外,在适当的情况下,例如,在事件之间,可能需要使用delay command。注意:示例
delay 0.5
,并已适当设置delay的value。