AppleScript获取窗口标题

6
我尝试获取一个应用程序的标题(并将其复制为变量) 到目前为止,这是我的代码,但是它失败了。
tell application "app"
    activate
end tell

tell application "System Events"
    set frontApp to name of first application process whose frontmost is true
end tell

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

结果:

错误 "应用程序出错:每个窗口都不理解“count”信息。" 编号 -1708 来自每个窗口

1个回答

7
错误信息表明当前应用程序不支持脚本。
要针对不支持脚本的应用程序窗口进行操作,唯一的方法是通过“系统事件”上下文中的“[应用程序]进程”对象属性,而不是通过其“应用程序”对象(只有支持脚本的“应用程序”对象才具有“窗口”元素):
tell application "app"
    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
       set window_name to name of front window
    end if
end tell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接