概要
最近、朝方はライトモード、昼過ぎたらダークモードという感じで頻繁に切り替えているんですが、
$ dark-mode # ダークモードに切り替える
$ light-mode # ライトモードに切り替える
こんなコマンドが欲しいなと思ったのでググって作ってみました
手順
1. 二つのファイルに以下のAppleScriptそれぞれをコピペ
- ファイル名: dark-mode
# !/usr/bin/osascript
on setDarkMode(shouldBeDark)
set paneID to "com.apple.preference.general"
tell application "System Events"
if dark mode of appearance preferences is shouldBeDark then return
end tell
set paneWasOpen to false
(*
If SysPrefs is already running and showing the "General" pane,
then toggling dark mode causes the UI of the pane to become
out-of-sync with actual system appearance. To account for this,
we'll quit SysPrefs if it's open to General prefs, and then
re-open it after we've made the change
*)
if application "System Preferences" is running then
tell application "System Preferences"
if show all is false and id of current pane is paneID then
set paneWasOpen to true
quit
end if
end tell
end if
tell application "System Events"
set dark mode of appearance preferences to shouldBeDark
end tell
if paneWasOpen then
tell application "System Preferences"
launch
delay 3
activate
reveal (the first pane whose id is paneID)
end tell
end if
end setDarkMode
setDarkMode(true)
- ファイル名: light-mode
# !/usr/bin/osascript
on setDarkMode(shouldBeDark)
set paneID to "com.apple.preference.general"
tell application "System Events"
if dark mode of appearance preferences is shouldBeDark then return
end tell
set paneWasOpen to false
(*
If SysPrefs is already running and showing the "General" pane,
then toggling dark mode causes the UI of the pane to become
out-of-sync with actual system appearance. To account for this,
we'll quit SysPrefs if it's open to General prefs, and then
re-open it after we've made the change
*)
if application "System Preferences" is running then
tell application "System Preferences"
if show all is false and id of current pane is paneID then
set paneWasOpen to true
quit
end if
end tell
end if
tell application "System Events"
set dark mode of appearance preferences to shouldBeDark
end tell
if paneWasOpen then
tell application "System Preferences"
launch
delay 3
activate
reveal (the first pane whose id is paneID)
end tell
end if
end setDarkMode
setDarkMode(false)
2. 実行権限を付与して、PATHを通す
これでいけると思います。
スクリプトが手抜きですが、気にしない
参考
-
macOS 10.14 MojaveのLightとDark Modeをワンクリックで切り替えることができるApple Script。 | AAPL Ch.
-
- 上記の記事にあったAppleScript、ダイアログを表示しているが、本記事ではダイアログの部分は消しました
-
- AppleScriptのshebang(pythonとかでよくみる
#!
のやつ、正しく指定するととファイル名に拡張子が無くても実行できるようになります)
- AppleScriptのshebang(pythonとかでよくみる