AppleScript
AppleScriptでポップアップボタン(セレクトボックス)の内容を変える方法

はじめに

今回は、AppleScriptでポップアップボタン(セレクトボックス)の内容を変える方法

ポップアップボタンは2回クリックを行う必要がある

ポップアップボタン(htmlで言うセレクトボックス)の内容を変えるには、2回スクリプトでクリック動作を行う必要がある。また要素を調べる際もポップアップを一度クリックした状態で調べる必要がある。今回もシステム環境設定の「一般」を使用して、一般内にある強調表示色のポップアップボタンを変えてみる。

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

前回、「AppleScriptでcheckboxを操作する」で使った一般タブの UI element を調べるスクリプトを実行する。


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


そうすると、次の用な結果を得られ、pop up button "強調表示色:" と言うのがあるのが分かる。名前がついているので分かりやすい


{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 }

と言うことで、pop up button "強調表示色:" のUI element を調べるため、次のスクリプトを走らせる


tell application "System Events"
	tell process "System Preferences"
		tell window "一般"
			tell pop up button "強調表示色:"
				every UI element
			end tell
		end tell
	end tell
end tell

すると、空の結果が返ってきてしまう。これを防ぐには、一度ポップアップをクリックした状態で調べる必要がある。


{}

と言うことで、click pop up button "強調表示色:" を tell window "一般" のすぐ後に挿入する。


tell application "System Events"
	tell process "System Preferences"
		tell window "一般"
			click pop up button "強調表示色:"
			tell pop up button "強調表示色:"
				every UI element
			end tell
		end tell
	end tell
end tell

そうすると、次の結果を得られる。もう一つ下の階層があるのが分かるので、tell pop up button "強調表示色:" を tell menu 1 of pop up button "強調表示色:" としてもう一度調べてみる。その下の階層に選択肢の要素があることが分かる。


{menu item "ブルー" , menu item "パープル" , menu item "ピンク" , menu item "レッド" , menu item "オレンジ" , menu item "イエロー" , menu item "グリーン" , menu item "グラファイト" , menu item "その他" }

以上の要素の手がかりを元に、二つのクリックを走らせる。1・click pop up button "強調表示色:" 2・click menu item "変えたい色" of menu 1 of pop up button "強調表示色:" で実際に色を変える。


tell application "System Events"
	tell process "System Preferences"
		tell window "一般"
			click pop up button "強調表示色:"
			click menu item "ピンク" of menu 1 of pop up button "強調表示色:" 
		end tell
	end tell
end tell

これで強調表示色がピンクに変わっていればスクリプト成功。