Some advice on a couple of basic applescripts

Fluffy
Contributor III

Hello everyone,

I am working on some straightforward scripts to run on users' MacBooks for things like showing a folder in Finder or turning the WiFi off and on to troubleshoot. Some of the things I want to do are through applescript/osascript to click buttons on applications. There are two scripts that are very frustrating, as it does not make any sense why they don't work. I'm an applescript noob, so any advice would be greatly appreciated.

 

First script: Clicking on Screen Mirroring in the Control Center.

The intent is to open Control Center, then click on Screen Mirroring to show available options as part of the user troubleshooting process. Script editor acts like the script I have is supposed to work, but nothing happens after Control Center opens.

The applescript:

 

Spoiler
tell application "System Events"
	tell application process "Control Center"
		tell menu bar item "Control Center" of menu bar 1
			click
		end tell
		
		delay 1
		tell window "Control Center"
			tell checkbox "Screen Mirroring"
				click
			end tell
		end tell
	end tell
end tell

What Automator's Watch Me Do says:

Spoiler
-- Click the menu bar item.
delay 1.994947
set timeoutSeconds to 2.000000
set uiScript to "click menu bar item \"Control Center\" of menu bar 1 of application process \"Control Center\""
my doWithTimeout( uiScript, timeoutSeconds )

-- Click the “Screen Mirroring” checkbox.
delay 1.300753
set timeoutSeconds to 2.000000
set uiScript to "click checkbox \"Screen Mirroring\" of window \"Control Center\" of application process \"Control Center\""
my doWithTimeout( uiScript, timeoutSeconds )

on doWithTimeout(uiScript, timeoutSeconds)
	set endDate to (current date) + timeoutSeconds
	repeat
		try
			run script "tell application \"System Events\"
" & uiScript & "
end tell"
			exit repeat
		on error errorMessage
			if ((current date) > endDate) then
				error "Can not " & uiScript
			end if
		end try
	end repeat
end doWithTimeout

What Accessibility Inspector Shows for Hierarchy (Screen Mirroring is actually checkbox 5):

Spoiler
Screen Shot 2022-03-11 at 1.23.38 PM.png

 

Second script: Clicking on My Resources in Jamf Teacher.

The intent is to open the Jamf Teacher app and go to My Resources to show scoped apps (as we are on Jamf School). The problem here being I am only getting index errors when running the script. Using the Press action from the Accessibility inspector acts as it should. Automator's Watch Me Do is also inconsistent with what shows in the hierarchy.

The applescript:

Spoiler
tell application "Jamf Teacher"
	activate
end tell

delay 3

tell application "System Events"
	tell application process "Jamf Teacher"
		tell window "Teacher"
			click static text 1 of group 4 of table 1 of group 1 of group 1 of group 1
		end tell
	end tell
end tell

What Automator's Watch Me Do says (notice, no table in hierarchy):

Spoiler
-- Click the text element.
delay 2.501810
set timeoutSeconds to 2.000000
set uiScript to "click static text 1 of group 4 of group 1 of group 1 of group 1 of group 1 of window \"Teacher\" of application process \"Jamf Teacher\""
my doWithTimeout( uiScript, timeoutSeconds )

on doWithTimeout(uiScript, timeoutSeconds)
	set endDate to (current date) + timeoutSeconds
	repeat
		try
			run script "tell application \"System Events\"
" & uiScript & "
end tell"
			exit repeat
		on error errorMessage
			if ((current date) > endDate) then
				error "Can not " & uiScript
			end if
		end try
	end repeat
end doWithTimeout

What Accessibility Inspector says (notice, table is shown before last group):

Spoiler
Screen Shot 2022-03-11 at 1.47.40 PM.png

 

Big thanks to anyone that takes the time to look at this nonsense.

 

2 REPLIES 2

dolfhoegaerts
New Contributor II

If I am correct Apple doesn’t allow remote clicking in the accessibility screen. Known frustration as sysadmin for remote support tools :-)

Do you mean if I were to push these scripts out to user devices or for anything that isn't a menu bar item? The first part of the the first script I shared works fine. It clicks on the menu item and opens Control Center.