【问题标题】:Applescript if no file is addedApplescript 如果没有添加文件
【发布时间】:2020-05-24 23:19:29
【问题描述】:

我正在使用以下自动机苹果脚本,因此当我将文件拖到停靠图标中时,它会在 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 ?

【问题讨论】:

    标签: vim applescript automator


    【解决方案1】:

    以下示例 AppleScript 代码 将按照您的要求进行;但是,请记住,input 是一个 list 并且目前 coded 它需要一个 单个项目列表,这意味着您只有将一个文件拖放到应用的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 代码就是这样,不包含任何错误处理可能是适当的。用户有责任根据需要或需要添加任何错误处理。查看AppleScript Language Guide 中的try statementerror statement。另请参阅Working with Errors。此外,在适当的情况下,可能需要在事件之间使用delay 命令,例如delay 0.5延迟设置得当。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多