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.
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:
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.
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:
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}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With