To programmatically open a specific pane in System Settings (formerly System Preferences) like “Privacy & Security > Camera” on macOS using SwiftUI, you can leverage the NSWorkspace
class to open specific preference panes using URL schemes.
Here’s how you can open the “Privacy & Security > Camera” pane using SwiftUI and NSWorkspace
:
SwiftUI Code
import SwiftUI
import AppKit
struct ContentView: View {
var body: some View {
VStack {
Button("Open Privacy & Security > Camera Settings") {
openPrivacySecuritySettings()
}
}
.padding()
}
func openPrivacySecuritySettings() {
if let url = URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_Camera") {
NSWorkspace.shared.open(url)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Explanation
- URL Scheme: The URL
"x-apple.systempreferences:com.apple.preference.security?Privacy_Camera"
opens the “Privacy & Security > Camera” pane. NSWorkspace.shared.open(url)
: This opens the System Settings application to the desired pane using the URL.
The URL scheme used to target specific sub-panes (like “Camera”) isn’t officially documented, so its behavior can change across macOS versions. Apple does not provide an official public API for directly accessing all individual sections of System Settings (formerly System Preferences) on macOS, especially for sub-sections like Camera or Microphone under Privacy & Security. However, based on existing URL schemes and usage, you can open specific preference panes using the x-apple.systempreferences
URL scheme. Below is a list of known URLs that work with NSWorkspace
to open various System Settings panes. Just replace the URL string with any of the below:
General Preference Panes
- General
x-apple.systempreferences:com.apple.preference.general
- Desktop & Screen Saver
x-apple.systempreferences:com.apple.preference.desktopscreeneffect
- Dock & Menu Bar
x-apple.systempreferences:com.apple.preference.dock
- Language & Region
x-apple.systempreferences:com.apple.preference.language
- Notifications
x-apple.systempreferences:com.apple.preference.notifications
Privacy & Security
- Privacy & Security (General)
x-apple.systempreferences:com.apple.preference.security
- Privacy & Security > Privacy > Location Services
x-apple.systempreferences:com.apple.preference.security?Privacy_LocationServices
- Privacy & Security > Privacy > Camera
x-apple.systempreferences:com.apple.preference.security?Privacy_Camera
- Privacy & Security > Privacy > Microphone
x-apple.systempreferences:com.apple.preference.security?Privacy_Microphone
- Privacy & Security > Privacy > Accessibility
x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility
- Privacy & Security > Privacy > Files and Folders
x-apple.systempreferences:com.apple.preference.security?Privacy_ApplicationData
Network & Internet
- Network
x-apple.systempreferences:com.apple.preference.network
- Wi-Fi
x-apple.systempreferences:com.apple.preference.network?service=Wi-Fi
- Bluetooth
x-apple.systempreferences:com.apple.preference.bluetooth
Sound & Display
- Sound
x-apple.systempreferences:com.apple.preference.sound
- Displays
x-apple.systempreferences:com.apple.preference.displays
Users & Groups
- Users & Groups
x-apple.systempreferences:com.apple.preference.users
Date & Time
- Date & Time
x-apple.systempreferences:com.apple.preference.datetime
Accessibility
- Accessibility
x-apple.systempreferences:com.apple.preference.universalaccess
Security & Privacy
- Firewall
x-apple.systempreferences:com.apple.preference.security?Firewall
- FileVault
x-apple.systempreferences:com.apple.preference.security?FileVault
Software Update
- Software Update
x-apple.systempreferences:com.apple.preferences.softwareupdate
Energy Saver
- Energy Saver
x-apple.systempreferences:com.apple.preference.energysaver
Time Machine
- Time Machine
x-apple.systempreferences:com.apple.preference.timemachine
Keyboard
- Keyboard
x-apple.systempreferences:com.apple.preference.keyboard
Mouse
- Mouse
x-apple.systempreferences:com.apple.preference.mouse
Trackpad
- Trackpad
x-apple.systempreferences:com.apple.preference.trackpad
Printers & Scanners
- Printers & Scanners
x-apple.systempreferences:com.apple.preference.printfax
Sharing
- Sharing
x-apple.systempreferences:com.apple.preferences.sharing
Notes
- Some sub-pane URLs may not behave exactly as expected, especially on newer versions of macOS (Ventura and later).
- This list includes known, documented, and tested URL schemes, but Apple’s URL scheme behavior may change in future macOS updates.