2

我们如何编写一个 alfred 工作流来从 finder 打开文件到 vim 编辑器?

这是我第一次尝试在 Alfred 中创建工作流。我仍在学习并想从一个简单的工作流程开始。

在这里,我首先创建了 Hotkey 和 Keyword。

Open the Alfred3
Go to Workflows
Click + sign at the bottom left sidebar
Choose Blak Workflow
Name: Vim Launcher
Description: Opens a file in the terminal inside vim editor

1. Right-click > Trigger > press cmd-alt-v 
2. Right-click > Inputs > Keyword > open in vim  and title also open in vim

3. Right-click > Actions > Run NSAppleScipt

里面复制粘贴以下内容Run NSAppleScipt

on alfred_script(q)
    tell application "Terminal"
        activate
        do script "vim '" & q & "'"
    end tell
end alfred_script

此工作流程有效,但会打开两个终端而不是一个。我们如何解决这个问题,使它只打开一个窗口?

工作流程总结如下。 在此处输入图像描述

4

1 回答 1

1

您需要终端命令操作。

只需将此对象连接到您的触发器并vim "{query}"在其上使用。

编辑:

我正在编辑答案以回复询问后续问题的评论:

工作流程现在为每个文件创建新的终端,如果终端已经在运行,我们可以让它在同一个终端中打开吗?

在这种情况下,Run NSAppleScript可以使用该对象,不同之处在于我们必须指定一个do script应该运行的位置,因为如果没有指定位置,它总是会打开一个新窗口:

on alfred_script(q)
    tell application "Terminal"
        if not (exists window 1) then reopen
        activate
        do script "vim '" & q & "'" in window 1
    end tell
end alfred_script
于 2018-04-13T02:30:17.517 回答