【问题标题】:Why this bug - applescript , automator & applescript editor为什么这个 bug - applescript , automator & applescript editor
【发布时间】:2016-05-13 08:08:36
【问题描述】:

我的 applescript 在 automator 作为服务运行时给出了正确的输出而没有输入,但在 automator 外部运行时给出了错误消息(safari、预览等)

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

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

我调试了这部分,从代码中删除后允许自动机运行脚本。尝试运行它,自动机和中提琴出错,完全没有问题。

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

下面是带有 try-error 功能的脚本部分:

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

【问题讨论】:

  • 脚本应该做什么?预期的结果是什么?错误信息是什么?基本上,只有当目标应用程序有一个 AppleScript 字典并且该字典包含一个具有默认不需要的 name 属性的窗口元素时,该脚本才会起作用。
  • 它是关于返回基于用户的输入以及元详细信息 [如果 safari - 然后是 url 其他任何其他应用程序 - 我的笔记本电脑中的文件位置、日期和时间、窗口标题]。我得到了结果,只是对我上面解释过的 automator 的奇怪行为感到好奇,直到我使用了 try-error 函数。我收到的错误消息是:“运行 AppleScript”操作遇到错误。
  • 要获取脚本的错误,请在脚本中使用它 --> on error err -- 下一行 --> display dialog err
  • 这是我发现的: 1. 系统事件出错:无法获取应用程序自动运行程序 & 2. 变量 window_title 未定义。如果我使用tell process writ 而不是tell application writ 也会有所不同

标签: applescript automator


【解决方案1】:

错误是:

  • 您的 python 脚本在作为服务运行时返回“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

【讨论】:

    猜你喜欢
    • 2021-08-22
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    相关资源
    最近更新 更多