Tuesday, September 29, 2020

#1 2020-08-04 12:41:12 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 620

System Preference Dialog

I wrote this AppleScript to allow easy access to frequently-opened System Preference panes and tabs. It is intended to be more of a template than a ready-to-use script, because it will take some work by users to tailor the script to their particular needs. However, just to get an idea of what the script does, simply open and then run it in Script Editor.

Applescript:


set preferenceList to {"Clock", "Dock", "Ethernet", "Zoom"}

choose from list preferenceList default items (item 1 of preferenceList) with title "System Preferences"
if result = false then
   error number -128
else
   set selectedPreference to item 1 of result
end if

if selectedPreference = "Clock" then -- opens pane with tab selection
   revealAnchor("com.apple.preference.datetime", "ClockPref")
else if selectedPreference = "Dock" then -- opens pane only
   revealPane("com.apple.preference.dock")
else if selectedPreference = "Ethernet" then -- opens pane and advanced menu
   revealAnchor("com.apple.preference.network", "Advanced Ethernet")
else if selectedPreference = "Zoom" then -- opens pane with sidebar selection
   revealAnchor("com.apple.preference.universalaccess", "Seeing_Zoom")
end if

delay 0.5

on revealPane(paneID) -- opens a pane only
   tell application "System Preferences"
       reveal pane id paneID
       activate
   end tell
end revealPane

on revealAnchor(paneID, anchorName) -- opens a pane and anchor
   tell application "System Preferences"
       reveal anchor anchorName of pane id paneID
       activate
   end tell
end revealAnchor

The operation of the script is quite simple, although it should be noted that the script will open either 1) a specified System Preference pane or 2) a specified System Preference pane and tab. The script contains separate handlers for each.

The most time-consuming task in customizing this script is obtaining the applicable pane ID and tab names, and I have written the following script to help with this.

Applescript:


tell application "System Preferences"
   activate
   try
       set paneID to id of current pane
       set anchorNames to name of every anchor of pane id paneID
       set AppleScript's text item delimiters to ", "
       set anchorNames to anchorNames as text
       set AppleScript's text item delimiters to ""
       display alert "The pane ID and anchor names can be copied to the clipboard by selecting the applicable button." message "Pane ID: " & linefeed & paneID & linefeed & linefeed & "Anchor Names: " & linefeed & anchorNames buttons {"Cancel", "Anchor Names", "Pane ID"} default button 3 as informational
       set buttonReturned to button returned of result
   on error
       display dialog "Select a System Preference item and rerun this script." buttons {"OK"} cancel button 1 default button 1 with icon caution
   end try
end tell

if buttonReturned = "Pane ID" then
   set the clipboard to paneID
else if buttonReturned = "Anchor Names" then
   set the clipboard to anchorNames
else
   error number -128
end if

I wrote theses scripts after reading the following thread:

https://macscripter.net/viewtopic.php?id=47823

Last edited by peavine (2020-08-08 09:47:42 am)


2018 Mac mini - macOS Catalina

Offline

 

#2 2020-08-04 05:00:53 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 620

Re: System Preference Dialog

This script is identical to that posted above except that it remembers the last-selected item in the dialog. It does this by creating a one-line text file in the user's preferences folder.

Applescript:


set preferenceList to {"Clock", "Dock", "Ethernet", "Zoom"}

set settingFile to (path to preferences as text) & name of me & ".txt"
set defaultAnswer to readSetting(settingFile)

choose from list preferenceList default items defaultAnswer with title "System Preferences"
if result = false then
   error number -128
else
   set selectedPreference to item 1 of result
end if

if selectedPreference = "Clock" then -- opens pane with tab selection
   revealAnchor("com.apple.preference.datetime", "ClockPref")
else if selectedPreference = "Dock" then -- opens pane only
   revealPane("com.apple.preference.dock")
else if selectedPreference = "Ethernet" then -- opens pane and advanced menu
   revealAnchor("com.apple.preference.network", "Advanced Ethernet")
else if selectedPreference = "Zoom" then -- opens pane with sidebar selection
   revealAnchor("com.apple.preference.universalaccess", "Seeing_Zoom")
end if

writeSetting(settingFile, selectedPreference)

delay 0.5

on revealPane(paneID) -- opens a System Preference pane
   tell application "System Preferences"
       reveal pane id paneID
       activate
   end tell
end revealPane

on revealAnchor(paneID, anchorName) -- opens a System Preference pane and anchor
   tell application "System Preferences"
       reveal anchor anchorName of pane id paneID
       activate
   end tell
end revealAnchor

on readSetting(theFile)
   try
       return paragraph 1 of (read file theFile)
   on error
       return ""
   end try
end readSetting

on writeSetting(theFile, theSetting)
   try
       set openedFile to open for access (file theFile) with write permission
       set eof of openedFile to 0
       write theSetting to openedFile
       close access openedFile
   on error
       try
           close access openedFile
       end try
   end try
end writeSetting

The preference folder can be viewed by running the following in Script Editor:

Applescript:

tell application "Finder" to reveal (path to preferences)

Last edited by peavine (2020-08-07 09:12:20 am)


2018 Mac mini - macOS Catalina

Offline

 

#3 2020-08-05 09:18:33 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 620

Re: System Preference Dialog

A few comments on the script.

First, it has one issue that I have not been able to resolve. If a preference pane is open and the script is run, the newly-opened preference pane does not always have the focus.

Second, the script has been tested on macOS Catalina only, and pane and tab names in the script may not work with other versions of macOS. In this regard, note should be made of the two formats of pane ID's as noted in my post 5 below.

Third, I use the terms anchor and tab interchangeably in the above posts and this is not entirely correct. For example, the Accessibility pane does not have any tabs in the commonly-accepted sense of the term but does contain the following anchors:

Accessibility_Shortcut, Seeing_Cursor, Seeing_Zoom, Keyboard, Seeing_VoiceOver, Virtual_Keyboard, TextToSpeech, Dwell, Dictation, Switch, Siri, Alternate_Pointer_Actions, Mouse, Captioning, Media_Descriptions, Head_Pointer, General, Seeing_ColorFilters, Seeing_Display, Hearing


Finally, some System Preferences require a password to make any changes and my script can be modified to prompt for a password, as in the following:

Applescript:


   else if selectedPreference = "Privacy" then
       revealAnchor("com.apple.preference.security", "Privacy")
       authorizePane("com.apple.preference.security")

-- other code

on authorizePane(paneID)
   tell application "System Preferences"
       authorize pane paneID
   end tell
end authorizePane

Last edited by peavine (2020-08-13 07:07:23 am)


2018 Mac mini - macOS Catalina

Offline

 

#4 2020-08-11 07:18:02 pm

ZaphodB
Member
Registered: 2020-08-07
Posts: 1

Re: System Preference Dialog

This is awesome. I'm using Mojave and works perfectly fine. I went into each individual pane to get the right name. Just go into the info.plist for each pane in the preference panes contents folder (ex. com.apple.preferences.softwareupdate). They all seem to work just fine in Mojave, no issues with any not working. This is very useful, at least for me. Thanks for the share.

Offline

 

#5 2020-08-12 10:11:22 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 620

Re: System Preference Dialog

Thanks ZaphodB. I'm always happy to hear that one of my scripts is useful.

After reading your post, it occurred to me that some preference pane ID's are in the format "com.apple.preference.[name]" while others are in the format "com.apple.preferences.[name]" This is the reason some preferences--such as Software Update--would not work with my script. It's great to have that fixed.


2018 Mac mini - macOS Catalina

Offline

 

#6 2020-08-19 07:53:45 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 620

Re: System Preference Dialog

This is a revised version of the script in post 2; it resolves the focus issue noted in post 3 and includes a few other improvements.

Applescript:

-- Revised 2020.09.29

main()

on main()
   set preferenceList to {"Dock", "Energy Saver", "General", "Keyboard Shortcuts", "Mouse", "Network", "Privacy", "Software Update"}
   
   set settingFile to (path to preferences as text) & name of me & ".txt"
   set defaultAnswer to readSetting(settingFile)
   
   choose from list preferenceList default items defaultAnswer with title "System Preferences"
   if result = false then
       error number -128
   else
       set selectedPreference to item 1 of result
   end if
   
   if selectedPreference = "Dock" then
       revealPane("com.apple.preference.dock")
   else if selectedPreference = "Energy Saver" then
       revealPane("com.apple.preference.energysaver")
   else if selectedPreference = "General" then
       revealPane("com.apple.preference.general")
   else if selectedPreference = "Keyboard Shortcuts" then
       revealAnchor("com.apple.preference.keyboard", "shortcutsTab")
   else if selectedPreference = "Mouse" then
       revealPane("com.apple.preference.mouse")
   else if selectedPreference = "Network" then
       revealPane("com.apple.preference.network")
   else if selectedPreference = "Privacy" then
       revealAnchor("com.apple.preference.security", "Privacy")
   else if selectedPreference = "Software Update" then
       revealPane("com.apple.preferences.softwareupdate")
   end if
   
   writeSetting(settingFile, selectedPreference)
   
   delay 0.5 -- test different values if script does not operate correctly
end main

on revealPane(paneID)
   tell application "System Preferences"
       if running then
           activate
           reveal pane id paneID
       else
           reveal pane id paneID
           activate
       end if
   end tell
end revealPane

on revealAnchor(paneID, anchorName)
   tell application "System Preferences"
       if running then
           activate
           reveal anchor anchorName of pane id paneID
       else
           reveal anchor anchorName of pane id paneID
           activate
       end if
   end tell
end revealAnchor

on readSetting(theFile)
   try
       return paragraph 1 of (read file theFile)
   on error
       return ""
   end try
end readSetting

on writeSetting(theFile, theSetting)
   try
       set openedFile to open for access (file theFile) with write permission
       set eof of openedFile to 0
       write theSetting to openedFile
       close access openedFile
   on error
       try
           close access openedFile
       end try
   end try
end writeSetting

Last edited by peavine (Today 08:08:39 am)


2018 Mac mini - macOS Catalina

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)