使用AppleScript如何在没有名称/标题的窗口中单击对话框中的按钮?

Tys*_*age 9 twitter macos applescript

我正在尝试编写一个AppleScript,它将清空Twitter for Mac的缓存,然后重新启动应用程序.我遇到的问题是确认对话框没有名称(标题栏为空)所以我迷失了如何在没有上下文的情况下定位按钮.这是我到目前为止所拥有的:

tell application "Twitter"
   activate
end tell
tell application "System Events"
   tell process "Twitter"
      tell menu bar 1
         tell menu bar item "Twitter"
            tell menu "Twitter"
               click menu item "Empty Cache"
            end tell
         end tell
      end tell
   end tell
end tell
tell application "System Events"
   click button "Empty Cache" of window "Empty Cache?"
end tell
tell application "Twitter"
   quit
end tell
tell application "Twitter"
   activate
end tell
Run Code Online (Sandbox Code Playgroud)

NSG*_*God 7

有许多方法可以引用对象层次结构中的对象:名称只有一种方式(请参阅AppleScript语言指南:参考表单.您的脚本已经使用了其他方法之一:索引.

tell menu bar 1
Run Code Online (Sandbox Code Playgroud)

这是指索引的菜单栏:(与许多编程语言不同,AppleScript列表中的项目从1开始索引而不是0).下面的脚本应该完成你想要的:

tell application "Twitter" to activate

tell application "System Events"
    tell application process "Twitter"
        click menu item "Empty Cache" of menu "Twitter" of menu bar item "Twitter" of menu bar 1
        delay 1
        --click button "Empty Cache" of front window
        click button "Empty Cache" of window 1
    end tell
end tell

tell application "Twitter"
    quit
    delay 1
    activate
end tell
Run Code Online (Sandbox Code Playgroud)

你可以注释掉这些delay 1线条; 我添加了这些以减慢程序,以便您可以更轻松地查看正在发生的事情.

当试图找出如何通过AppleScript"获取"应用程序中的对象时,我经常发现使用AppleScript编辑器创建小的查询脚本以尝试发现有关该应用程序的更多信息会很有帮助.例如,在Twitter中,我选择了Twitter> Empty Cache来调出Empty Cache窗口.然后我运行了以下脚本:

tell application "Twitter"
    set theWindows to every window -- get a list of windows
    (* turns out there's only one window listed,
      so get the first item in the list *)
    set theWindow to first item of theWindows
    -- get the properties of the window
    properties of theWindow
end tell
Run Code Online (Sandbox Code Playgroud)

此脚本返回以下结果:

{closeable:true, zoomed:false, class:window, index:1,
  visible:true, name:missing value, miniaturizable:true,
 id:28551, miniaturized:false, resizable:true,
 bounds:{-618, 76, -128, 756}, zoomable:true}
Run Code Online (Sandbox Code Playgroud)