UI Scripting - testing if a button exists

Hi there,

I’ve got a slight niggle with a script that I’m trying to solve.

Basically I’m trying to test whether or not a dialog box contains one of two possible buttons that I want to click.
So far I’ve used a key stroke which has worked ok, but I’ve now found that there could be another dialog box that pops up and that key stroke doesn’t exist.
So I need to check which button exists on the dialog box. I’ve created a test to see if the dialog box exists which is ok, it’s the testing of the button I need to sort.

So far I’ve tried something like this:-

if ((first window whose description is "dialog") exists) then
									
if button "Replace" exists then
  do this
else
  do that
end if
									
end if

and also, something like this:-

if ((first window whose description is "dialog") exists) then
									
if button whose description is "Replace" exists then
  do this
else
  do that
end if
									
end if

However, both of these don’t seem to work. I’ve put in some ‘display dialogs’ to test what’s happening and see where the script is getting to.

I’ve searched the archives however as yet haven’t found anything so any help would be appreciated.

Thanks in advance,

Nick

Post #4 of this thread - http://bbs.applescript.net/viewtopic.php?id=17460 - might help.

j

You don’t mention what the intended alternative action is, Nick ” and that might be significant in terms of where one ends up in a nest of tell statements. The following examples, targeting a Save As… dialog in TextEdit, may provide one or two syntactic clues:

tell application "System Events" to tell application process "TextEdit"
	tell (first window whose description is "dialog")
		tell (first button whose name is "Replace")
			if exists then
				click
			else
			(* the target of the else statement is the still the button - even if it doesn't exist *)
			end if
		end tell
	end tell
end tell
tell application "System Events" to tell application process "TextEdit"
	tell (first window whose description is "dialog")
		set target_button to a reference to (first button whose name is "Replace")
		if target_button exists then
			click target_button
		else
			(* the target of the else statement is now the dialog window *)
		end if
	end tell
end tell

tell application "System Events" to tell application process "TextEdit"
	set target_button to a reference to (first button whose name is "Replace") of (first window whose description is "dialog")
	if target_button exists then
		click target_button
	else
		(* the target of the else statement here is application process "TextEdit" *)
	end if
end tell

And finally, of course, there’s the one-line version:

tell application "System Events" to tell (first button whose name is "Replace") of (first window of application process "TextEdit" whose description is "dialog") to if exists then click

Of course :smiley:

Thanks to you all for responding, it’s given me plenty to play with. :slight_smile: