Why does this Applescript often fail the first time, but always work after that? The following Applescript opens a menubar app called QuickHue and then clicks a specified item in the menu.  Often, when I use the applescript, it does nothing.  But if I wait a moment or two and try again, it works perfectly.  And it keeps working.  But if some time passes and I trigger it again, it'll fail again the first time.  After that, it works like a champ.  Here's the applescript.  Any idea what's going on here?

ignoring application responses
    tell application "System Events" to tell process "QuickHue"
        click menu bar item 1 of menu bar 1
    end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "QuickHue"
    tell menu bar item 1 of menu bar 1
        click menu item 2 of menu 1
    end tell
end tell
You're right, I didn't test it well enough. The error was shown when System Events was not already running. As another workaround, I added launch application "System Events" and delay 0.2 to the start:

launch application "System Events"
delay 0.2
ignoring application responses
    tell application "System Events" to tell process "QuickHue"
        click menu bar item 1 of menu bar 1
    end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "QuickHue"
    tell menu bar item 1 of menu bar 1
        click menu item 2 of menu 1
    end tell
end tell


If it still doesn't work, try increasing the delays.