Apple script to bring System Preferences pane to front

700 Views Asked by At

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
2

There are 2 best solutions below

1
vadian On BEST ANSWER

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
    
3
CJK On

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