Search code examples
eventsapplescriptitunes

AppleScript event listening


I'm wanting to make a AppleScript to give me the current track name playing on itunes. So far I have made it get the name and refresh after the song has played with duration of the song.

Delay Song_duration

What I'm getting a problem is if the user skips the song manually or changes the song; I can see the are events called "next track" and "previous track" but it seems you can't do stuff like:

if (next track) then

because it does the event in the brackets. What I'm wanting to know is if there is a way to listen for the event. I don't know much about AppleScript, just been looking online for answers.

The general gist of the script is I want to get the name of the track and artist instantly with as little CPU/Memory as possible or I would just do delay 1.


Solution

  • Basically you can't do it with applescript.

    iTunes sends out a system wide notification whenever it's state changes called "com.apple.itunes.playerInfo". So you need to write in a language that can receive system notifications, objective-c for example. In that language you register for system notifications under the NSDistributedNotificationCenter. Once registered your program would run in the background, using little to no system resources, and then whenever something changes in iTunes your program would receive a notification and then you do something with iTunes.

    Here's a link to a similar question and a couple solutions are posted.