Log your played iTunes tracks

A few days ago I was looking for a way to log all tracks that I listen to in iTunes. Something like Last.fm but that works local and for macOS. I did a quick search and couldn’t find anything (maybe there is something already). So I decided to built something simple on my own using AppleScript, which allows me to easily access the iTunes information I need.

Doing some research I found an article at Doug’s AppleScripts that described the problem I wanted to solve. Copy the code below in the Script Editor (or get from GitHub), change the path to the text file and save as an application and don’t forget to check the “Stay open after run handler”.

Save Dialog of Script Editor

Run the app and voilĂ ! I haven’t looked into possibilities to hide the dock icon or anything else. And I guess you can run the code with some modifications as a shell script in the background. Maybe I’ll look into it later.

# based on http://dougscripts.com/itunes/itinfo/idle00.php

global latest_song

on run
	tell application "System Events"
		if not (exists process "iTunes") then return
	end tell
	
	set latest_song to ""
end run

on idle
	tell application "iTunes"
		try
			copy name of current track to current_tracks_name
			if current_tracks_name is not latest_song then
				copy current_tracks_name to latest_song
				copy artist of current track to current_track_artist
				set output to current_track_artist & " - " & latest_song
				set dateString to do shell script "date +'%Y-%m-%d %H:%M'"
				
				do shell script "echo " & dateString & " - " & output & " >> ~/Desktop/test.txt"
			end if
		on error
			return 20
		end try
		return 10
	end tell
end idle