PDA

View Full Version : AppleScript To Click One Button Only If Another Button Is Unavailable




JonHimself
Jun 29, 2012, 12:45 PM
I'm fairly new to AppleScript so I'll try and explain this as best as I can. I've tried searching (web and MR forums) but haven't been able to come up with much... I'm not entirely sure I know how to ask the question so that makes it harder to find what I'm looking for.

I'm writing an AppleScript to use for running video files through a tagging program (Subler). The issue that I have run into is that when I get to the point where the program searches for results, not all video files return a result. For the files that do not return a result, most of the code will be useless (as it walks through subsequent screens that only matter if search results were found) and when it gets to the end - the last few codes triggering the program to save, close the window and quit the program - will not work because the Search dialogue/pop-up window is still displayed. My work-around is to add a line that triggers the Esc button but I would prefer to do it properly/learn about if/else and UI Elements.

I started using UIElementInspector to learn more about writing AppleScript to control specific UI elements as opposed to using a keystroke (like 36 to just hit enter when I know that the button I want is always the one highlighted).

Once I get to the point where the Search dialogue is opened I have a delay of 65 seconds (usually only takes 20-30 to search but I left extra time). There are two buttons that can then be pressed: "Add" and "Cancel".

I want to enter an "if" piece so that if the "Add" button is unavailable (using UIElementInspector I have assumed the relevant value is AXEnabled which is 0 if "Add" is unavailable and 1 if "Add" is available) the Script will click the "Cancel" button and then close the window and quit the program (that last part I can do). Then the "else" part would be if the "Add" button was available, in which case I would then just use the rest of the code that I have that already works.

I have attached the code below with some comments. Please note that I do plan on removing all of the code to simply hit 'return' with code that will instruct the button to click but I have only recently learned that. I'm hung up on this if/else piece and don't even know if it is possible. Please let me know if any other information might help.


tell application "System Events"
tell process "Subler"
delay 5
keystroke "m" using {shift down, command down} <-- opens search box
delay 65
tell sheet 1 of window 1 <-- this is where I would like the if/else piece
tell button "Add"
click
end tell
end tell
delay 5
key code 124
delay 5
key code 36
delay 65
key code 53 <-- this is my temporary solution... hitting escape to 'cancel'
delay 5
keystroke "s" using {command down}
delay 15
keystroke "w" using {command down}
delay 5
keystroke "q" using {command down}
end tell
end tell



Red Menace
Jun 29, 2012, 02:00 PM
You can just get the names of all of the buttons of the sheet and look to see if the desired button is in the list. In the following example, I use the full button paths so that the add part of the script can be contained in the if statement (and not in the sheet tell statement):

tell application "System Events" to tell process "Subler"
delay 5
keystroke "m" using {shift down, command down} -- opens search box
delay 65
tell sheet 1 of window 1 to set theButtons to name of buttons -- get all of the button names
if "Add" is not in theButtons then
click button "Cancel" of sheet 1 of window 1 of application process "Subler"
else
click button "Add" of sheet 1 of window 1 of application process "Subler"
-- the rest of the add script
end if
end tell

JonHimself
Jul 4, 2012, 12:16 PM
Great, thanks! I'm going to give this a shot (hopefully) tonight.