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:
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
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.
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:
You can probably comment out the
delay 1lines; I added those to slow down the procedure so you can easier see what’s happening.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:
This script returned the following result: