Applescript,如果未添加文件

问题描述 投票:1回答:1

我有以下正在使用的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打开vim
  • 如果没有传递文件名(仅双击图标),它只会使用vim打开vim?
vim applescript automator
1个回答
1
投票

以下示例 AppleScript 代码将按照您的要求进行;但是,请记住input是一个list,并且按照目前的[[code]],它期望一个single item list,这意味着您仅拖放了一个file进入应用程序的Dock Tileon 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 statementAppleScript Language Guide statement。另请参阅Working with Errors。另外,在适当的情况下,例如,在事件之间,可能需要使用delay commanddelay 0.5,并已适当设置delayvalue
© www.soinside.com 2019 - 2024. All rights reserved.