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 September 2, 2024 Combine in SwiftUI and how you can rewrite the same code using async await

Combine is Apple’s declarative framework for handling asynchronous events and data streams in Swift. Introduced in SwiftUI and iOS 13, Combine leverages reactive programming principles, allowing developers to process values over time and manage complex asynchronous workflows with clarity and efficiency.

question swift September 8, 2024 How to convert only the first character to uppercase in a string in Swift?

In Swift, you can make the first character of a string uppercase by using built-in string manipulations or by extending the String type. Here’s an example of both approaches:

question macOS swift August 14, 2020 How to detect Delete key press in Swift?

Delete key press detection is slightly different than other keys. It uses NSDeleteCharacter like below: