Verify a checkbox before clicking with Applescript - macos

I'm facing an issue with one of my applescript.
I'm trying to create an applescript that check/uncheck the checkbox that call the password after the mac wake up or the screensaver stop in the mac security pannel.
I'm using this with proximity.app, with the idea that when i'm back home and my phone is in range, proximity.app remove the password, but when i'm out of range, it put the password back.
Well... I'm forced to do it using UI scripting, because of the new security policy in Mountain Lion.
So there is the code when out of range :
tell application "System Preferences"
set current pane to pane id "com.apple.preference.security"
tell application "System Events"
tell process "System Preferences"
tell first window
tell first tab group
click radio button 1
if not 1 then click checkbox 1
click menu item 6 of menu of pop up button 1
end tell
end tell
end tell
end tell
quit
end tell
and when in range :
tell application "System Preferences"
set current pane to pane id "com.apple.preference.security"
tell application "System Events"
tell process "System Preferences"
tell first window
tell first tab group
click radio button 1
click checkbox 1
end tell
end tell
end tell
end tell
quit
end tell
What i want to improve, is a way to first verify if the box is check or uncheck before checking or unchecking it.
Thanks for your help.

Just check the value of the checkbox.
0 = Uncheck, 1 = check
tell application "System Preferences" to ¬
reveal anchor "Advanced" of pane id "com.apple.preference.security"
tell application "System Events"
tell first tab group of first window of process "System Preferences"
tell checkbox 1 to if value is 0 then click -- checkbox was not checked.
end tell
end tell
quit application "System Preferences"

Related

How to click on iCloud button in System Preferences using applescript

I am trying to get the script to click the iCloud button, but I am getting the syntax/logic incorrect.
tell application "System Preferences"
activate
set current pane to pane id "com.apple.preferences.internetaccounts"
delay 1
end tell
tell application "System Events"
tell process "System Preferences"
click button "iCloud" of window "Internet Accounts"
end tell
end tell
Help is much appreciated. I am on Monterey (12,4) iMac.
Try this.
tell application "System Preferences"
activate
set current pane to pane id "com.apple.preferences.internetaccounts"
end tell
tell application "System Events" to tell process "System Preferences"
repeat until exists of UI element "iCloud" of UI element 1 of row 1 of ¬
table 1 of scroll area 1 of group 1 of window "Internet Accounts"
delay 0.1
end repeat
click button "iCloud" of UI element 1 of row 1 of table 1 of scroll area 1 of ¬
group 1 of window "Internet Accounts"
end tell

Applescript to turn mirroring on and select iPad

I recently updated my mac to Monterey and my old applescript is not working anymore and I get this error: 'System Events got an error: Can’t get pop up button 1 of window 1 of process "System Preferences". Invalid index.'
I honestly have no idea which part should I change. Thank you in advance.
tell application "System Preferences"
set current pane to pane "com.apple.preference.displays"
activate
end tell
tell application "System Events"
tell process "System Preferences"
click pop up button 1 of window 1
click menu item 1 of menu 1 of pop up button 1 of window 1
end tell
end tell
tell application "System Preferences"
delay 10
quit
end tell
tell application "System Preferences"
reveal anchor "displaysDisplayTab" of pane id "com.apple.preference.displays"
activate
end tell
tell application "System Events"
tell application process "System Preferences"
click pop up button "Add Display" of window "Displays"
click menu item 2 of menu 1 of pop up button "Add Display" of window "Displays"
end tell
end tell
tell application "System Preferences"
delay 5
quit
end tell

Applescript Check for Flash Player Update in System Preferences

I've made it to here:
tell application "System Events"
tell process "System Preferences"
tell window "Flash Player"
click radio button "Advanced" of tab group 1
tell scroll area "Advanced"
click button "Check Now" of window 2
end tell
end tell
end tell
end tell
but I can't get applescript to click the "Check Now" Button.
I'm thinking I need to add another tell application "System Events" to click the second area of the advanced tab, but so far I just get errors
This works perfectly at me:
tell application "System Events"
tell process "System Preferences"
tell window "Flash Player"
tell tab group 1
click radio button "Advanced"
tell scroll area 1
click button "Check Now"
end tell
end tell
end tell
end tell
end tell
In such cases the tool Accessibility Inspector is very helpful.

How to set checkbox in Mission Control?

I'm trying to set checkbox in Mission Control pane in System Preferences. So far I'm able to open MC but I have no idea how to click on checkbox. I tried everything I found on Internet but with no luck...
How can I do it?
tell application "System Preferences" activate set current pane to
pane "com.apple.preference.expose" end tell
This will click the first checkbox of the pane only if it is currently not checked:
tell application "System Preferences"
set current pane to pane "com.apple.preference.expose"
activate
end tell
tell application "System Events"
tell process "System Preferences"
set firstCheckbox to checkbox 1 of group 2 of window 1
set checked to value of firstCheckbox as boolean
if (not checked) then
click firstCheckbox
end if
end tell
end tell

Dropdown in Monitor System Preferences

tell application "System Events"
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.displays"
set theWindows to windows
set win2 to item 2 of theWindows
tell win2
set index to 1
set visible to false
set visible to true
end tell
set value of combo box 1 to "90°"
end tell
end tell
I'm trying to change the Rotation of the External Screen via Applescript, but I do
not find out how to access this dropdown menu. Google seems to give me a lot about combo box and pop menu, but under Lion at least all this stuff doesn't work.
This worked for me on 10.8.
tell application "System Preferences"
reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell
tell application "System Events" to tell process "System Preferences"
tell pop up button 1 of tab group 1 of window 1
click
click menu item 3 of menu 1
end tell
end tell

Resources