Other way to open System Preference Pane.

This is maybe not the best approach…
But I like to show a different way to open System Preference Pane.

The extension ‘prefPane’ is default open with ‘System Preferences’ so we only need to
set the path and use open command.

set thePathPref to (path to library folder from system domain as text) & "PreferencePanes:"

tell application "System Preferences"
	open thePathPref & "Accounts.prefPane"
end tell

tell application "System Preferences"
	open thePathPref & "Appearance.prefPane"
end tell

tell application "System Preferences"
	open thePathPref & "Bluetooth.prefPane"
end tell

tell application "System Preferences"
	open thePathPref & "DateAndTime.prefPane"
end tell

tell application "System Preferences"
	open thePathPref & "DesktopScreenEffectsPref.prefPane"
end tell

(*
tell application "System Preferences"
	open thePathPref & "DidHubDiscs.prefPane"
end tell
*)

tell application "System Preferences"
	open thePathPref & "Displays.prefPane"
end tell

tell application "System Preferences"
	open thePathPref & "Dock.prefPane"
end tell

tell application "System Preferences"
	open thePathPref & "EnergySaver.prefPane"
end tell

tell application "System Preferences"
	open thePathPref & "Expose.prefPane"
end tell

tell application "System Preferences"
	open thePathPref & "Extensions.prefPane"
end tell

(*
tell application "System Preferences"
	open thePathPref & "FibreChannel.prefPane"
end tell
*)

tell application "System Preferences"
	open thePathPref & "iCloudPref.prefPane"
end tell

(*
tell application "System Preferences"
	open thePathPref & "Ink.prefPane"
end tell
*)

tell application "System Preferences"
	open thePathPref & "InternetAccounts.prefPane"
end tell

tell application "System Preferences"
	open thePathPref & "Keyboard.prefPane"
end tell

tell application "System Preferences"
	open thePathPref & "Localization.prefPane"
end tell

tell application "System Preferences"
	open thePathPref & "Mouse.prefPane"
end tell

tell application "System Preferences"
	open thePathPref & "Network.prefPane"
end tell

tell application "System Preferences"
	open thePathPref & "Notification.prefPane"
end tell

tell application "System Preferences"
	open thePathPref & "ParentalControls.prefPane"
end tell

tell application "System Preferences"
	open thePathPref & "PrintAndScan.prefPane"
end tell

tell application "System Preferences"
	open thePathPref & "Profiles.prefPane"
end tell

tell application "System Preferences"
	open thePathPref & "Security.prefPane"
end tell

tell application "System Preferences"
	open thePathPref & "SharingPref.prefPane"
end tell

tell application "System Preferences"
	open thePathPref & "SoftwareUpdate.prefPane"
end tell

tell application "System Preferences"
	open thePathPref & "Sound.prefPane"
end tell

tell application "System Preferences"
	open thePathPref & "Speech.prefPane"
end tell

tell application "System Preferences"
	open thePathPref & "Spotlight.prefPane"
end tell

tell application "System Preferences"
	open thePathPref & "StartupDisk.prefPane"
end tell

tell application "System Preferences"
	open thePathPref & "TimeMachine.prefPane"
end tell

(*
tell application "System Preferences"
	open thePathPref & "TouchID.prefPane"
end tell
*)

tell application "System Preferences"
	open thePathPref & "Trackpad.prefPane"
end tell

tell application "System Preferences"
	open thePathPref & "UniversalAccessPref.prefPane"
end tell

(*
tell application "System Preferences"
	open thePathPref & "Wallet.prefPane"
end tell
*)

Thanks Fredrik71 for the post. I’ve never scripted System Preferences but this got me started.

You say that your script is the other way to do this, and I wondered what the first approach is? Perhaps the following:

tell application "System Preferences"
	activate
	set the current pane to pane id "com.apple.preference.general"
end tell

BTW, there is a path-to command for System Preferences in Catalina, although I don’t know if works with earlier versions.

It’s an old command which is available at least in 10.13.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 2 aout 2020 18:58:29

You can actually open specific tabs of panes. Here’s an example:

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

current application's NSWorkspace's sharedWorkspace()'s openURL:(current application's NSURL's URLWithString:"x-apple.systempreferences:com.apple.preference.security?Privacy_Camera")

The x-apple URLs to use were listed at macosxautomation.com, but the link no longer works. However, you can find them in the second reply posted here:

https://stackoverflow.com/questions/6652598/cocoa-button-opens-a-system-preference-page

Thanks Shane. The following is a snippet showing the approach I have taken. It works OK but doesn’t allow me to select a specific item within a tab of a preference pane (camera item in privacy tab in your script).

-- choose from list dialog

if selectedPreference = "General" then
	set preferencePane to "com.apple.preference.general"
	set shortcutTab to "Main"
else if selectedPreference = "Keyboard" then
	set preferencePane to "com.apple.preference.keyboard"
	set shortcutTab to "keyboardTab"
	-- other items deleted for brevity
end if

tell application "System Preferences"
	reveal anchor shortcutTab of pane id preferencePane
	activate
end tell

The issue I encouter with the approach you suggest is getting the specific URL’s to use. Just one example is:

System Preferences > Keyboard > Shortcuts > App Shortcuts

Without some method to get that information the script just won’t work. I’ll study the post you reference to see if I can get the information but I fear that’s beyond my current knowledge level.

Apologies to Fredrik71 for taking thread a bit off topic.

The way I will do it then was…

Open System Preferences and go to the pane of interest. Run the below script.
It will return information… next I will use GUI Scripting to extract every UI elements.

tell application "System Preferences"
	get every anchor of current pane
end tell

You could then do, I read somewhere I believe it was Bill Cheeseman that said activate statement after the command reveal the pane faster. But maybe I misunderstood it…

tell application "System Preferences"
	reveal anchor "DesktopPref" of pane id "com.apple.preference.desktopscreeneffect"
	activate
end tell

you could also do…

Open System Preferences, run below Script.

tell application "System Preferences"
	tell every pane
		its id
	end tell
end tell

PS.
I made long ago AS Script that you could find in code exchange. I use above code to get what I want.
For me it was very fast way to find everything in System Preferences.
https://macscripter.net/viewtopic.php?pid=200671#p200671

Today I will maybe do something like this. It will give me the correct UI elements to do for
GUI Scripting in the event log.

Its works like this…

Open System Preferences go to the pane of interest. Run below script and click somewhere in the UI elements of System Preference. It will give you that UI element that was focus.
Run the script again and click somewhere else and you could see it give you a different UI element.

-- Only to give little time to move the mouse to right UI element for mouse click.
delay 3

axFocusedUIElement("System Preferences")

on axFocusedUIElement(_appName)
	activate application _appName
	tell application "System Events"
		set theApplication to first application process whose frontmost is true
		set theControl to value of attribute "AXFocusedUIElement" of theApplication
		-- get the name of every attribute of theApplication
	end tell
end axFocusedUIElement

Here is example of above Script have been used to extract UI element from Shortcuts in Keyboard of System Preferences.

-- table 1 of scroll area 1 of splitter group 1 of tab group 1 of window "Keyboard" of application process "System Preferences" of application "System Events"


tell application "System Events" to tell application process "System Preferences"
	set frontmost to true
	
	-- The UI element code we find above...
	tell window "Keyboard" to tell tab group 1 to tell splitter group 1 to tell scroll area 1
		tell table 1
			-- its properties -- not much... then we do
			-- its value of attributes --> aha, table 1 include row.
			tell every row to its static text
			tell every row
				its name of its static text --> now we have a list of text.
			end tell
		end tell
	end tell
	----------------------------
end tell

And doing something more advance its could…

tell application "System Events" to tell application process "System Preferences"
	set frontmost to true
	
	-- Here the code we find above...
	tell window "Keyboard" to tell tab group 1 to tell splitter group 1 to tell scroll area 1
		tell table 1
			-- class of UI elements, return row and column
			tell every row to its name of static text
			{{"Launchpad & Dock"}, {"Mission Control"}, {"Keyboard"}, {"Screenshots"}, {"Services"}, {"Spotlight"}, {"Accessibility"}, {"App Shortcuts"}}
			
			-- Lets build a list
			tell every row
				set theName to its name of static text
				set theList to {}
				repeat with i in theName
					repeat with j in i
						copy the contents of j to end of theList
					end repeat
				end repeat
				
				try
					set theItem to (choose from list theList) as text
				end try
				
				-- Build the index
				set idx to missing value
				repeat with n from 1 to count theList
					if theList's item n is theItem then set idx to n
				end repeat
				---
			end tell
			select row idx
			--------------
		end tell
	end tell
	----------------------------
end tell

And if we like to know what all attributes does… instead of output everything in the event log.
This code will make a list of every attributes in from row in outlines 1

Ps. You need to have Keyboard row of shortcuts tab open in system preferences to run this code.

-- outline 1 of scroll area 2 of splitter group 1 of tab group 1 of window "Keyboard" of application process "System Preferences" of application "System Events"

tell application "System Events" to tell application process "System Preferences"
	set frontmost to true
	
	-- Here is the code axFocusedUIElement handler return.
	tell window "Keyboard" to tell tab group 1 to tell splitter group 1 to tell scroll area 2
		tell outline 1
			tell every row
				-- set theAttributes to its name of every attribute
				-- get the value of attribute "AXSize"
				
				-- Lets build a list
				set theName to its name of every attribute
				set theList to {}
				repeat with i in theName
					repeat with j in i
						copy the contents of j to end of theList
					end repeat
				end repeat
				
				set theAttribute to (choose from list theList) as text
				return (its value of attribute theAttribute)
			end tell
		end tell
	end tell
end tell

The interesting part about building a list of attributes of UI elements we could learn
very quickly what this attributes does.

ex.

AXSelected will give the index position of current selected UI element
it return something like this from Shortcuts tab > Keyboard
When ‘Turn keyboard access on or off’ was selected.

→ {false, true, false, false, false, false, false, false, false, false}

Or

Instead of asking for class of UI elements we could ask for AXChildren.

Hmm… interesting.