1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Macのダークモード/ライトモードをCLIで切り替えるスクリプト

Posted at

概要

最近、朝方はライトモード、昼過ぎたらダークモードという感じで頻繁に切り替えているんですが、

$ 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を通す

これでいけると思います。

スクリプトが手抜きですが、気にしない

参考

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?