0

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
MacDeveloper
  • 1,334
  • 3
  • 16
  • 49

2 Answers2

1

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
    
vadian
  • 274,689
  • 30
  • 353
  • 361
0

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
CJK
  • 5,732
  • 1
  • 8
  • 26
  • I answered it specifically for the case you stated in your question: you said you were trying to navigate to a pane, the example of which you gave was the _Security Preferences_. Therefore, if you're in the process of navigating between panes, there is no concern about losing application state. But I can appreciate that my answer is less flexible than vadian's, however, it remains a workable option that's simpler to execute under the conditions that you laid out. Hopefully, it may prove useful to some other user in a similar situation. – CJK Oct 31 '18 at 05:25
  • If going this route I'd suggest adding a `delay 1` _command_ after the `quit` _command_, so as to avoid a `System Preferences got an error: Connection is invalid.` error that can occur while trying to `activate` System Preferences while it's quitting. – user3439894 Oct 31 '18 at 12:07
  • @user3439894 Good idea, although now my solution is rather less simple. – CJK Oct 31 '18 at 12:20