虎(牛)龍未酉2.1

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

なんだか苦労した、解像度の切り替え by AppleScript (Automator)

やりたいことは単純で、

f:id:masatora_bd5:20170517231604p:plain

f:id:masatora_bd5:20170517231637p:plain

の切り替えが頻発するので、簡単にやれるようにしたかったわけ。

いつも通り、ワークフローで作り、アプリ書き出しし、

今回はtoggleRes.appにしたので、spotlight > toggleと入力して起動。

 

できたコードはこんな感じ(インデント崩れてるけど)

on run {input, parameters}

 

if application "System Preferences" is running then

tell application "System Preferences" to quit

end if

 

tell application "System Preferences"

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

end tell

 

tell application "System Events"

 

if UI elements enabled then

 

tell process "System Preferences"

if value of radio button 1 of radio group 1 of tab group 1 of window 1 is 1 then

click radio button 2 of radio group 1 of tab group 1 of window 1

click radio button 4 of radio group 1 of group 2 of tab group 1 of window 1

else if value of radio button 2 of radio group 1 of tab group 1 of window 1 is 1 then

click radio button 1 of radio group 1 of tab group 1 of window 1

end if

end tell

 

quit application "System Preferences"

 

else

 

tell application "System Preferences"

activate

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

display dialog "UI element スクリプティングが有効になっていません。 「プライバシー」タブの「下のアプリケーションにコンピュータの制御を許可」にチェックを入れて下さい。"

end tell

 

end if

 

end tell

 

return input

 

end run

 何が良くて何が良くないのかさっぱりわからないけれど

(たまにエラーが起きる)、

 

・システム環境設定が起動していたらquitする

にしてみた。その他いろいろ試してみたけど(アンカーを使うとか)、

結局何が良くて何が悪いかわからん。

 

とりあえず動くからいいやと、いい加減な終わらせ方。

 

今回もこちらにかなりお世話になった:

AppleScriptでモニタの明るさを調整したい

https://discussionsjapan.apple.com/thread/10062506?start=0&tstart=0