Check if GUI element in the menu bar is available/visible? [SOLVED]

I’m using Keyboard Maestro to run a script to click the menu bar to Pause/Resume Google Drive’s sync, but sometimes I get an error and I would like to know if I can use AS to check if a certain GUI element in the menu bar is visible/available?

Right now I have a script, but sometimes it throws me an error like this (this is KM’s log):

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:

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

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

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

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?

You can insert repeat loops to wait until a specific UI element exists for example

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
1 Like

So basically it’s repeating the delay, right?

Do you know if it’s possible to check if a certain element has a specific text?
For example the menu option I end up clicking at the end, is either Resume Syncing or Pause Syncing and based on what option is selected, I would like Keyboard Maestro to run a certain action.

When I use UI Browser on each option I get this:

SCR-20240215-kyvb
SCR-20240215-kzak

So I guess I would use some sort of “IF THEN” action?
To make it work with KM, I think the best option is to maybe set a variable to value A if it’s “Pause Syncing” or to value B if it’s “Resume Syncing” and then KM will check the value of that variable and will go to action A or action B.

How would I be able to check that text?

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

Now after this, it’s all Keyboard Maestro :slight_smile:

Thanks @StefanK once again for your precious help! :raised_hands:

Yes, the AppleScript terminology is pretty descriptive in this case

repeat until exists ...
1 Like

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