1

I need get info about track (song name, artist, album) when user changed player (iTunes) state: next track, last track, play and stop.

I'm writed something, but it's doesn't work with action (play, stop, etc..)

repeat delay 1 tell application "iTunes" if player state is stop then set myAlbumiTunes to {get name of current track, get artist of current track, get album of current track} log myAlbumiTunes end if end tell end repeat 

1 Answer 1

3

The easiest way to do this is if you know how to write a cocoa application because every time something changes in iTunes it send out a system wide notification. A coca app can receive that notification and take an action.

Since you want an applescript method then you must do it the hard way. You'll need some variables to keep track of the current song and player state. Every time through your repeat loop check the current song against the song and player state you have stored in your variables. If something changes then you can take an action.

I didn't test this but it shows the idea. I hope it helps. Good luck.

property savedSongName : missing value property savedPlayerState : missing value repeat delay 1 set somethingChanged to false tell application "iTunes" set currentTrack to current track set currentSongName to name of currentTrack set currentPlayerState to player state if currentPlayerState is not savedPlayerState then set somethingChanged to true else if currentSongName is not savedSongName then set somethingChanged to true end if if somethingChanged then set myAlbumiTunes to {get name of currentTrack, get artist of currentTrack, get album of currentTrack} log myAlbumiTunes end if set savedPlayerState to currentPlayerState set savedSongName to currentSongName end tell end repeat 

EDIT: if you do write the objective-c program, just register for the notification as follows.

[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(receivediTunesNotification:) name:@"com.apple.iTunes.playerInfo" object:nil]; 

Then create a handler "-(void)receivediTunesNotification:(id)notif". That will be called whenever something changes. You can get a dictionary from the notification that gives the change information as follows.

NSDictionary* itunesDict = [notif valueForKey:@"userInfo"]; 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much. Yap, I'm writing my own Obj-C application (cocoa) and I need getting track info every time, when it changes in iTunes. I try find answer for this, but I can't.. If you know solution for this task on Obj-c - it'll very well for me and other!
See the EDIT section of my post. I added the details for the objective-c method.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.