LoginSignup
19
22

More than 5 years have passed since last update.

AppleScriptでUI Elementsをリストで取得して使う方法

Posted at

System EventsでUI Elementsを取得する方法はいろんなページに載っていたけど、みんな各所各所で every UI elementsを使ってターミナルに一覧を表示して求める方法ばかり。
パーツがimageで毎回変わるような時は固定値だとスクリプトが動かなくなる。
プログラムでevery UI elementsで表示された内容を取得して、その中の値を使う方法がやっとわかったのでメモ。

  1. System Eventsを使って操作する基本的な方法
  2. every UI elementの内容をリストで取得して使う方法

System Eventsを使って操作する基本的な方法

例えば、システム環境設定で、 一般をスクリプトで開きたい場合

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

とやると

{window "システム環境設定" of application process "System Preferences" of application "System Events", menu bar 1 of application process "System Preferences" of application "System Events"}

と結果が表示され、Window名が「 window "システム環境設定" 」だとわかるので、「 tell window "システム環境設定" 」を追記してから、また「 every UI elements 」を書いてまた一覧を表示。

tell application "System Events"
    tell process "System Preferences"
        tell window "システム環境設定"
        every UI element
        end tell
    end tell
end tell
{button 1 of window "システム環境設定" of application process "System Preferences" of application "System Events", button 2 of window "システム環境設定" of application process "System Preferences" of application "System Events", button 3 of window "システム環境設定" of application process "System Preferences" of application "System Events", scroll area 1 of window "システム環境設定" of application process "System Preferences" of application "System Events", 
・・・・

tell scroll area 1 」を追記して、また「 every UI elements 」。

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
{button "一般" of scroll area 1 of window "システム環境設定" of application process "System Preferences" of application "System Events", button "デスクトップと
スクリーンセーバ" of scroll area 1 of window "システム環境設定" of application process "System Preferences" of application "System Events",
・・・

システム環境にあるアイコンの名前がずらっと表示され 「 button "一般" 」だとわかるので、「 click button "一般" 」を追加することでスクリプトで 一般 を開く事ができる。

tell application "System Events"
    tell process "System Preferences"
        tell window "システム環境設定"
            tell scroll area 1
                click button "一般"
            end tell
        end tell
    end tell
end tell

ofを付ける事で短く書く事もできる。(下記は同じ挙動)

tell application "System Events"
    tell process "System Preferences"
        click button "一般" of scroll area 1 of window "システム環境設定"
    end tell
end tell

基本的にはこんな感じで各所各所で「 every UI elements 」を使ってUI Elementsを取得する。

every UI elementの内容をリストで取得して使う方法

途中に「 name of 」を挟む事でパーツの名前リストを得る事ができる。

tell application "System Events"
    tell process "System Preferences"
        tell window "システム環境設定"
            tell scroll area 1
                set scroll_List to name of every UI element
            end tell
        end tell
    end tell
end tell
{"一般", "デスクトップと
スクリーンセーバ", "Dock", "Mission
Control", "言語と地域", "セキュリティと
プライバシー", "Spotlight", "通知", "CD と DVD", "ディスプレイ", "省エネルギー", "キーボード", "マウス", "トラックパッド", "プリンタと
スキャナ", 
・・・

上記の例の場合、リストとして scroll_Listに入るので、 to item 1で呼び出せる

tell application "System Events"
    tell process "System Preferences"
        tell window "システム環境設定"
            tell scroll area 1
                set scroll_List to name of every UI element
                set button_name to item 1 of scroll_List
                click button button_name
            end tell
        end tell
    end tell
end tell
19
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
22