0

我正在编写一个脚本,它将不断扫描 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 函数正是被注释掉的代码部分,只是概括性的。注释掉的部分有效,这是我询问类型的很大一部分原因。

首先,我该如何实现这样的功能?可以说我实际上希望这个功能能够工作,我怎么能做到呢?

其次,有没有更好的方法来做到这一点?我不是指整个脚本(尽管我不怀疑它可以做得更好),我的意思是在按钮中搜索我期望的特定按钮。

编辑:我注意到的另一件事是“标题”是脚本正文中的保留字,但函数中的变量。我习惯了其他语言,其中保留字被普遍保留,所以我也想要一些关于那里发生的事情的指导。

4

1 回答 1

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
于 2012-06-19T19:50:33.480 回答