Applescript 将系统首选项窗格置于前面的Apple脚本

Applescript 将系统首选项窗格置于前面的Apple脚本,applescript,macos-mojave,system-preferences,Applescript,Macos Mojave,System Preferences,我试图使用下面的脚本导航到系统首选项中的“安全”窗格,若该窗格已打开并最小化,则脚本无法将其显示在前面。有没有激活的方法,我可以把它带到最前面 tell application "System Preferences" activate set current pane to pane "com.apple.preference.security" end tell 此脚本检查窗口的状态 如果窗口不存在,请打开它 如果窗口存在但已缩小,请使其可见 如果窗口可见,则不执行任

我试图使用下面的脚本导航到系统首选项中的“安全”窗格,若该窗格已打开并最小化,则脚本无法将其显示在前面。有没有激活的方法,我可以把它带到最前面

tell application "System Preferences"
     activate
     set current pane to pane "com.apple.preference.security"
end tell

此脚本检查窗口的状态

  • 如果窗口不存在,请打开它
  • 如果窗口存在但已缩小,请使其可见
  • 如果窗口可见,则不执行任何操作

    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
    
我只需退出系统首选项,然后再次激活它:

tell application "System Preferences"
    quit
    activate
    set current pane to pane "com.apple.preference.security"
end tell

注意:有时,退出然后立即激活应用程序可能会失败,因为这两个过程重叠,从而产生错误。如果发生这种情况,以下额外的几行(在原始答案的上下文中添加)应缓解这种情况:

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

这正是我要找的,像宝石一样工作。。KudosI特别针对您在问题中所述的情况回答了这个问题:您说您试图导航到一个窗格,您给出的示例是安全首选项。因此,如果您正在窗格之间导航,则不必担心丢失应用程序状态。但我可以理解,我的答案没有瓦迪安的灵活,但是,它仍然是一个可行的选择,在您所设定的条件下更容易执行。希望在类似情况下,它可能对其他用户有用。如果使用此方法,我建议在
quit
命令之后添加
delay 1
命令,为了避免出现
系统首选项错误:连接无效。
在退出时尝试
激活
系统首选项时可能发生的错误。@user3439894好主意,尽管现在我的解决方案不那么简单。