How to programatically open Settings/Preferences window in a macOS SwiftUI App

By inspecting the app menu, I found this solution:

NSApp.sendAction(Selector(("showPreferencesWindow:")), to: nil, from: nil)

I assume that it may not pass App Review for using a private API, though, so less shady alternatives are still welcome.

Update: This has actually passed App Review, so I had marked it as the answer.

Update 2: For a macOS 13 solution, see Manny’s answer.

Update 3: For macOS 14 with SwiftUI, you must use the new SettingsLink view documented here. NSApp.sendAction no longer works.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)