Posted on 03-11-2022 11:56 AM
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:
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:
-- 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):
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:
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):
-- 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):
Big thanks to anyone that takes the time to look at this nonsense.
Posted on 03-12-2022 12:10 AM
If I am correct Apple doesn’t allow remote clicking in the accessibility screen. Known frustration as sysadmin for remote support tools :-)
Posted on 03-14-2022 05:52 AM
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.