Apple script to bring System Preferences pane to front - applescript

I am trying to navigate to security pane in system preferences using below script, if pane is already opened and minimised, script is not able to bring it front. Is there a way along with activation , I can bring it to front
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.security"
end tell

This script checks the state of the window.
If the window does not exist open it.
If the window exists but is miniaturized make it visible.
If the window is visible do nothing.
tell application "System Preferences"
activate
if exists window "Security & Privacy" then
tell window "Security & Privacy" to if it is miniaturized then set miniaturized to false
else
set current pane to pane "com.apple.preference.security"
end if
end tell

I would simply quit System Preferences then activate it again:
tell application "System Preferences"
quit
activate
set current pane to pane "com.apple.preference.security"
end tell
Note: Sometimes, quitting and then immediately activating an application can fail as the two processes overlap, generating an error. Should this happen, the following additional few lines (added in the context of the original answer) should mitigate this circumstance:
tell application "System Preferences"
quit
repeat while it is running
delay 0.2
end repeat
activate
set current pane to pane "com.apple.preference.security"
end tell

Related

How to to select specific checkboxes in System Preferences?

I am trying to select the third checkbox of the Keyboard menu item and tab to change the basic fn key function with one run of the script. The rest of the code appears to work fine, but I just recently started trying to code at all so I have no idea.
Here is my current code:
tell application "System Preferences"
activate
set current pane to pane id "com.apple.preference.keyboard"
delay 1
tell application "System Events"
tell (click checkbox 3 of tab group 1)
delay 2
end tell
end tell
end tell
tell application "System Preferences" to quit
And here is the error message:
error "System Events got an error: Can’t get tab group 1." number -1728 from tab group 1
It looks like I'm just not defining it correctly, but I can't find out how to. Any help is appreciated!
Also, this is not needed but would it be possible to run the script without visibly opening the System Preferences application?
The following example AppleScript code was tested under macOS Catalina and clicks the Turn keyboard backlight off after checkbox at: System Preferences > Keyboard > Keyboard
As coded, it does the following:
Checks to see if System Preferences is running and if it is, it closes it so as to not have to see the UI flashing thru the different panes.
If System Preferences is not running it opens to the target anchor/pane without showing the UI.
Clicks the target checkbox.
Closes System Preferences
Example AppleScript code:
if running of application "System Preferences" then
try
tell application "System Preferences" to quit
on error
do shell script "killall 'System Preferences'"
end try
delay 0.1
end if
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
tell application "System Preferences" to ¬
reveal anchor "keyboardTab" of pane id ¬
"com.apple.preference.keyboard"
tell application "System Events"
tell front window of application process "System Preferences"
repeat until (exists checkbox 3 of tab group 1)
delay 0.01
end repeat
click checkbox 3 of tab group 1
delay 0.1
end tell
end tell
tell application "System Preferences" to quit
Note: The example AppleScript code is just that and sans any included error handling, does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

AppleScript: How to identify checkbox location in System Preferences?

I want to issue a keyboard command to toggle the checkbox "Change picture every 30 minutes" in OS X for Desktop 1 (my main monitor desktop). I have multiple monitors so it may be necessary to identify which particular preference pane to manipulate.
I have set up the start of an AppleScript, but I'm at a loss how to formulate how to identify this particular check box:
tell application "System Preferences"
activate
end tell
tell application "System Events"
tell process "System Preferences"
click menu item "Desktop & Screen Saver" of menu "View" of menu bar 1
delay 2
tell window "Desktop & Screen Saver"
click checkbox 1 of …
It's in tab group 1. You can recognize this with the help of the Xcode tool Accessibility Inspector (Xcode > Open Developer Tool > Accessibility Inspector in the menu bar or Dock).
tell application "System Preferences"
activate
set the current pane to pane id "com.apple.preference.desktopscreeneffect"
end tell
delay 2
tell application "System Events" to tell process "System Preferences"
click checkbox "Change picture:" of tab group 1 of window "Desktop & Screen Saver"
end tell

Creating an AppleScript to edit a setting in 'System Preferences'

I am using AppleScript to create a script that will turn on "Remote Login" in the System Preferences without the actual window opening.
Below is my code that is working but still opens the System Preferences window, therefore I can see exactly what it is doing:
tell application "System Preferences" to launch
tell application "System Preferences" to set current pane to pane "com.apple.preferences.sharing"
tell application "System Events" to set visible of process "System Preferences" to false
tell application "System Events" to tell process "System Preferences"
click checkbox 1 of row 5 of table 1 of scroll area 1 of group 1 of window "Sharing"
tell application "System Preferences"
quit
end tell
end tell
I have tried various lines such as:
tell application "System Events" to set visible of process "System Preferences" to false
but it still doesn't give me the result I want.
My question is, is there a line(s) I can write that can hide the window while it does this process? Thanks.
You can avoid to open System Preferences, you can start ssh (Remote Login) from the shell, so the AppleScript command will be:
do shell script "launchctl load -w /System/Library/LaunchDaemons/ssh.plist" user name (short user name of (system info)) password "currentuserpasssord" with administrator privileges
replace the currentuserpassword with the password of the current user, that is required to be an administrator.

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

Use AppleScript to change System Preferences without being visible?

I would like to change settings in System Preferences without the user seeing things happen.
If I have a script that starts like:
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.sound"
end tell
the System Preference window will be shown to the user.
I'd like to know if there is a way to do it in the background somehow, or at least keep the window minimized.
(Example script can be found in this question.)
You can just remove the activate command. System Events can perform actions in hidden windows.
tell application "System Preferences"
reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
end tell
tell application "System Events" to tell process "System Preferences"
click checkbox 1 of tab group 1 of window 1
end tell
quit application "System Preferences"
If you open menus, they will be visible though.
tell application "System Preferences"
reveal anchor "TTS" of pane "com.apple.preference.speech"
end tell
tell application "System Events" to tell process "System Preferences"
tell pop up button 1 of tab group 1 of window 1
delay 0.1
click
if value is "Alex" then
click menu item "Kathy" of menu 1
else
click menu item "Alex" of menu 1
end if
end tell
end tell
quit application "System Preferences"
The brightness of displays can also be changed with brightness.c.
You might be able to do what you want with with the command line app defaults, there are some other command line apps that can manipulate other system stuff also like, pmset
Some things can be set using scripting additions also, for example the systems volumn can be set using the standard additions, you also you may be able to find other scripting additions to add more stuff.

Resources