UI elementを丸ごと取得

※10月17日更新

タイトル通り、指定したアプリケーションのUI elementを取得します。
要素が多いメニュー項目などはそれなりに時間がかかります。

このスクリプトだけで、今まで面倒だったUI elementの取得が一気にできるんだから、
多少時間がかかるのは、まーいいでしょう。

http://d.hatena.ne.jp/zariganitosh/
色々アイディアをいただきました。ありがとうございます。

スクリプトエディタで開く

※Safari上のpop up buttonはmenu item のUI elementがないので内容が取得できません。
※pop up buttonをクリックしたあとに、下矢印など、キーストロークで選択するしかないようです。

基本部分は5行目。

取得から整形までをやってます。
ここだけで一応OKですが、他の部分で最終的な見やすい形にしています。

do shell script 内にosascriptを使い取得部分を入れているのは、do shell scriptの制限対策。
あと、こうすると取得した値がテキストで返ってくるので、あとの処理が楽だったりします。

tell application “System Events”
set {o4, a1, a11} to {o1(name of application processes whose background only is false) of me, “”, “”}
tell application process (o4’s item 2) to set frontmost to true
end tell
set a2 to (do shell script “osascript -s s -e ‘tell application \”System Events\” \n tell application process \”” & o4’s item 2 & “\”\n tell ” & o4’s item 1 & ” to UI elements & UI elements of UI elements & UI elements of UI elements of UI elements & UI elements of UI elements of UI elements of UI elements & UI elements of UI elements of UI elements of UI elements of UI elements & UI elements of UI elements of UI elements of UI elements of UI elements of UI elements & UI elements of UI elements of UI elements of UI elements of UI elements of UI elements of UI elements & UI elements of UI elements of UI elements of UI elements of UI elements of UI elements of UI elements of UI elements & UI elements of UI elements of UI elements of UI elements of UI elements of UI elements of UI elements of UI elements of UI elements & UI elements of UI elements of UI elements of UI elements of UI elements of UI elements of UI elements of UI elements of UI elements of UI elements & UI elements of UI elements of UI elements of UI elements of UI elements of UI elements of UI elements of UI elements of UI elements of UI elements of UI elements & UI elements of UI elements of UI elements of UI elements of UI elements of UI elements of UI elements of UI elements of UI elements of UI elements of UI elements of UI elements \nend tell\nend tell’| perl -pe ‘s/of application \”System Events\”/\n/g’ | grep -E ‘(list|menu|value indicator|pop up button|button|radio button|checkbox|text field|slider)’ | perl -pe ‘s/[{},]//g'”)
if not o4’s item 1 = “menu bar 1” then
repeat with a9 in paragraphs of a2
try
if “pop up button” is in a9 then
try
run script (“tell application \”System Events\”\nset a1 to name of UI elements of menu 1 of (click ” & a9 & “)\nclick (menu items of menu 1 of ” & a9 & ” whose selected is true)\nreturn a1\nend\nend” as Unicode text)
repeat with b1 in result
set a1 to a1 & b1 & “,”
end repeat
end try
tell application “System Events” to set {a11, a1} to {a11 & “click menu item \”hoge\” of menu 1 of ” & a9 & “–>hoge = ” & a1 & return as Unicode text, “”}
else if “button” is in a9 or “checkbox” is in a9 or “radio button” is in a9 then
set a10 to run script (“tell application \”System Events\” to properties of ” & a9 as Unicode text)
tell application “System Events” to set a11 to a11 & “click ” & a9 & “–>” & role description of a10 & “,” & value of a10 & “,” & accessibility description of a10 & return as Unicode text
else if “value indicator” is in a9 or “text field” is in a9 or “list” is in a9 or “slider” is in a9 then
set a10 to run script (“tell application \”System Events\” to properties of ” & a9 as Unicode text)
tell application “System Events” to set a11 to a11 & “set value of ” & a9 & “to \”” & value of a10 & “\”–>” & role description of a10 & “,” & accessibility description of a10 & return as Unicode text
end if
end try
end repeat
else
set a11 to a2
end if
set a11 to do shell script “echo ‘” & a11 & “‘ | sed ‘s/of application process \”” & o4’s item 2 & “\”//g'”
try
tell application “AppleScript Editor” to (activate) & (check syntax of (make new document with properties {text:”tell application \”System Events\”\ntell application process \”” & o4’s item 2 & “\”\n ” & a11 & “\nend\nend”}))
end try

on o1(a4)
tell application “Finder”
activate
return {item 1 of (choose from list {“menu bar 1”, “window 1”, “every window”} with prompt “取得したいUI elementを選択” with title “uni-factory”), item 1 of (choose from list a4 with prompt “取得するアプリケーションを選択” with title “uni-factory”)}
end tell
end o1

5件のコメント

  1. さらに完成度が上がりましたね。
    UI別に操作するアクションも付加され、そのまま使えていい感じです。

    ただ一つ、pop up buttonの処理をする部分だけ、コードを読んでもよくわかりませんでした。
    tryブロックなっていますが、こちらの環境ではpop up buttonの処理は毎回エラーになるらしく、pop up buttonについてはごっそり消えてしまいます。
    (ゆうちょダイレクトの中で、入出金明細を見るページです。)

    それにしてもこのスクリプトは便利ですね。
    スクリプトメニューのUI Element Scriptsに標準で含まれていて欲しいくらいです。

    使っていて感じたのは、UIごとに長くなりがちなコードが、少しでも短くれきれば良いかなと思いました。
    具体的には、以下のようなコードで出力されると、さらに簡潔で素晴らしいかと。

    tell application “System Events” to tell process “Safari” to tell window “ゆうちょダイレクト | ログイン(お客さま番号入力)”
    click button 1 –>閉じるボタン,missing value,missing value
    click button 2 –>拡大/縮小ボタン,missing value,missing value
    click button 3 –>しまうボタン,missing value,missing value
    …中略…
    end

    重複する部分をtellブロックに含めるバージョンです。
    自分でやれって感じですよね。(笑)
    そのうちチャレンジしてみます。

  2. zariganitosh さん。コメントありがとうございます。

    pop up button 部分ですが、ここのUI element 取得は困ったことに
    クリックしてメニューが表示されている状態じゃないと取得できないという、困った仕様なんです。

    click pop up button 1–pop up buttonを開く
    UI elements of menu 1 of pop up button 1–pop up button内の要素取得。メニューを開いていないとこれが取得できない。。。。

    そして、次の作業に入るためには、もう一度pop up buttonをクリックしてメニューを閉じなくてはいけない。。

    なので
    1:pop up buttonをクリック
    2:要素取得
    3:クリックする時に選択されていたメニュー項目をもう一度クリック。
    という作業をやってまいます。

    エラーになるのは、スクリプトメニューから操作しているからかもしれません。
    アプリケーション形式で実行すると大丈夫だと思います。

    こちらでも、ダメだった時があったのですが。。どのような時にエラーがでていたか忘れてしまって。。

    最終出力の簡素化は考え中です。私も長いよなーと思っていました。

    >スクリプトメニューのUI Element Scriptsに標準で含まれていて欲しいくらいです。
    ありがとうございます!
    というか、もっと簡単に取得できればいいんですけどね。UI element。。。。。

  3. おはようございます。

    > pop up button 部分ですが、ここのUI element 取得は困ったことに
    > クリックしてメニューが表示されている状態じゃないと取得できないという、困った仕様なんです。

    なるほど、そうでしたか。
    その後、以下のページの血液型を自由に選択することを目指していろいろ試してみました。(アプレットにして実行したり、Automatorで操作したり)
    http://www.htmq.com/html/select.shtml
    しかし自分の環境(Safari 5.1)では、pop up menu をクリックすることはできますが、その選択項目を直接変更する方法は分かりませんでした。
    (キーボードで操作すれば変更はできます。)

    pop up menuは未解決ですが、その他のUIがこれだけ一気に正確に取得できると、やはり便利ですね。
    素敵なツールをありがとうございます。

  4. zariganitoshさん。こんばんは。

    Safari上では、pop up button内の項目にUI elementが定義されていないようです。。
    あまり多くのページで検証していませんが、仕様みたいですね。
    なので、スマートではありませんが、こんな感じでしょうか。例によってgroup 3などが環境によって変わるかもしれません。

    tell application “System Events”
    tell application process “Safari”
    click pop up button 1 of group 26 of UI element 1 of scroll area 1 of group 1 of group 1 of group 3 of window 1
    key code 125
    keystroke return
    click static text “みずがめ座” of list 1 of group 27 of UI element 1 of scroll area 1 of group 1 of group 1 of group 3 of window 1
    end tell
    end tell

    pop up button部分はプリントダイアログなどで検証していたので、気づきませんでした。

コメントはできません。