AppleScript 获取 window 的标题

AppleScript get title of a window

我尝试获取应用程序的标题(并将其复制为 var) 到目前为止,这是我的代码,但这是失败的

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

结果:

error "app got an error: every window doesn’t understand the “count” message." number -1708 from every window

错误消息表明最前面的应用程序不可编写脚本

针对不可编写脚本的应用程序的 windows 的唯一方法是通过 System Events 上下文中的 [application] process 对象的属性 - not 通过它的 application 对象(只有 scriptable application 对象有 windows 元素:

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