macos 使用 AppleScript 如何在没有名称/标题的窗口中单击对话框中的按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7355763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using AppleScript how do I click a button in a dialog within a window that has no name/title?
提问by Tyson Rosage
I'm trying to write an AppleScript that will empty the cache of Twitter for Mac and then restart the app. The problem I'm running into is that the confirmation dialog doesn't have a name (the title bar is empty) so I'm lost as to how I target the button with no context. Here's what I have so far:
我正在尝试编写一个 AppleScript,它将清空 Mac 版 Twitter 的缓存,然后重新启动应用程序。我遇到的问题是确认对话框没有名称(标题栏为空),所以我不知道如何定位没有上下文的按钮。这是我到目前为止所拥有的:
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
采纳答案by NSGod
There are numerous ways to refer to objects within the object hierarchy: name is only one way (see AppleScript Language Guide: Reference Forms. Your script already uses one of the other ways: Index.
在对象层次结构中引用对象的方法有很多种:名称只是一种方法(请参阅AppleScript 语言指南:参考表单。您的脚本已经使用了其他方法之一:索引。
tell menu bar 1
That's referring to the menu bar by its index: (unlike in many programming languages, items in a list in AppleScript are indexed starting at 1 rather than 0). The script below should accomplish what you want:
这是通过索引来引用菜单栏的:(与许多编程语言不同,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
You can probably comment out the delay 1
lines; I added those to slow down the procedure so you can easier see what's happening.
您可能可以注释掉这些delay 1
行;我添加了这些来减慢过程,这样你就可以更容易地看到发生了什么。
When trying to figure out how to "get at" an object in an application via AppleScript, I often find it helpful to use AppleScript Editor to create little query scripts to try to discover more info about the app. For example, in Twitter, I chose Twitter > Empty Cache to bring up the Empty Cache window. I then ran the following script:
当试图弄清楚如何通过 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
This script returned the following result:
此脚本返回以下结果:
{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}