4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

macOSのシステム設定をSwiftから開く

Last updated at Posted at 2024-12-17

システム画面の特定の画面を開く

  • 例えばPrivacy & Securityは下記の2通りのやり方で開くことができる
  • 一つは.prefPaneファイルを開く方法で、もう一方はschemeで指定して開く方法
  • どちらも動きは同じだが、その設定画面のさらに詳細を開きたい際にはSchemeを使うと良い
Button("\"プライバシーとセキュリティ\"を開く (.prefPane経由)") {
    let url = URL(filePath: "/System/Library/PreferencePanes/Security.prefPane", directoryHint: .notDirectory)
    NSWorkspace.shared.open(url)
}

Button("\"プライバシーとセキュリティ\"を開く (Scheme経由)") {
    guard let url = URL(string: "x-apple.systempreferences:com.apple.settings.PrivacySecurity.extension") else {
        return
    }
    NSWorkspace.shared.open(url)
}
  • Swiftではなくターミナルで試したい場合は以下
open "/System/Library/PreferencePanes/Security.prefPane"
open "x-apple.systempreferences:com.apple.settings.PrivacySecurity.extension"

.prefPaneの選択

  • .prefPaneファイルは/System/Library/PreferencePanes以下に配置されているので利用したいものを選択すればOK

schemeのペインIDの選択

  • またこのplistに含まれない詳細設定もあり、その際は下記をターミナルで実行するとペインIDの一覧が取得できる
for pref in $(strings "/System/Applications/System Settings.app/Contents/MacOS/System Settings" | awk '/^com.apple./ {print $1 }'); do echo "$pref"; done
(出力結果)
com.apple.systempreferences.debug
com.apple.ClassroomSettings
com.apple.CD-DVD-Settings.extension
com.apple.preferences.ClassKitPreferencePane
...

ペインIDとアンカーの取得

  • 例えばPrivacy & Security > Cameraの設定を開きたい
  • これはSchemeの後ろに?Privacy_Cameraとアンカーをつけるとできる
x-apple.systempreferences:com.apple.settings.PrivacySecurity.extension?Privacy_Camera

-- Open System Preferences.app and click into desired pane/setting. Then, run this script to find out name (Pane ID) and any anchors.

tell application "System Settings"
	set AppleScript's text item delimiters to ", "
	set CurrentPane to the id of the current pane
	get the name of every anchor of pane id CurrentPane
	set CurrentAnchors to get the name of every anchor of pane id CurrentPane
	set the clipboard to CurrentPane
	display dialog "Current Pane ID: " & CurrentPane & return & return & "Pane ID has been copied to the clipboard." & return & return & "Current Anchors: " & return & (CurrentAnchors as string)
end tell
  • 下記のようにペインIDとアンカーがそれぞれ取得できる

com.apple.settings.PrivacySecurity.extension
Privacy_Camera

参考

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?