[SOLVED] Check if GUI element is available/visible?

I don't know if the term "GUI element" is the correct one, but I have a macro that clicks the Google Drive menu bar icon, then clicks another button when the menu is visible, then another one. Is it possible to pause the macro until those elements are available, without relying on the Found Image action?

Right now I have a script, but sometimes it throws me an error like this:

Execute an AppleScript failed with script error: text-script:114:207: execution error: System Events got an error: Can’t get group 3 of UI element 1 of window 1 of process "Google Drive". Invalid index. (-1719)

So I'm assuming it's because it's not being fast enough. I could increase the delay, but I was wondering if I could pause the macro and wait for each element to be visible or available?

Here's the script

tell application "System Events"
	tell process "Google Drive"
		click menu bar item 1 of menu bar 1
		delay 0.2
		click pop up button 1 of group 1 of group 1 of group 1 of group 3 of UI element 1 of window 1
		delay 0.2
		click menu item 4 of menu 1 of group 1 of group 2 of group 1 of group 3 of group 1 of UI element 1 of window 1
		delay 0.2
		click menu bar item 1 of menu bar 1
	end tell
	
end tell

For example this is the icon:

SCR-20240215-ijpx

Then when the window opens up, this is the icon I need to click:

SCR-20240215-ijrm

Then after that, I click the Resume syncing / Pause syncing

SCR-20240215-ijtz


I found this topic, tested the script using Safari and it indeed showed me a list of elements:

image

Script:

tell application "System Events"
	tell process "Safari"
		set visible to true
		return every UI element of front window
		return name of every UI element of front window
	end tell
end tell

So I would guess that the menu bar can also be scanned and check if element XYZ is visible or not?

With the help of StefanK at MacScripter forum, I got this and it seems to be working:

tell application "System Events"
	tell process "Google Drive"
		click menu bar item 1 of menu bar 1
		repeat until exists pop up button 1 of group 1 of group 1 of group 1 of group 3 of UI element 1 of window 1
			delay 0.2
		end repeat
		click pop up button 1 of group 1 of group 1 of group 1 of group 3 of UI element 1 of window 1
		repeat until exists menu 1 of group 1 of group 2 of group 1 of group 3 of group 1 of UI element 1 of window 1
			delay 0.2
		end repeat
		click menu item 4 of menu 1 of group 1 of group 2 of group 1 of group 3 of group 1 of UI element 1 of window 1
		click menu bar item 1 of menu bar 1
	end tell
end tell

Now the next goal is to check if the last option is either Pause Syncing or Resume Syncing and based on that, save the result to a variable and then KM will check the variable's value and if it's A, it runs action A, if it's B it runs action B.

Here are the images from UI Browser:

I would guess that I need an "IF THEN" condition, but I can't seem to find anything online related to that part of the path (or I just can't find it, because I don't know what that part represents? Is it called "description"? Something else?)


EDIT:
After doing some research I got to this and in Script Debugger I am able to get the name, so I'm getting closer :wink:

return name of menu item 4 of menu 1 of group 1 of group 2 of group 1 of group 3 of group 1 of UI element 1 of window 1


EDIT 2
More research got me here:

if name of menu item 4 of menu 1 of group 1 of group 2 of group 1 of group 3 of group 1 of UI element 1 of window 1 is "Pause Syncing" then
			display dialog "Yes"
		else
			display dialog "No"
		end if

So I am able to see the dialog boxes depending on the text. One step closer. Now the next step is to define the variables, which I believe will be easy


Final edit:

if name of menu item 4 of menu 1 of group 1 of group 2 of group 1 of group 3 of group 1 of UI element 1 of window 1 is "Pause Syncing" then
			--display dialog "Yes"
			set syncingState to "Resumed"
		else
			--display dialog "No"
			set syncingState to "Paused"
		end if

So now I just need to send the variable's value back to the normal KM flow :slight_smile: and that it the easy part

Final script

tell application "System Events"
	tell process "Google Drive"
		click menu bar item 1 of menu bar 1
		repeat until exists pop up button 1 of group 1 of group 1 of group 1 of group 3 of UI element 1 of window 1
			delay 0.2
		end repeat
		click pop up button 1 of group 1 of group 1 of group 1 of group 3 of UI element 1 of window 1
		repeat until exists menu 1 of group 1 of group 2 of group 1 of group 3 of group 1 of UI element 1 of window 1
			delay 0.2
		end repeat
		if name of menu item 4 of menu 1 of group 1 of group 2 of group 1 of group 3 of group 1 of UI element 1 of window 1 is "Pause Syncing" then
			set syncingState to "Paused"
		else
			set syncingState to "Resumed"
		end if
		click menu item 4 of menu 1 of group 1 of group 2 of group 1 of group 3 of group 1 of UI element 1 of window 1
		click menu bar item 1 of menu bar 1
	end tell
end tell

set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine" to setvariable "Local__syncingState" instance kmInst to syncingState
2 Likes

I love your posts man. I appreciate that even after you raise a question you continue to research and test, and more often than not end up finding the solution yourself. But on top of that, you document the entire process here which is great for posterity’s sake. I tip my hat to you sir!

2 Likes

Thanks Chris! Appreciate your kind words! ::raised_hands:

When I have time to do some extra research, I definitely do it, so I can learn it while I research (sometimes I end up not only finding what I need, but also other things that are useful, so it's a double win haha). And I know that other people have their own things to do, so I initially start the topic, in case I can't find the solution or if someone can share the solution faster than my research (or if I really don't have time to research, because I'm in the middle of something important that needs my focus, or simply when I really can't find a way to even write the question to Google it haha).

And yes, I like to keep sharing the process and hopefully the final solution. As you said, for posterity's sake. Who knows if someone else will need this information one day, right? :wink:

And I gotta be honest, even though it feels good when someone can help me, it feels even better when I am able to find the solution myself, and pat myself in the back and say "good job, Tiago!" hahaha

Again, thanks for taking the time to share your kind words. :slight_smile:

2 Likes

Hello Tiago (@alltiagocom) :wave:

Many thanks for your work and documentation on this beautiful journey of a Script..

I recently installed Google Drive on all my Macs and this is a bummer of a Time saver … not only for the aspect of accessibility.. also for the development of Automations - this saved me many hours on building macros.

Thank you for sharing …

Greetings from Germany

Tobias

2 Likes

Happy to know that this is helpful to other people! :slight_smile:
Always a pleasure to share my journey and then the end result with others.