iTunes script- How long have I been listening?

Discussion in 'Mac Help/Tips' started by G4scott, Jun 24, 2003.

  1. G4scott macrumors 68020

    G4scott

    Joined:
    Jan 9, 2002
    Location:
    Austin, TX
    #1
    Is there an apple script anywhere out there that gets the play counts of all your songs, and figures out how long you've been listening to music?

    It doesn't seem like it'd be too hard, and I'd be interested to see how many days of listening time I have...

    Just a thought...
     
  2. scan300 macrumors 6502

    Joined:
    Mar 25, 2003
    Location:
    Melbourne, Australia
    #2
    Here's a script I've written:

    Code:
    tell application "Finder"
    	set myLocation to (((path to desktop folder) as text) & "iTunes_PlayCount.html")
    	if file myLocation exists then
    		delete file myLocation
    	end if
    	set myFileRef to (open for access file myLocation with write permission)
    	
    	
    	write "<HTML><HEAD><TITLE>my Play Counts </TITLE></HEAD><BODY>" & return to myFileRef
    	write "<p>My playcounts for all my songs...</p>" & return to myFileRef
    	write "<TABLE width=500 border=1>" & return to myFileRef
    	
    	
    	tell application "iTunes"
    		set myPlaylist to every playlist
    		set x to the first item of myPlaylist
    		set myTrackList to every track of x
    		repeat with y in myTrackList
    			set myTrackName to name of y
    			set myPlayedCount to played count of y
    			tell application "Finder"
    				write "<tr><td>" & myTrackName & "</td>" & "<td width=15%>" & myPlayedCount & "</td></tr>" & return to myFileRef
    			end tell
    		end repeat
    	end tell
    	
    	write "</TABLE>" & return to myFileRef
    	write "</BODY></HTML>" & return to myFileRef
    	close access myFileRef
    	
    	display dialog ("Finished Summary")	
    end tell
    
    Copy this code into your applescript editor and click the check syntax button, which will format the script and inform you of errors. Then save it as an application.

    This script will list all of the tracks in your first library item in iTunes which hopefully is the default library which lists all of your songs. It will then output the song name and the played count to a table and save it as a html file on your desktop. It's not the fastest of scripts so be patient when it runs.

    Your second request of logging how long you listen to music is trickier. You can write a logging script which logs when itunes is launched and when it's quit and calculate the hours in between. To use appplescript to log how much time is spent playing tracks is much more complicated.

    My apologies for the page width being altered because of the code.
     
  3. G4scott thread starter macrumors 68020

    G4scott

    Joined:
    Jan 9, 2002
    Location:
    Austin, TX
    #3
    OK, I made a little change to your script, to display the track time of each song,
    and just show the songs that have been played at least once.

    Code:
    tell application "Finder"
    	set myLocation to (((path to desktop folder) as text) & "iTunes_PlayCount.html")
    	if file myLocation exists then
    		delete file myLocation
    	end if
    	set myFileRef to (open for access file myLocation with write permission)
    	
    	
    	write "<HTML><HEAD><TITLE>my Play Counts </TITLE></HEAD><BODY>" & return to myFileRef
    	write "<p>My playcounts for all my songs...</p>" & return to myFileRef
    	write "<TABLE width=600 border=1>" & return to myFileRef
    	
    	
    	tell application "iTunes"
    		set myPlaylist to every playlist
    		set x to the first item of myPlaylist
    		set myTrackList to every track of x
    		repeat with y in myTrackList
    			set myTrackName to name of y
    			set myTrackTime to time of y
    			set myPlayedCount to played count of y
    			if myPlayedCount > 0 then
    				tell application "Finder"
    					write "<tr><td>" & myTrackName & "</td>" & "<td width=15%>" & myPlayedCount & "</td>" & "<td width=15%>" & myTrackTime & "</td></tr>" & return to myFileRef
    				end tell
    			end if
    		end repeat
    	end tell
    	
    	write "</TABLE>" & return to myFileRef
    	write "</BODY></HTML>" & return to myFileRef
    	close access myFileRef
    	
    	display dialog ("Finished Summary")
    end tell

    Now, I'm not quite sure how to do this, but is there a way I can multiply the
    track times by their corresponding play counts, and add them all together for a
    total play time?

    I'm not quite sure how to work with time formats in Apple Script...
     
  4. G4scott thread starter macrumors 68020

    G4scott

    Joined:
    Jan 9, 2002
    Location:
    Austin, TX
    #4
    YES!!! I have figured it out!!! MWAHAHAHA

    Instead of using the time, I used the finish, which shows how many seconds until the finish of the song...

    So far, I'm only able to give a total in seconds, but with a calculator, you can do the math. I've played music for a total of:

    16 days, 17 hours, 40 minutes, and 50 seconds.

    Now, I need to find a way to get this script to get data from an iPod...

    here's the code:

    Code:
    tell application "Finder"
    	set myLocation to (((path to desktop folder) as text) & "iTunes_PlayCount.html")
    	if file myLocation exists then
    		delete file myLocation
    	end if
    	set myFileRef to (open for access file myLocation with write permission)
    	
    	
    	write "<HTML><HEAD><TITLE>my Play Counts </TITLE></HEAD><BODY>" & return to myFileRef
    	write "<p>My playcounts for all my songs...</p>" & return to myFileRef
    	write "<TABLE width=800 border=1>" & return to myFileRef
    	
    	
    	tell application "iTunes"
    		set myPlaylist to every playlist
    		set x to the first item of myPlaylist
    		set myTrackList to every track of x
    		set myTotalTime to 0
    		repeat with y in myTrackList
    			set myTrackName to name of y
    			set myTrackTime to finish of y
    			set myTrackTimeText to time of y
    			set myPlayedCount to played count of y
    			if myPlayedCount > 0 then
    				tell application "Finder"
    					write "<tr><td>" & myTrackName & "</td>" & "<td width=15%>" & myPlayedCount & "</td>" & "<td width=15%>" & myTrackTime & "<td width=15%>" & myTrackTimeText & "</td>" & "</td></tr>" & return to myFileRef
    				end tell
    				set myPlayedTime to myTrackTime * myPlayedCount
    				set myTotalTime to myTotalTime + myPlayedTime
    			end if
    		end repeat
    	end tell
    	
    	write "</TABLE>" & return to myFileRef
    	write "<p>" & "Total Time Played:  " & myTotalTime & " seconds" & return to myFileRef
    	write "</BODY></HTML>" & return to myFileRef
    	close access myFileRef
    	
    	display dialog ("Finished Summary")
    end tell
    Have fun!
     
  5. scan300 macrumors 6502

    Joined:
    Mar 25, 2003
    Location:
    Melbourne, Australia
    #5
    Well the iTunes dictionary does have the class "device playlist", so I assume you should be able to adjust the script from just looking at the iTunes playlist to looking at the iPod playlist.

    I don't have an iPod so I can't help you with the script.

    Good luck.
     

Share This Page