虎(牛)龍未酉2.1

記録帳|+n年後のジブンが思い出せますように……

【記録】プロジェクタモード

道半ばやけどそろそろメモっとかな忘れる自信あり

 

1)shell scriptかAppleScript

 

shell scriptのメリット:明示的に指定できる

shell scriptのデメリット:デフォルトの設定からずれる、システム環境設定から遠のく

 

AppleScriptのメリット:けっこう面倒くさい、情報も少ない(気がする)

AppleScriptのデメリット:システム環境設定の世界のなかで完結する

 

今回はAppleScriptを追求してみることとした。

 

 

2)今のとこのコードと、急所

 

--

--まず省エネルギーを長く

--

 

tell application "System Preferences"

reveal anchor "Options" of pane id "com.apple.preference.energysaver"

activate

--reveal anchor "PortableOptions" 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 1

set value of slider 1 of tab group 1 of window "省エネルギー" to 550 --スライダ値を62分ぐらいに

-- 値が保存されないので、キーを使ってスライダを動かす

delay 2

keystroke tab

delay 1

keystroke tab

delay 1

keystroke (ASCII character 28) -- 左矢印

delay 1

keystroke return -- アラートに対しOKreturn

 

click radio button "バッテリー" of tab group 1 of window "省エネルギー"

delay 1

set value of slider 1 of tab group 1 of window "省エネルギー" to 550 -- スライダ値を62分ぐらいに

-- 値が保存されないので、キーを使ってスライダを動かす

delay 1

keystroke (ASCII character 28) -- 左矢印

delay 1

keystroke return

-- end tell

 

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"

--reveal anchor "ScreenSaverPref" of pane id "com.apple.preference.desktopscreeneffect"

activate

delay 2

-- アンカー名称取得コード --

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

 

tell pop up button 1 of tab group 1 of window 3

click

delay 1

click menu item "30" of menu 1

end tell

 

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

 

・オブジェクト名称(radio button 1とか)を探してくるのが一番大変

 ①動いている(らしい)コードを拾ってくる

 ②Xcodeをダウンロードし、Accessibility Inspectorを使う

  Xcode > Open Developer Tool > Accessibility Inspector

f:id:masatora_bd5:20181218222337p:plain

 

・下記のところはなんでwindow 3なのか未だにわからない。

 どうやってwindow 3だと見つけたのかも不明

tell pop up button 1 of tab group 1 of window 3

click

delay 1

click menu item "30分" of menu 1

end tell 

 

 

・何に苦労した(引き続き苦労しているか)って、UI elementを指定

 しているのに、OKなときとNGなときがあって、その再現条件が

 わからん

 

 ①怪しいところでdelay 1〜2を入れるようにしてみた。

  今のところ動いているのでまあ良しとするか

 

 ②tell application "System Preferences"

  reveal anchor "Options" of pane id "com.apple.preference.energysaver"

  activate

  end tell

  方式で、anchorを指定して開くようにしてみた。

 

 ③anchor指定して開いた上で、ラジオボタンをクリックして狙いの

  画面に遷移させる

 

 ②ー1。システム環境設定のpane idを拾うには

  tell application "System Preferences" to every pane

  スクリプトエディタで動かせば結果が下に見える

f:id:masatora_bd5:20181218224010p:plain

 

 ②ー2。paneのanchorを拾うには(上のコメントにも

  残しているが)

set current pane to pane "com.apple.preference.desktopscreeneffect"

get the name of every anchor of pane "com.apple.preference.desktopscreeneffect"

  などなど

 

 

3)残課題

 

・delayのタイミングをもう少し短くしたい

 環境依存しそうなのでもう良いかという気もする

 

・本当はトグルさせたくて、ディスプレイをオフにするまでの時間を

 拾って、長ければ、デフォルトに戻し、スクリーンセーバを短くする

 動きをさせたい。

 

4)参考リンク

 

まずは自画自賛だけど自分の過去メモ

Workflow + AppleScriptでMac(Book Pro)の明るさ(輝度)を調整してバッテリライフを伸ばそうと - Qiita

【備忘録】輝度(75%)に自動的に調整する by AppleScript (Automator) - 虎(牛)龍未酉2.1

なんだか苦労した、解像度の切り替え by AppleScript (Automator) - 虎(牛)龍未酉2.1

 

その他

 

anchorか!と思ったのはこの記事さまさま

AppleScriptでサウンド設定を自動化 - Qiita

 

paneのanchorの拾い方とか諸々詳しい

AppleScript: Scriptable System Preferences

 

省エネルギー設定でset valueだけではダメなことと、解決策を示してある救世主

Applescript To Change Power Settings

 

キーボードのアスキーコード

キーボードを操作できるApplescript | 戦業主夫

 

それではうまくいかなくて困っているんだな……ということも多いけれど、やりたいことは同じなので、次の課題解決に使わせていただきたい

AppleScriptことはじめ | NYORO PRESS

 

直接使わなかったが、全体像がわかるような気がした

よく行われる作業:ユーザインターフェイスを自動化する - mytrans マニュアル等の個人的な翻訳