私は、iTunesを常にスキャンして新しいダイアログボックスを探し、その動作に応じて正しく閉じるスクリプトを書いています。ダイアログボックスのボタンを検索しないことを除いて、ほとんどの場合、私のスクリプトは正しく機能しています。
これまでのところ、スクリプトは別のプロセスがiTunesを開くまで待機し、最初のダイアログボックスが表示されるのを待ちます(スピン待機ループを使用)。ダイアログボックスが表示されると、ウィンドウが表示され、次にウィンドウのボタンが表示されます。その後、ボタンが何であるかを判断してもらいたいのですが、ボタンが何であるかを知るのに苦労しています。以下はスクリプト全体です。
#repeat
set windowOpen to false
tell application "System Events"
repeat until windowOpen
if window 1 of process "iTunes" exists then
set windowOpen to true
end if
end repeat
set windowOpen to false
tell process "iTunes"
set frontmost to true
set wantedWindow to null
repeat until windowOpen
try
set wantedWindow to first UI element whose role description is "dialog"
set windowOpen to true
on error erMessg
set windowOpen to false
end try
end repeat
set buttonList to every button in wantedWindow
if (count of buttonList) is 1 then
if title of item 1 of buttonList is not "Stop" then
click item 1 of buttonList
end if
else
if my windowContainsButton(buttonList, "Cancel") then
say "Cancel"
end if
# repeat with theButton in buttonList
# if title of theButton is "Cancel" or title of theButton is "Restore" then
# say "cancel"
# delay 1
# end if
# end repeat
end if
end tell
set wantedWindow to null
end tell
#end repeat
on windowContainsButton(listOfButtons, searchFor)
repeat with theButton in listOfButtons
if title of theButton is searchFor then
return true
end if
end repeat
return false
end windowContainsButton
これまでのところ、「キャンセル」と表示してキャンセルボタンが見つかったかどうかを調べています。代わりに、エラーが発生します。System Events got an error: Can't make |title| of button "Cancel" of window 1 of application process "iTunes" into type reference.
次に、次の行の関数windowContainsButtonをポイントします。
if title of theButton is searchFor then
searchForが強調表示されます。
windowContainsButton関数は、コードのコメントアウトされたセクションであり、一般化されています。コメントアウトされたセクションは機能します。これは、タイプについて質問する理由の大部分です。
まず、このような関数を実装するにはどうすればよいですか?私が実際にこの関数を機能させたかったとしましょう、どうすればそれを作ることができますか?
第二に、これを行うためのより良い方法はありますか?スクリプト全体を意味するのではなく(スクリプトがより適切に実行されることは間違いありませんが)、期待している特定のボタンをボタンで検索することを意味します。
編集:私が気付いたもう1つのことは、「タイトル」はスクリプトの本文では予約語ですが、関数では変数であるということです。私は予約語が普遍的に予約されている他の言語に慣れているので、そこで何が起こっているのかについてのガイダンスも欲しいです。