【发布时间】:2015-11-15 17:39:40
【问题描述】:
我编写了一个小服务脚本来在 Finder 的任意文件夹上打开一个 iTerm 终端窗口。
我希望它检查 iTerm 是否正在运行,以及是否要在新选项卡而不是现有选项卡中打开终端会话。
脚本如下:
on run {input, parameters}
set cdPath to "cd " & (quoted form of POSIX path of (input as string))
if application "iTerm" is running then
display notification "running"
tell application "iTerm"
set termWin to (current terminal)
tell termWin
launch session "Default"
tell the last session
write text cdPath
end tell
end tell
end tell
else
display notification "not running"
tell application "iTerm"
activate
set termWin2 to (current terminal)
tell termWin2
tell the last session
write text cdPath
end tell
end tell
end tell
end if
return input
end run
问题是,当我将脚本作为服务运行时,它总是表现得好像 iTerm 已经在运行(显示“正在运行”通知),即使 iTerm 已关闭并且很明显没有运行。
但是,如果我将相同的脚本粘贴到脚本编辑器(将 cdPath 设置为文字,如 set cdPath to "cd /etc")并直接执行它,它将正常工作,打开一个新的 iTerm 实例或重用现有的实例并创建一个新的选项卡,并显示相应的通知。
如果我将脚本简化为只显示通知(像这样删除tell aplication 块:
on run {input, parameters}
set cdPath to "cd " & (quoted form of POSIX path of (input as string))
if application "iTerm" is running then
display notification "running and path is " & cd
else
display notification "not running and path is " & cdPath
end if
return input
end run
它将按预期运行(相应地显示“正在运行”或“未运行”)。
但是,如果我添加“告诉应用程序”部分,无论如何,它将始终通过“运行”分支。
例如:
on run {input, parameters}
set cdPath to "cd " & (quoted form of POSIX path of (input as string))
if application "iTerm" is running then
display notification "running"
else
display notification "not running"
tell application "iTerm"
activate
end tell
end if
return input
end run
将始终打开 iTerm(即使 tell application "iTerm" 它位于“未运行”分支上,但会显示来自“正在运行”分支的“正在运行”通知......仅存在“告诉应用程序”将触发打开应用程序然后运行服务。
有没有办法避免这种情况?为什么“告诉应用程序”块会打开应用程序,即使它在条件的另一个分支上?
【问题讨论】:
-
什么|输入|正在传递给脚本?是文件路径的字符串吗?
-
当作为服务运行时,它会收到一个路径。但我认为它与输入无关,而是与“告诉应用程序”块有关。当我删除它们时,它可以工作(尽管它没用),并且我让它在我的第二个代码块中工作(正确处理输入)。
标签: macos applescript automator osx-services