如何使用applescript获取当前Aquamacs窗口的标题

时间:2018-02-08 02:18:46

标签: applescript window-management

我正在尝试以编程方式检索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之间存在这样的不一致:它显然有一些肯定有标题的窗口,但不知何故是脚本无法访问的。

1 个答案:

答案 0 :(得分: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

它将返回一个窗口计数。

相关问题