log itunes songs played

Discussion in 'Mac Programming' started by nullx86, Dec 28, 2009.

  1. nullx86 macrumors 6502a

    nullx86

    Joined:
    Jun 26, 2009
    Location:
    Wilmington/Jacksonville, NC
    #1
    I know it can be done, because of Adium and Windows Live Messengers ability to report what song is playing, so my question, is there a way to have iTunes dump whatever is played to a text document? I want to do this to keep track of what songs I listen to over a radio stream. If anyone has any ideas, please post them, I have no clue how to make this happen. Thanks.
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    I don't have something ready-made, but it seems like AppleScript is the way to go. My suggestion would be to have an AppleScript that is always running when you are wanting this task performed. You can get the current track's artist and title, store this in a property, then sleep some amount of time. I would say the length of the shortest possible song minus 15 seconds, etc. Then wake up and check again. If the track is different, record the saved values to your file. If not, sleep again.

    I am very unskilled at AppleScript, but I think this could be done in a dozen lines or so.

    -Lee
     
  3. nullx86 thread starter macrumors 6502a

    nullx86

    Joined:
    Jun 26, 2009
    Location:
    Wilmington/Jacksonville, NC
    #3

    yeah exactly what i want, i just know jack about how to do it
     
  4. seepel macrumors 6502

    seepel

    Joined:
    Dec 22, 2009
    #4
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    Code:
    set saveTrack to ""
    repeat
    	tell application "iTunes"
    		set trackName to artist of current track & " - " & name of current track
    	end tell
    	if trackName is not equal to saveTrack then
    		if saveTrack is not equal to "" then
    			do shell script "echo \"" & saveTrack & "\" >>~/SongList.txt"
    		end if
    		set saveTrack to trackName
    	end if
    	do shell script "sleep 5"
    end repeat
    
    13. That's about a dozen.

    Code:
    set saveTrack to ""
    repeat
    	tell application "iTunes"
    		set trackName to artist of current track & " - " & name of current track
    	end tell
    	if trackName is not equal to saveTrack then
    		if saveTrack is not equal to "" then do shell script "echo \"" & saveTrack & "\" >>~/SongList.txt"
    		set saveTrack to trackName
    	end if
    	do shell script "sleep 5"
    end repeat
    
    Down to 11! a little more difficult to extend in the future, though.

    The things you might want to tweak:
    What is actually getting set for trackname. Now it's artist - title, but you can get any of these things that are a property of a track:
    Otherwise, the sleep of 5 is way low, i did that for testing. 1 minute seems to be reasonable, but you'll need to tweak this to the "right" value for you.

    Also, i have it logging in my home directory to SongList.txt. You can tweak this in the "do shell script" line.

    -Lee
     
  6. JoshDC macrumors regular

    Joined:
    Apr 8, 2009
    #6
    iTunes posts a notification with player status changes as of version 4.7, so if you've got Xcode tools installed you can make an cocoa application to do this for you (without having to poll).

    Using the standard application template you need to add to the ApplicationDelegate this:

    Code:
    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    	NSDistributedNotificationCenter *notificationCenter = [NSDistributedNotificationCenter defaultCenter];
    	
    	[notificationCenter addObserver:self
    			selector:@selector(log:)
    				name:@"com.apple.iTunes.playerInfo"
    			  object:nil];
    	
    	[NSApp hide:nil];
    }
    
    Which simply registers the application to be given status updates, and this:

    Code:
    - (void)log:(id)sender
    {
    	NSAppleScript *script = [[NSAppleScript alloc] initWithSource:@"tell application \"iTunes\" \n\
    							 if player state is playing then \n\
    							 set trackName to name of current track\n\
    							 set trackArtist to artist of current track\n\
    							 end if\n\
    							 end tell\n\
    							 \n\
    							 return trackName & \" – \" & trackArtist "];
    	
    	NSAppleEventDescriptor *eventDesc = [script executeAndReturnError:nil];
    		
    	NSString *filePath = NSHomeDirectory();
    	filePath = [filePath stringByAppendingPathComponent:@"songLog.txt"];
    	
    	NSFileManager *dfm = [NSFileManager defaultManager];
    	
    	if (![dfm fileExistsAtPath:filePath]) {
    		[@"" writeToFile:filePath
    			  atomically:NO
    				encoding:NSUTF8StringEncoding
    				   error:nil];
    	}
    	
    	NSString *trackname = [eventDesc stringValue];
    	
    	if (trackname) {
    		trackname = [trackname stringByAppendingString:@"\n"];
    		NSFileHandle *newHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
    		[newHandle seekToEndOfFile];
    		[newHandle writeData:[trackname dataUsingEncoding:NSUTF8StringEncoding]];
    	}	
    
    	[script release];
    }
    
    Which queries iTunes for it's currently playing song (as per previously discussed) and writes it to the end of the file "songLog.txt" in your home directory.

    You can then add this application to your login items to have it run when you log in.

    EDIT: attaching a zip for the application if you can't be bothered with all that...
     

    Attached Files:

  7. nullx86 thread starter macrumors 6502a

    nullx86

    Joined:
    Jun 26, 2009
    Location:
    Wilmington/Jacksonville, NC
    #7
    thanks for the zip, i just tried that, il try to compile something in xcode later, im a total noob with this...

    uhh, the zipped app you attached. the code said it would save in home, i checked, and sure enough, the file is there, but it isnt getting stream vaules correctly. Also, the stream connects are duplicated. Any ideas on this?

    Code:
    Distortion Radio – missing value
    Distortion Radio – missing value
    THE EDGE 103.9 – missing value
    THE EDGE 103.9 – missing value
    Rescue Me – Buckcherry
    THE EDGE 103.9 – missing value
    THE EDGE 103.9 – missing value
    
    Rescue Me was a local file on my iMac, just as a test. The two images below show that the song title is shown, and the radio title is shown as well. I tried this on Distortion Radio as well, which only has a static display of the song/artist name. Thanks guys.
     

    Attached Files:

  8. JoshDC macrumors regular

    Joined:
    Apr 8, 2009
    #8
    Not sure about the duplicate entries, but the 'missing value' issue is because the radio station doesn't provide a value iTunes interprets as an artist. You'll need to find out what value the radiostation uses as it's description ("KILLERS - SOMEBODY TOLD ME" on the screen shot you posted) and modify the code appropriately. I can't find what this value is, lee posted all the possible ones, so it could be that you can't get this information through applescript.
     
  9. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #9
    Another (possible) problem here is that the stream may show up as a single "track". That's to say, song to song, DJ to commercials, etc. it may not "change tracks". Hopefully if you can find the "banner message" they send, that's enough to tell when things change. What field those things in, though, is a mystery. I'd try description and comment next to see if that's where itunes is sticking that extra info.

    -Lee
     

Share This Page