RedVodkaJelly Logo

Getting data from iTunes on a Mac

POSTED AT 02:58 on 17th December 2006

Recently I was looking into how to get the currently playing track and track details from iTunes into a PHP script so that I could update a database and have it displayed on my blog (as you can see under the “Currently Listening To” header. The quick and easy solution that I can up with was to run a simple applescript command on the command line and use the PHP exec() function to capture the output. I had originally written a stand alone applescript but had trouble parsing out any quotes that were preventing it from being saved to a file.

This is only a quick and dirty hack, but works like a charm for what I was doing.

The Applescript
‘tell app “iTunes” to get the name of current track’

Running Applescript on the terminal
osascript -e ‘tell app “iTunes” to get the name of current track’

Mixing it with the PHP
exec(”osascript -e ‘tell app \”iTunes\” to get the name of current track’”, $arrOutput);


And then you just have to parse the output as you wish. To get other details about the current track simply change the detail of what you want in the applescript - “…get the artist of…”, “…get the album of…”, “…get the time of…”, “…get the podcast of…” etc.


Leave a Comment