UI elementを丸ごと取得

_UNI3812

※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