我正在编写一个脚本,它会不断扫描iTunes以获取新的对话框,并根据他们的操作正确关闭它们。在大多数情况下,我的脚本正常工作,但不会搜索对话框的按钮。
到目前为止,脚本将一直等到另一个进程打开iTunes,然后等待第一个对话框出现(使用spin-wait循环)。一旦出现对话框,它就会出现窗口,然后是窗口的按钮。我希望它能根据按钮的内容做出决定,但是我很难找到按钮的内容。以下是整个脚本:
#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函数正是注释掉的代码段,只是一般化。注释掉的部分有效,这是我询问类型的主要原因。
首先,我该如何实现这样的功能?让我们说我真的希望这个功能起作用,我该怎么做呢?
其次,有更好的方法吗?我不是指整个脚本(虽然我不怀疑它可以做得更好),我的意思是在按钮上搜索我期待的特定按钮。
编辑:我注意到的另一件事是“标题”是脚本正文中的保留字,但是函数中的变量。我已经习惯了普遍保留保留字的其他语言,所以我也希望得到一些关于那里发生的事情的指导。
答案 0 :(得分:1)
我没有完成整个脚本,但这应该修复你的处理程序:
on windowContainsButton(listOfButtons, searchFor)
repeat with theButton in listOfButtons
tell application "System Events"
if title of theButton is searchFor then return true
end tell
end repeat
return false
end windowContainsButton
这有点清洁:
property theSeconds : 1
tell application "System Events"
repeat until window 1 of process "iTunes" exists
delay theSeconds
end repeat
tell process "iTunes"
set frontmost to true
repeat until exists (first UI element whose role description is "dialog")
delay theSeconds
end repeat
set wantedWindow to first UI element whose role description is "dialog"
tell wantedWindow
set buttonList to title of every button in wantedWindow
if (count of buttons) is 1 and title of button 1 is not "Stop" then
click button 1
else if buttonList contains "Cancel" then
say "Cancel"
end if
end tell
end tell
end tell