Duplicate Firing of `active chat message received` handler

Well, for the first time in I don’t know how long, http://stackoverflow.com/questions/34800851/duplicate-firing-of-active-chat-message-received-handler seems to have failed me, so I’m hoping someone here is able to help me out.

I’m writing an AppleScript to execute a handler when Messages.app receives a message (under El Capitan). The script is being saved to ~/Library/Application\ Scripts/com.apple.iChat and is set in the Messages preferences to be the AppleScript handler.

When Messages is the frontmost application and a message is received, the active chat message received handler is fired twice. This doesn’t seem to be a problem when Messages is in the background (received messages then fire message received, and that handler only once). I know which handler is fired because the portion that deals with the handlers looks like this:


    using terms from application "Messages"
		
        on message received _msg from _sender for _chat with _text_desc
            if DEBUG then display dialog "message received"
            message_received(_sender)
        end message received
	
        on chat room message received _msg from _sender for _chat with _text_desc
            if DEBUG then display dialog "chat room message received"
            message_received(_sender)
        end chat room message received
	
        on active chat message received _msg from _sender for _chat with _text_desc
            if DEBUG then display dialog "active chat message received"
            message_received(_sender)
        end active chat message received

        -- More handlers below, mostly like the above or empty

    end using terms from

I set a DEBUG property to true and can see which handler gets fired.

I’ve tried working around this by writing a temporary file (using the UUID of the _sender). The message_received handler checks for the existence of the file and is supposed to do nothing if it’s present. But this hasn’t worked, even with random delays. I tried extending the length of the random delays, but this brings up errors about the AppleScript running for more than 10 seconds, even when enclosing the code within a with timeout of block.

Regardless of Apple’s apparent support for executing AppleScripts in response to Messages events, perhaps I should look at some other mechanism to support this request from the client. I’m open to ideas.

Model: MacBook Pro 17" Mid-2012
Browser: Safari 601.3.9
Operating System: Mac OS X (10.10)