AppleScript
AppleScriptでcheckboxを操作する

はじめに

今回は、AppleScriptでチェックボックスの操作を行う

チェックボックスの状態を調べて、オンオフを行う必要がある

チェックボックスには、オンとオフの状態があるので現在の状態を調べて求めている値に変える必要がある。今回は、システム環境設定の「一般」で「メニューバーを自動的に表示/非表示」の状態を切り替えを例に説明していく

とりあえず、システム環境設定を立ち上げて、「一般」を選択する

最終的なスクリプトの流れとして、システム環境設定を立ち上げる→「一般」を選択する→「メニューバーを自動的に表示/非表示」をオンにする にする。

とりあえず、必要な情報を調べる。システム環境設定の要素がどうなってるのか知りたいので、環境設定を立ち上げた状態で以下のスクリプトで要素を調べる。


tell application "System Events"
	tell process "System Preferences"
		every UI element
	end tell
end tell


結果の中に、radio group 1 というのがあるので、スクリプトの変数に radio group 1 of を書き足して実行させる


tell application "System Events"
	set pla to radio group 1 of group 1 of window "名称未設定" of application process "TextEdit" of application "System Events"
	tell pla
		every UI element
	end tell
end tell

以下の結果を得られる。(例のごとく、of application process "System Preferences" of application "System Events" は長いので置換して削除してある)


{window "システム環境設定" , menu bar 1 , UI element 3 }

window "システム環境設定"の要素を調べていく


tell application "System Events"
	tell process "System Preferences"
		tell window "システム環境設定"
			every UI element
		end tell
	end tell
end tell

以下の結果を得られる。(例のごとく、いらん部分は置換して削除してある)


{scroll area 1 , button 1 , button 2 , button 3 , toolbar 1 }

今度は、scroll area 1 を調べてみる


tell application "System Events"
	tell process "System Preferences"
		tell window "システム環境設定"
			tell scroll area 1
				every UI element
			end tell
		end tell
	end tell
end tell

お目当てのものの要素が出てくる、と言うことで、scroll area 1のボタンをクリックすれば望むメニューに行くことができる。それはあとで使うとして、手動で"一般"を選んで、チェックボックスの在りかを調べていく


{button "一般" , button "デスクトップとスクリーンセーバ" , button "Dock" , button "MissionControl" , button "言語と地域" , button "セキュリティとプライバシー" , button "Spotlight" , button "通知" , button "ディスプレイ" , button "省エネルギー" , button "キーボード" , button "マウス" ,button "トラックパッド" , button "プリンタとスキャナ" , button "サウンド" , button "起動ディスク" , button "iCloud" , button "インターネットアカウント" , button "WalletとApple Pay" , button "ソフトウェアアップデート" , button "ネットワーク" , button "Bluetooth" , button "機能拡張" , button "共有" , button "Touch ID" , button "ユーザとグループ" , button "ペアレンタルコントロール" , button "Siri" , button "日付と時刻" , button "TimeMachine" , button "アクセシビリティ" , button "Flash Player" }

と言うことで、一般に移動すると、環境設定のウインドウ名が "一般" に変わるので UI element で要素を調べる


tell application "System Events"
	tell process "System Preferences"
		tell window "一般"
			every UI element
		end tell
	end tell
end tell

と、以下の結果を得られる


{static text "スクロールバーのクリック時:" , static text "サイドバーのアイコンサイズ:" , radio group 1 , static text "強調表示色:" , pop up button "強調表示色:" , static text "選択すると、開いていた書類とウインドウは、アプリケーションの再開時には復元されません。" , radio group 2 , button 1 , pop up button "サイドバーのアイコンサイズ:" , checkbox "アプリケーションを終了するときにウインドウを閉じる" , checkbox "書類を閉じるときに変更内容を保持するかどうかを確認" , static text "デフォルトのWebブラウザ:" , pop up button 3 , static text "スクロールバーの表示:" , static text "最近使った項目:" , static text "個の書類、アプリケーション、およびサーバ" , checkbox "使用可能な場合は滑らかな文字を使用" , checkbox "このMacとiCloudデバイス間でのHandoffを許可" , pop up button 4 , checkbox "メニューバーを自動的に表示/非表示" , checkbox "ライト" , checkbox "ブルー" , checkbox "ダーク" , static text "外観モード:" , static text "アクセントカラー:" , static text "ライト" , static text "ダーク" , checkbox "パープル" , checkbox "ピンク" , checkbox "レッド" , checkbox "オレンジ" , checkbox "イエロー" , checkbox "グリーン" , checkbox "グラファイト" , button 2 , button 3 , button 4 , toolbar 1 }

checkbox "メニューバーを自動的に表示/非表示" があるのが分かる。環境設定では要素名が付けられているので分かりやすいが、数字の連番だと総当たりで行くしかない。この項目が真ん中位にあるのは、おそらく後から追加された機能だからなんじゃなかろうかと思っている。何にせよ、名前を付けてくれているのでもうまんたい。

で、click checkbox "メニューバーを自動的に表示/非表示" をすればcheckbox自体はクリックできるが、それだとどの状態になるのか分からないので条件式で状態を調べてクリックさせる。チェックがある場合は、value が1 ない場合は、0なので、たとえば必ずcheckboxをオンにしたい場合は以下のようなスクリプトを走らせる


tell application "System Events"
	tell process "System Preferences"
		tell window "一般"
			#オフの場合
			if (value of checkbox "メニューバーを自動的に表示/非表示") = 0 then
				click checkbox "メニューバーを自動的に表示/非表示"
			end if
			
		end tell
	end tell
end tell

と言うことで、すべての動作をがっちゃんこさせる。これで、スクリプトを走らせると、"メニューバーを自動的に表示/非表示"のcheckboxがオンになる


tell application "System Preferences"
	activate
end tell

tell application "System Events"
	tell application "System Events"
		tell process "System Preferences"
			tell window 1
				click button "すべてを表示" of group 1 of group 2 of toolbar 1
				tell scroll area 1
					click button "一般"
				end tell
			end tell
			delay 1
			tell window 1
				#オフの場合
				if (value of checkbox "メニューバーを自動的に表示/非表示") = 0 then
					click checkbox "メニューバーを自動的に表示/非表示"
				end if
				
			end tell
		end tell
	end tell
end tell

最初に、システム環境設定を立ち上げて、activateさせる。続いて、"一般"に移動して、checkboxをクリックさせるが、その間には delay 1 を加えている。と言うのも、次の処理を遅らせないとマシンによっては動作しないことがある。

で、システム環境設定は同時に二枚でないので、window名を 1 として、click button "すべてを表示" of group 1 of group 2 of toolbar 1 を入れることで、万が一システム環境設定が立ち上がっていて、他のメニューに移動していても最初のメニュー画面に戻るので動くはず。