vim - Applescript(如果未添加文件)

标签 vim applescript automator

我正在使用以下 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/

相关文章:

javascript - 在 VIM 中使用的 Javascript 手册页

vim - 在vim中,如何将一列文本粘贴到不规则长度行的末尾?

swift - 在 Mac 上运行脚本提示 Error?

macos - 如何使用 `emacsclient` 命令双击 OS X 中的 EMACS 文件来打开它?

macos - 在OSx Mojave的Finder中未选择任何内容时,如何在右键单击上下文菜单中添加选项?

c++ - 在 Mac OS 中使用 vim 单键编译和运行 C++ 代码

vim - 删除 vim 编辑器中行长度超过 80 个字符的警告

pdf-generation - 使用 Automator 或 Applescript 或两者递归地将文档打印为 PDF

macos - 在文件夹操作上运行applescript

python - OSX Automator 无法使用 shell 中的模块运行 Python 脚本