I’m writing a script that will constantly scan iTunes for new dialog boxes and close them correctly depending on what they do. For the most part my script is working correctly with the exception that it doesn’t search the buttons of the dialog box.
So far the script will wait until another process opens iTunes, and then wait for the first dialog box to appear (using spin-wait loops). Once a dialog box appears it gets the window and then the window’s buttons. I would like it to then make decisions based on what the buttons are, but I’m having a bad time with finding out what the buttons are. Below is the entire script:
#repeat
set windowOpen to false
tell application "System Events"
repeat until windowOpen
if window 1 of process "iTunes" exists then
set windowOpen to true
end if
end repeat
set windowOpen to false
tell process "iTunes"
set frontmost to true
set wantedWindow to null
repeat until windowOpen
try
set wantedWindow to first UI element whose role description is "dialog"
set windowOpen to true
on error erMessg
set windowOpen to false
end try
end repeat
set buttonList to every button in wantedWindow
if (count of buttonList) is 1 then
if title of item 1 of buttonList is not "Stop" then
click item 1 of buttonList
end if
else
if my windowContainsButton(buttonList, "Cancel") then
say "Cancel"
end if
# repeat with theButton in buttonList
# if title of theButton is "Cancel" or title of theButton is "Restore" then
# say "cancel"
# delay 1
# end if
# end repeat
end if
end tell
set wantedWindow to null
end tell
#end repeat
on windowContainsButton(listOfButtons, searchFor)
repeat with theButton in listOfButtons
if title of theButton is searchFor then
return true
end if
end repeat
return false
end windowContainsButton
So far, I’m trying to find out if it has found a cancel button by having it say “Cancel”. Instead, it’s coming up with an error: System Events got an error: Can't make |title| of button "Cancel" of window 1 of application process "iTunes" into type reference. It then points me to my function windowContainsButton on this line:
if title of theButton is searchFor then
and it highlights searchFor.
The windowContainsButton function is exactly the commented out section of code, just generalized. The commented out section works, which is a large part of the reason I’m asking about types.
First off, how can I go about implementing a function like this? Lets say that I actually wanted this function to work, how could I make it?
Secondly, is there a better way to do this? I don’t mean the entire script (although I wouldn’t doubt that it could be done better), I mean searching the buttons for a particular button that I’m expecting.
edit: another thing I noticed is that “title” is a reserved word in the body of the script, but a variable in the function. I’m used to other languages where reserved words are reserved universally, so I would also like some guidance with what’s going on there.
I have not gone through the whole script but this should fix your handler:
This is a bit cleaner: