1

我正在尝试以编程方式检索 aquamacs 的主要模式。我有一些想法:获取菜单栏项目,获取窗口标题并使用正则表达式解析它。

我都试过了,但遇到了一个问题,菜单栏和窗口数组都是空的,这使得不可能做到这一点:

on test()
try
    tell application "System Events"
        if application "Aquamacs" exists then
            -- display notification "It's aquamacs"
            tell application "Aquamacs" to activate
            if menu bar item "File" of menu bar 1 exists then
                display notification "File exists"
                --Fails when the file menu bar item clearly is there
            else
                display notification "File doesn't exist"
            end if
        else
            display notification "It isn't aquamacs"
        end if
    end tell

end try
end test

test()

或这个:

on getAppTitle(appN)

tell application appN
    activate
end tell

tell application "System Events"
    # Get the frontmost app's *process* object.
    set frontAppProcess to first application process whose frontmost is true
end tell

# Tell the *process* to count its windows and return its front window's name.
tell frontAppProcess
    if (count of windows) > 0 then --never runs because count is always zero
    set window_name to name of every window

    end if
end tell
end getAppTitle

getAppTitle("Aquamacs")

然后查看文件扩展名。

我不明白为什么系统和 AppleScript 之间存在这种不一致:它显然有窗口,这些窗口肯定有标题,但不知何故超出了脚本的范围。

4

1 回答 1

0

问题出在您的代码中!

在第一个代码块中,您if menu bar item "File" of menu bar 1 exists then在一个tell application "System Events"块内部没有任何指定的应用程序或应用程序进程来查询该信息以及它失败的原因。

在第一个代码块中,修复它的方法不止一种,一种是更改:

if menu bar item "File" of menu bar 1 exists then

至:

if menu bar item "File" of menu bar 1 of application process "Aquamacs" exists then

在第二个代码块中,tell frontAppProcess相当于:

tell application process "Aquamacs"

这就是失败的原因,它必须是:

tell application "Aquamacs"

在运行 Aquamacs 的脚本编辑器中,运行以下代码:

tell application "Aquamacs" to count windows

它将返回一个窗口计数。

于 2018-02-08T03:38:09.863 回答