Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 29, 2012, 12:45 PM   #1
JonHimself
macrumors 68000
 
JonHimself's Avatar
 
Join Date: Nov 2004
Location: Toronto, Ontario
AppleScript To Click One Button Only If Another Button Is Unavailable

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.

Code:
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
__________________
shambo, Changing the definition of "future proof" since April 2009.

Last edited by JonHimself; Jun 29, 2012 at 12:50 PM.
JonHimself is offline   0 Reply With Quote
Old Jun 29, 2012, 02:00 PM   #2
Red Menace
macrumors 6502
 
Join Date: May 2011
Location: Littleton, Colorado, USA
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):

Code:
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
__________________
MacBook Pro / OS X Mavericks (10.9.4) / Xcode 5.1.1 / RubyMotion FTW
Red Menace is offline   0 Reply With Quote
Old Jul 4, 2012, 12:16 PM   #3
JonHimself
Thread Starter
macrumors 68000
 
JonHimself's Avatar
 
Join Date: Nov 2004
Location: Toronto, Ontario
Great, thanks! I'm going to give this a shot (hopefully) tonight.
__________________
shambo, Changing the definition of "future proof" since April 2009.
JonHimself is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Using Javascript | Applescript to click button in Safari o.rka Web Design and Development 4 Feb 18, 2014 04:15 PM
Applescript - Button returned error bgrantmyre Mac Programming 1 Jul 30, 2013 07:04 PM
Applescript Safari how to click button redwgaontoy Mac Programming 1 Jun 11, 2013 11:12 PM
Remapping Keyboard Shortcuts: Add(+) button is unavailable pwdavenport Mac Basics and Help 0 Mar 10, 2013 09:31 AM
iPhone 4 Sleep/wake button, vibrate button, and volume button not responsive McEggrolls iPhone 6 Sep 20, 2012 09:16 AM

Forum Jump

All times are GMT -5. The time now is 08:29 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC