UI elementを丸ごと取得2

do shell script の制限を回避するの方法を使って、
たまにエラーになるメニュー項目の多いアプリケーションにも対応させます。


しかし、これだと最後にアプリケーション名を渡せないんで無理矢理最後に前面にあるアプリケーションを拾ってくっつけてます。
なので、処理が終るまでアプリケーションを切り替えられません。。。

次回はこれを回避するお話。

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

script s1
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
end script
set a11 to do shell script “osascript  -e ‘run s1 of (load script alias \”” & (path to me) & “\”)’ | sed ‘s/of application process \”.[^\”]*\”//g;s/window \”.[^\”]*\”/window 1/g'” –script a1の呼び出し
tell application “System Events” to set a4 to name of application processes whose frontmost is true
try
tell application “AppleScript Editor” to (activate) & (check syntax of (make new document with properties {text:“tell application \”System Events\”\ntell application process \”” & a4 & “\”\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