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.


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.


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

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.


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:

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

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:

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:


	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

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.

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.

Thank, Peavine.
I tried to simplify your last script. Maybe it became better as well.


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

try
	set theText to (read file settingFile)
	set {defaultAnswer1, defaultAnswer2} to {paragraph 1 of theText, paragraph 2 of theText}
on error
	set {defaultAnswer1, defaultAnswer2} to {"", ""}
end try

tell application "System Preferences" to set panesNamesList to name of panes
set selectedPreference to (choose from list panesNamesList default items defaultAnswer1 with title "Select Pane Name")
if selectedPreference = false then error number -128
set selectedPreference to item 1 of selectedPreference

tell application "System Preferences"
	set anchorNamesList to name of anchors of pane selectedPreference
	set selectedAnchor to (choose from list anchorNamesList default items defaultAnswer2 with title "Select Anchor Name")
	if selectedAnchor = false then error number -128
	set selectedAnchor to item 1 of selectedAnchor
	reveal anchor selectedAnchor of pane selectedPreference
end tell

try
	set openedFile to open for access (file settingFile) with write permission
	set eof of openedFile to 0
	write (selectedPreference & return & selectedAnchor) to openedFile
	close access openedFile
on error
	try
		close access openedFile
	end try
end try

This is the current version of my script. It utilizes a plist rather than a text file to save a default and adds an “All” item to the dialog. To test this script, open it in a script editor and run. This script works without issue on Catalina but may not work on other versions of macOS due to differing pane ID and anchor names. This is fixed with the helper script in post 1.

-- Revised 2021.06.27

use framework "Foundation"
use scripting additions

on main()
	set settingPlist to POSIX path of (path to preferences as text) & "SystemPreferencesMenu.plist"
	
	set dialogDefault to readPlist(settingPlist)
	if dialogDefault = "missing value" then set dialogDefault to "All"
	
	set preferenceList to {"All", "Dock", "Energy Saver", "General", "Keyboard Shortcuts", "Mouse", "Network", "Security & Privacy", "Software Update"}
	
	choose from list preferenceList default items dialogDefault OK button name "Open" with title "System Preferences"
	if result = false then
		error number -128
	else
		set selectedPreference to item 1 of result
	end if
	
	if selectedPreference = "All" then
		showAll()
	else 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 = "Security & Privacy" then
		revealAnchor("com.apple.preference.security", "Privacy")
	else if selectedPreference = "Software Update" then
		revealPane("com.apple.preferences.softwareupdate")
	end if
	
	writePlist(settingPlist, selectedPreference)
	delay 0.3 -- test different values if script does not operate correctly
end main

on showAll()
	tell application "System Preferences"
		activate
		set show all to true
	end tell
end showAll

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 readPlist(thePath)
	set theString to current application's NSString's stringWithContentsOfFile:thePath
	return theString as text
end readPlist

on writePlist(thePath, theString)
	set theString to current application's NSString's stringWithString:theString
	theString's writeToFile:thePath atomically:true
end writePlist

main()