Loading...

How to open macOS System Settings (or a specific pane) programmatically with Swift?

question swift macos
Ram Patra Published on September 15, 2024

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

  1. General
    x-apple.systempreferences:com.apple.preference.general
    
  2. Desktop & Screen Saver
    x-apple.systempreferences:com.apple.preference.desktopscreeneffect
    
  3. Dock & Menu Bar
    x-apple.systempreferences:com.apple.preference.dock
    
  4. Language & Region
    x-apple.systempreferences:com.apple.preference.language
    
  5. Notifications
    x-apple.systempreferences:com.apple.preference.notifications
    

Privacy & Security

  1. Privacy & Security (General)
    x-apple.systempreferences:com.apple.preference.security
    
  2. Privacy & Security > Privacy > Location Services
    x-apple.systempreferences:com.apple.preference.security?Privacy_LocationServices
    
  3. Privacy & Security > Privacy > Camera
    x-apple.systempreferences:com.apple.preference.security?Privacy_Camera
    
  4. Privacy & Security > Privacy > Microphone
    x-apple.systempreferences:com.apple.preference.security?Privacy_Microphone
    
  5. Privacy & Security > Privacy > Accessibility
    x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility
    
  6. Privacy & Security > Privacy > Files and Folders
    x-apple.systempreferences:com.apple.preference.security?Privacy_ApplicationData
    

Network & Internet

  1. Network
    x-apple.systempreferences:com.apple.preference.network
    
  2. Wi-Fi
    x-apple.systempreferences:com.apple.preference.network?service=Wi-Fi
    
  3. Bluetooth
    x-apple.systempreferences:com.apple.preference.bluetooth
    

Sound & Display

  1. Sound
    x-apple.systempreferences:com.apple.preference.sound
    
  2. Displays
    x-apple.systempreferences:com.apple.preference.displays
    

Users & Groups

  1. Users & Groups
    x-apple.systempreferences:com.apple.preference.users
    

Date & Time

  1. Date & Time
    x-apple.systempreferences:com.apple.preference.datetime
    

Accessibility

  1. Accessibility
    x-apple.systempreferences:com.apple.preference.universalaccess
    

Security & Privacy

  1. Firewall
    x-apple.systempreferences:com.apple.preference.security?Firewall
    
  2. FileVault
    x-apple.systempreferences:com.apple.preference.security?FileVault
    

Software Update

  1. Software Update
    x-apple.systempreferences:com.apple.preferences.softwareupdate
    

Energy Saver

  1. Energy Saver
    x-apple.systempreferences:com.apple.preference.energysaver
    

Time Machine

  1. Time Machine
    x-apple.systempreferences:com.apple.preference.timemachine
    

Keyboard

  1. Keyboard
    x-apple.systempreferences:com.apple.preference.keyboard
    

Mouse

  1. Mouse
    x-apple.systempreferences:com.apple.preference.mouse
    

Trackpad

  1. Trackpad
    x-apple.systempreferences:com.apple.preference.trackpad
    

Printers & Scanners

  1. Printers & Scanners
    x-apple.systempreferences:com.apple.preference.printfax
    

Sharing

  1. 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.
Presentify

Take your presentation to the next level.

FaceScreen

Put your face and name on your screen.

ToDoBar

Your to-dos on your menu bar.

Ram Patra Published on September 15, 2024
Image placeholder

Keep reading

If this article was helpful, others might be too

question swiftui swift August 31, 2024 @StateObject vs @ObservedObject in SwiftUI

In SwiftUI, both @StateObject and @ObservedObject are property wrappers used to manage state in your views, specifically when working with objects that conform to the ObservableObject protocol. However, they serve slightly different purposes and have different use cases. Here’s a breakdown:

question swift macOS October 29, 2023 How to make Color conform to RawRepresentable in SwiftUI in macOS?

For various reasons you may want to convert the Color type to a String. And, below is a relatively cleaner way to do it.

question swift November 17, 2023 How do get the string value of an enum in Swift?

In Swift, to get the string value of an enum, you typically have a couple of approaches depending on the enum’s definition. Let’s go through them: