为什么这个错误 - applescript , automator 和 applescript 编辑器

Why this bug - applescript , automator & applescript editor

当 运行 在自动机内部作为服务而没有输入时,我的 applescript 给出了正确的输出,但当 运行 在自动机外部(Safari、预览等)

时,它给出了错误消息

当 运行 通过 applescript 编辑器时,它也能给出正确的输出,运行使用 safari、预览或任何其他应用程序都没有问题。

我如此热衷于使用 automator 完成它的原因是我不想安装第三方应用程序只是为了将一个快捷方式分配给一个脚本。

我调试了这部分,在从代码中删除后允许自动化程序 运行 脚本。 运行 试了一下,automator 和 viola 出错,完全没有问题。

但我想知道为什么当 运行 在自动化应用程序之外时,此代码较早且没有尝试错误功能会给出错误消息。

这是带有试错函数的脚本部分:

try

set writ to do shell script "/usr/bin/python Users/[username]/Documents/tech_toolkit/windtitle"

tell application writ
    if the (count of windows) is not 0 then
        set window_title to name of front window
    end if
end tell

on error

     delay 2

end try

对于shell脚本/windtitle,windtitle是一个可执行文件

#!/usr/bin/env python

from AppKit import NSWorkspace
active_app_name = NSWorkspace.sharedWorkspace().frontmostApplication().localizedName()
print active_app_name

错误是:

  • 您的 python 脚本 return “Automator Runner” 应用程序 运行 作为服务时,它是 最前面在后台但不在前台。

要获取最前面的应用程序,请使用此脚本:

try
    tell application (path to frontmost application as text)
        set window_title to name of front window
        set writ to its name -- get the name of this application
    end tell
on error
    return -- quit this script, because this application is not scriptable, or no window
end try