如何获取应用程序的属性

How to get properties of an application

我想获取应用程序的 properties(名称作为命令行参数给出)。基本上我想知道命令行参数中的 application_name 是否在最前面。

这就是我一直在做的(对我有用)

tell application "Google Chrome"
    get properties
end tell

但是当我尝试这样做时:

on run argv
  tell application (item 1 of argv)
    get properties
  end tell
end run

使用命令 osascript has_focus.scpt "Google Chrome"

执行时出现错误

environment/mac/scripts/has_focus.scpt:56:66: execution error: Google Chrome got an error: Can’t get every property. (-1728)

请帮忙。

这行不通。

tell application 的参数必须是(文字)常量,因为术语是在编译时评估的。

一个例外是任何应用程序检索的属性 versionfrontmostrunning。这些属性在内部组织为应用程序对象,独立于现有的 AppleScript 字典。

我看到您通过讨论解决了您的问题,并了解了 frontmost 属性 对象的 application

所以这更像是一个兴趣点来完成你最初开始的事情。

@vadian 是正确的:

The argument of tell application must be a (literal) constant because the terminology is evaluated at compile time.

解决这个问题的方法是不使用术语。术语要求被指示的应用程序能够查找其脚本字典中使用的术语,并将它们从人性化的术语转换为原始 Apple 事件代码。因此,去掉中间人,您可以通过原始人字形语法发出语句、发出命令和检索属性:

on run argv
    set [appName] to argv
    tell the application named appName ¬
            to return its «class pALL»
end run

然后,在终端中:

osascript ~/Scripts/getAppProperties.applescript "Brave Browser"

«class ChBB»:«class CrBF» id 1, frontmost:false, «class ChOB»:«class CrBF» id 2,
class:application, name:Brave Browser, version:83.1.10.97

自然地,在没有字典查找的情况下,特定于应用程序的属性及其值以原始语法返回。


注意: 将原始语法代码输入 脚本编辑器 可能有点棘手,因为它会立即编译并且任何术语都是可以被 Script Editor 或 AppleScript(例如 properties 理解,它们具有全面的通用性并具有相同的类型代码)。但是您可以在任何纯文本编辑器中创建 AppleScript。如果你真的必须把它编译成一个 .scpt 文件,那么 osacompile 就可以了。