Applescript iChat message font size problem

I’m writing a script that modifies my outgoing ichat messages. Everything works fine except for the fact that as soon as I return a new text value, the message is sent with a miniscule font. This occurs for me with even the most minimal example code. Any ideas?


using terms from application "iChat"
    on message sent theMessage for theChat
        return "test"
    end message sent
end using terms from

Note that to get this example to fire off, you have to go to save the applescript to a file, go to the alerts tab in ichat preferences, and assign the applescript file to the message sent event handler (new in Leopard).

I’m not familiar with this but in general the “message sent” handler sounds like it will run an applescript after a message is sent. If that’s the case then how do you propose to modify the message before it is sent when this handler does something after?

The handler runs before the message is sent. If a text value is returned, it replaces the value of the message. This works properly, and is listed in the iChat applescript dictionary. The only problem is that the font size becomes miniscule.

I ran into this exact same problem. I don’t know why the font is so small, but I found a workaround. You can cancel the original message by returning whitespace. When attempting to send whitespace, iChat just cancels the message altogether. This looks kind of silly in the script, since one would think to return a false or null to cancel the message.

Anyway, try this:


using terms from application "iChat"
    on message sent theMessage for theChat
        send "test" to theChat
        return " "
    end message sent
end using terms from

Sounds like a great workaround, but that code snippet seems to be an infinite loop. The " send “test” to theChat" line creates another event which is processed by the event handler each time around, so it just keeps creating events until it blows up the stack.

Ah, right. I didn’t run into that problem when I did something like this because it involved a condition that prevented such a situation. My case was different and didn’t require anything tricky, but a more general solution would be comparing the last previous message to the current one. You could try something like this (I haven’t tested this, but if it doesn’t work than some close variation should):


using terms from application "iChat"
	
	on message sent theMessage for theChat
		global lastMessage
		
		if theMessage is not the lastMessage then
			send theMessage to theChat
			set the lastMessage to theMessage
		end if
		
		return " "
	end message sent
	
end using terms from