Repeat until UI element found

Hey guys,

I have a work app that has a list employees when you search for a name. If you search for a manager’s name it will show you the manager, the employees that report to that manager, and the manager’s direct reports. So if I search for Bob Smith, I would see Bob Smith, Bob Smith (direct reports), and all the numerous employee names that report to bob smith individually.

I’m trying to make a script that will find the Bob Smith (direct reports) and choose it for me. I’m doing this for multiple managers. So far, I have it working by searching for the name. Then I hard coded how many times to press the down arrow before pressing return. But the manager lists can change weekly, so the number of down arrow presses changes frequently. So now I’m trying to find a more reliable way to always choose the direct reports choice I need.

The commonality I’ve found is that there is a UI image (always called image 1) next to the direct report row I need. It is the only thing in the table that has an image like that. So if I can find the row with that image in it, then I can set the number of down arrow presses equal to that row’s number and 100% choose direct reports.

What I can’t seem to figure out is the repeat logic. I want it to try to select the image in row 1; if it can’t and errors then try row 1+1, then row 2+1, then 3+1, etc down the line until it can select the image. When it eventually does select it, tell me the final row number that was used.

In it’s current form it’s getting stuck in an infinite loop. Any help would be greatly appreciated.

tell application "System Events"
	tell process "Work Manager"
		set frontmost to true
		set rowNumber to 1
		set x to true
		repeat while x is true
			try
				select image 1 of UI element 1 of row rowNumber of table 1 of scroll area 1 of window 1 --select the image next to the direct reports
				set x to false
			on error
				rowNumber + 1
			end try
		end repeat
	end tell
end tell




Of course I can’t test it but it seems that the code below may do the job.

tell application "System Events"
	tell process "Work Manager"
		set frontmost to true
		set x to true
		repeat with rowNumber from 1 to 1000
			if exists image 1 of UI element 1 of row rowNumber of table 1 of scroll area 1 of window 1 then
				set x to false
				exit repeat
			end if
		end repeat
		if x is false then error "No match !"
	end tell
end tell

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) lundi 22 mai 2017 19:57:06

But then won’t it always say “No Match!” since finding a match would set X to false then triggers the error you specified?

Ah, I tweaked what you gave. This seems to be working! Thanks for the nudge in the right direction!! I should be able to figure out the rest from here.

Thanks again!

tell application "System Events"
	tell process "Work Manager"
		set frontmost to true
		repeat with rowNumber from 1 to 1000
			if exists image 1 of UI element 1 of row rowNumber of table 1 of scroll area 1 of window 1 then
				return rowNumber
				exit repeat
			end if
		end repeat
	end tell
end tell