Answers:
创建一个Automator应用程序以运行以下小程序:
on run {input}
set the_path to POSIX path of input
set cmd to "vim " & quoted form of the_path
tell application "System Events" to set terminalIsRunning to exists application process "Terminal"
tell application "Terminal"
activate
if terminalIsRunning is true then
do script with command cmd
else
do script with command cmd in window 1
end if
end tell
end run
保存自动化应用程序。(例如,将其命名为Vim Launcher)
右键单击(或按住Control单击)您的自定义vim类型的文件(例如,使用.vim作为扩展名),然后在Open With…下选择底部选项Other…,然后找到您的Automator Application(例如Vim Launcher),双击点击它。
繁荣。
set the_path to POSIX path of input
set cmd to "vim " & quoted form of the_path & "; exit"
tell application "System Events" to set terminalIsRunning to exists application process "Terminal"
tell application "Terminal"
if terminalIsRunning is true then
do script with command cmd
else
do script with command cmd in window 1
end if
activate
end tell
end run
我改用这个AppleScript。它会在执行之后(而不是之前!)激活Terminal.app,以防止使用Spaces时事情变得奇怪。Vim退出后,它也会关闭窗口。只需将Terminal.app设置为在干净退出后关闭即可。
我只是想在接受的答案中添加评论,并对其进行更改以使其在优胜美地中工作所需的代码更改,但是由于我没有足够的声誉,因此无法添加评论,因此尝试通过答案进行回复。
Mavericks中的“从Finder在终端中打开文件”脚本在Mavericks中可以正常工作,但是在升级到优胜美地之后,它停止了工作。在优胜美地,接受的答案中的代码仅在第一时间有效-这意味着当我双击Finder中的第一个文件时,它可以很好地打开,但是当我单击后续文件时,它们将只打开空白的新终端窗口(vim将无法打开),并显示命令提示符。
在浏览了多个站点之后,将一个可以正常工作的版本拼凑在一起。我确信有更好的方法可以做到这一点,但是我对Applescript没有任何经验,因此将它留给他人提出任何改进建议。
on run {input}
set the_path to POSIX path of input
-- set cmd to "vim " & quoted form of the_path
-- we can do a change directory to make NerdTree happy
set cmd to "clear;cd `dirname " & the_path & "`;vim " & quoted form of the_path & "; exit"
tell application "System Events" to set terminalIsRunning to exists application process "Terminal"
tell application "Terminal"
if terminalIsRunning is true then
-- CHANGED code starts --
set newWnd to do script with command cmd
do script with command cmd in newWnd
-- CHANGED code ends --
else
do script with command cmd in window 1
end if
activate
end tell
end run
New Document
,选择Application
模板。在Actions->Library
标签中,点击Utilities
,然后点击Run AppleScript
。