1
votes

I've got a script than on login, ask a user if they want to watch a movie. For the most part it works great. However on occasion and for reasons unknown to me I get an AppleEvent handler failed error. I've read other post on this error but they all seem to be unique. So, if possible can someone please take a look at my script and tell me why this occasionally pops up and if there's anything i can do to prevent it?

One thing that might help to know, is the one thing in the script that fails when this error occurs is the movie doesn't play. It opens in quicktime but doesn't start.

Thanks in advance, here's the script.

tell application "Welcome" to activate
set question to display dialog "Would you like a welcome video?" buttons {"No, I've seen it", "Yes, please"} default button 2
set answer to button returned of question
if answer is equal to "Yes, please" then tell application "QuickTime Player"
    set theMovie to "Macintosh HD:Library:Desktop Pictures:Mac ML Opening Chalkbaord Video.mov"
    set openMovie to open theMovie
    present openMovie
    play openMovie
    delay 30
    quit
end tell
if answer is equal to "No, I've seen it" then tell application "Welcome"
    quit
    tell application "System Events"
        delete login item "Welcome"
    end tell
end tell
1
What are the occasions? Some context can go a long way. Be prepared to edit your question as you gain more insight into the context and conditions. - New Alexandria

1 Answers

2
votes

My guess is that you probably need a delay between opening and playing the movie. Sometimes the code runs faster than the computer can react. If that's the case then the movie may still be trying to open when the code tells the movie to play... thus the error. As such I added 2 repeat loops which checks for things to make sure they're available before proceeding to the next step in the code. You also need "open file" in the code instead of just "open".

Your approach in your if statements of telling an application to do something is unusual. I wouldn't do that. I would also combine your if statements into one if/else if statement. Anyway, here's how I would write your code (I'm assuming application "Welcome" is the code itself). I hope this helps!

set theMovie to "Macintosh HD:Library:Desktop Pictures:Mac ML Opening Chalkbaord Video.mov"

tell me to activate
set question to display dialog "Would you like a welcome video?" buttons {"No, I've seen it", "Yes, please"} default button 2
set answer to button returned of question

if answer is equal to "Yes, please" then
    tell application "QuickTime Player"
        activate
        set openMovie to open file theMovie

        -- delay until the movie opens
        set startTime to current date
        repeat until exists document 1
            delay 0.2
            if (current date) - startTime is greater than 10 then return -- a precaution so you don't get stuck in the repeat loop forever
        end repeat

        present openMovie
        play openMovie

        -- delay until the movie stops playing
        repeat until document 1 is not playing
            delay 1
        end repeat

        quit
    end tell
else if answer is equal to "No, I've seen it" then
    tell application "System Events" to delete login item "Welcome"
end if