Itunes track names / metadata to serial port

Pretty much what I’m trying to do it send the currently playing iTunes track to an arduino via a serial port (with SerialPort X). I had a few questions about applescript.

  1. is it possible to open a dialog and run code in the background? I’d like to open a dialog that says “running” and you can click a button called something like “stop” and it will stop running through a loop.
    kinda like:
display dialog "sending track data" buttons {"stop"}
repeat until --[stop pressed]
	[code ]
end repeat

I need to do this to play nice with the serial port (close it when I’m done); otherwise I have to quit applescript editor. I’m not sure how this will work out when I export it as an application.

  1. is there a way for the script to only update when the current song changes? i.e. get data for the current track, send it, then wait / delay until the track changes, then send the data again? I could do this with a loop in the applescript I guess, but is there a way iTunes can tell the applescript the song has changed?


Also, is there a way to make a script respond to a quit command while in an infinite loop (most of which is a delay)?

I exported my script as a standalone app, but I can’t quit it, only force quit, which is rather annoying. I’ve tried using a quit handler, but it still doesn’t quit.