プロジェクタモード切替スクリプト、とりあえず動くものになった!
覚えているうちに記録……。
できること
下記を切り替える
省エネルギーを60分(電源アダプタ/バッテリともに) 、
スクリーンセーバを30分
↑↓
省エネルギーをデフォルトに戻す(電源アダプタ/バッテリともに) 、
スクリーンセーバを2分
環境
マシン|MacBook Pro (13-inch, 2016, Four Thunderbolt 3 Ports)
2.9 GHz Intel Core i5 16 GB 2133 MHz LPDDR3
OS|macOS Mojave 10.12.2 (18C54)
ソフト|Automator 2.9 (444.39)
コード!
インデントとかカラーリングされないので見にくいですが……
なんか良い方法ないのかな
set projectorMode to false --仮にプロジェクタモードをオフとする
--
--システム環境設定が開いていたら閉じる
--
if application "System Preferences" is running then
tell application "System Preferences" to quit
delay 2 --直ぐに次の動作をするとエラーとなるので待つ
end if
--
--まず省エネルギーを切り替える
--
tell application "System Preferences"
--"電源アダプタ"タブを開く
reveal anchor "Options" of pane id "com.apple.preference.energysaver"
activate
--アンカー名称取得コード --
--set current pane to pane "com.apple.preference.energysaver"
--get the name of every anchor of pane "com.apple.preference.energysaver"
--アンカー名称取得コード --
end tell
tell application "System Events"
if UI elements enabled then
tell application "System Events"
tell process "System Preferences"
--"電源アダプタ"ラジオボタンを押す
click radio button "電源アダプタ" of tab group 1 of window "省エネルギー"
delay 0.1
--30分未満なら projectorModeをオンにする(プロジェクタモード)
if value of slider 1 of tab group 1 of window "省エネルギー" < 270 then set projectorMode to true
if projectorMode then
--プロジェクタモードなら
--スライダ値を62分ぐらいに
set value of slider 1 of tab group 1 of window "省エネルギー" to 550
--値が保存されないので、キーを使ってスライダを動かす
delay 0.1
keystroke tab
delay 0.1
keystroke tab
delay 0.1
keystroke (ASCII character 28) --左矢印
delay 1 --アラートが出るまで少し時間がかかるので待つ
keystroke return --アラートに対しOK(return)
else
--プロジェクタモードではないなら
--"デフォルトに戻す"ボタンを押す
click button "デフォルトに戻す" of tab group 1 of window "省エネルギー"
end if
--"バッテリー"ラジオボタンを押す
click radio button "バッテリー" of tab group 1 of window "省エネルギー"
delay 0.1
if projectorMode then
--プロジェクタモードなら
--スライダ値を62分ぐらいに
set value of slider 1 of tab group 1 of window "省エネルギー" to 550
--値が保存されないので、キーを使ってスライダを動かす
delay 0.1
keystroke (ASCII character 28) --左矢印
delay 0.1
keystroke return
else
--プロジェクタモードではないなら
--"デフォルトに戻す"ボタンを押す
click button "デフォルトに戻す" of tab group 1 of window "省エネルギー"
end if
end tell
end tell
else
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.universalaccess"
display dialog "UI element scripting is not enabled."
end tell
end if
end tell
--
--次にスクリーンセーバを切り替える
--
tell application "System Preferences"
reveal anchor "DesktopPref" of pane id "com.apple.preference.desktopscreeneffect"
activate
delay 1 --paneが切り替わるまで時間かかるので待つ
--アンカー名称取得コード --
--set current pane to pane "com.apple.preference.desktopscreeneffect"
--get the name of every anchor of pane "com.apple.preference.desktopscreeneffect"
--アンカー名称取得コード --
end tell
tell application "System Events"
if UI elements enabled then
tell application "System Events"
tell process "System Preferences"
--"スクリーンセーバ"ボタンを押す
click radio button "スクリーンセーバ" of tab group 1 of window 1
delay 1 --ラジオボタン押してから切り替わるまで時間かかるので待つ
if projectorMode then
--プロジェクタモードなら"30分"を選ぶ
tell pop up button 1 of tab group 1 of window 3
click
delay 0.1
click menu item "30分" of menu 1
end tell
else
--プロジェクタモードでないなら"2分"を選ぶ
tell pop up button 1 of tab group 1 of window 3
click
delay 0.1
click menu item "2分" of menu 1
end tell
end if
end tell
end tell
else
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.universalaccess"
display dialog "UI element scripting is not enabled."
end tell
end if
end tell
ポイントメモ
と思ったけど、だいたい途中経過で書いたので、
まあいいか。
projectorModeの判定をして(スライダが30分未満かどうかで判定)、
true なら ディスプレイオフ時間とスクリーンセーバ時間を長く
false なら ディスプレイオフ時間とスクリーンセーバ時間を短く
コードの美しさ(構造の美しさ)は今回は問わない。
動けばいい。スクリプトだし。
anchorが違うほうが動作がきれいだろうなというのも
あるけど、今回は問わない。
変えたらデバッグが発生する。
工数削減優先。
本プロジェクトのヒストリー
これまでの記録を辿れるようにしておけば、
必要なリソースは見つけられるだろう(将来の自分)。
【記録】プロジェクタモードに切替(Automator、AppleScript、UI Element) - 虎(牛)龍未酉2.1
【記録】プレゼンモードに切替(Automator、shell script) - 虎(牛)龍未酉2.1
ざっくりいうと、
- Automatorとshell scriptでやろうと思った
- が考え直してAutomatorとAppleScriptでやることにした
- 動作不信の原因(delayを入れないとおかしくなる)を発見
- トグル(切替)になるようif文を入れた
という流れだった。
いつの日かQiitaにまとめようと思っているが
どうなることやら。