代码之家  ›  专栏  ›  技术社区  ›  Red5Seeker7

将AppleScript放入工作流时工作失败

  •  0
  • Red5Seeker7  · 技术社区  · 7 年前

    非常 AppleScript新手。

    我有一个非常简单的, 在Automator中作为工作流/应用程序的一部分运行时,总是失败的AppleScript。我知道脚本可以工作,因为我可以在脚本编辑器中多次运行它,并且没有错误。

    以下是脚本:

    activate application "Chess"
    
    tell application "System Events" to tell process "Chess"
        click menu item "Preferences…" of menu "Chess" of menu bar item "Chess" of menu bar 1
    
        tell window 1
                repeat until sheet 1 exists
                    delay 0.1
                end repeat
        end tell
    
        tell group "Speech" of sheet 1 of window 1
            set checkboxOne to checkbox "Allow Player to Speak Moves"
            set checkboxTwo to checkbox "Speak Computer Moves"
            set checkboxThree to checkbox "Speak Human Moves"
    
            tell checkboxOne
                set checkBoxStatus to value of checkboxOne as boolean
                if checkBoxStatus is true then click checkboxOne
            end tell
            tell checkboxTwo
                set checkBoxStatus to value of checkboxTwo as boolean
                if checkBoxStatus is true then click checkboxTwo
            end tell
            tell checkboxThree
                set checkBoxStatus to value of checkboxThree as boolean
                if checkBoxStatus is true then click checkboxThree
            end tell
        end tell
    
        click button "OK" of sheet 1 of window 1
    end tell
    

    System Events got an error: Can’t get sheet 1 of window 1 of process "Chess". Invalid index.
    

    突出显示的线为

    click button "OK" of sheet 1 of window 1
    

    有什么我遗漏的吗?为什么会发生此错误?我如何修复它?

    提前感谢您的帮助!

    2 回复  |  直到 7 年前
        1
  •  0
  •   wch1zpink    7 年前

    尝试将这部分代码延迟地包装在自己的tell语句中。这样地:

    tell application "System Events"
        delay .2
        click button "OK" of sheet 1 of window 1
    end tell
    
        2
  •  0
  •   unlocked2412    7 年前

    也许你可以试试这个代码。它将切换复选框状态。

    tell group "Speech" of sheet 1 of window 1
        tell checkbox 1
            perform action "AXPress"
        end tell
    end tell