iTunes Inventory and update

These are two scripts I use to manage iTunes data across multiple computers. My purpose is to update my play counts and last played date on both my desktop and laptop computers.

This script creates a tab delimited file of iTunes data.


set tab_char to (ASCII character 9)
set log_file_path to (path to desktop as string) & "iTunes Inv .tab"
set file_id to open for access file log_file_path with write permission
write ("Persistent ID" & tab_char & "Album" & tab_char & "Disc Number" & tab_char & "Track Number" & tab_char & "Track Name" & tab_char & "Track Artist" & tab_char & "Genre" & tab_char & "Played Count" & tab_char & "File Location" & tab_char & "Description" & tab_char & "Enabled" & tab_char & "Last Played" & tab_char & "Skipped Count") to file_id starting at eof
close access file_id
tell application "iTunes" to set track_list to every track of source 1
repeat with current_track in track_list
	tell application "iTunes"
		set curr_persistent_id to (persistent ID of current_track as string)
		set curr_album to (album of current_track as string)
		set curr_disc to (disc number of current_track as string)
		set curr_track to (track number of current_track as string)
		set curr_name to (name of current_track as string)
		set curr_artist to (artist of current_track as string)
		set curr_genre to (genre of current_track as string)
		set curr_played_count to (played count of current_track as string)
		set curr_location to (location of current_track as string)
		set curr_description to (description of current_track as string)
		set curr_enabled to (enabled of current_track as string)
		set curr_skipped_count to (skipped count of current_track as string)
		if played date of current_track = missing value then
			set last_played to " "
		else
			set AppleScript's text item delimiters to ", "
			set last_played to "#" & ((text items 2 thru -1 of (played date of current_track as string)) as string) & "#"
			set AppleScript's text item delimiters to ""
		end if
	end tell
	set AppleScript's text item delimiters to ":"
	set curr_location to (text items 2 thru -1 of curr_location) as string
	set AppleScript's text item delimiters to ":"
	set file_id to open for access file log_file_path with write permission
	write (return & curr_persistent_id & tab_char & curr_album & tab_char & curr_disc & tab_char & curr_track & tab_char & curr_name & tab_char & curr_artist & tab_char & curr_genre & tab_char & curr_played_count & tab_char & curr_location & tab_char & curr_description & tab_char & curr_enabled & tab_char & last_played & tab_char & curr_skipped_count) to file_id starting at eof
	close access file_id
end repeat
say "Finished"

I load the output of the first script into an Access database running in Widows under VMware Fusion on my Mac Pro. The Access database creates a update file which the second script uses to update selected properties in iTunes.


set source_file to (choose file with prompt "Pick the count update file for this user") as string
set tab_char to (ASCII character 9)
tell application "iTunes" to activate
set file_id to open for access file source_file
try
	repeat
		set my_text to read file_id until return
		if length of my_text > 1 then
			set my_text to text 1 thru -2 of my_text
			set AppleScript's text item delimiters to tab_char
			set persistent_id to text item 1 of my_text
			set played_count to text item 2 of my_text
			set track_genre to text item 3 of my_text
			set track_enabled to ("1" = text item 4 of my_text)
			set track_last_played to (text item 5 of my_text)
			set skipped_count to text item 6 of my_text
			if skipped_count = "" then set skipped_count to 0
			set AppleScript's text item delimiters to ""
			if (count of every character of track_last_played) = 1 then
				set this_date to "Null"
			else
				set this_date to date track_last_played
			end if
			log persistent_id
			tell application "iTunes"
				set current_track to first track whose persistent ID is persistent_id
				if played count of current_track < played_count then set played count of current_track to played_count
				set genre of current_track to track_genre
				set enabled of current_track to track_enabled
				if this_date ≠ "Null" then
					if (played date of current_track as string) = "missing value" then
						set played date of current_track to this_date
					else
						if played date of current_track < this_date then set played date of current_track to this_date
					end if
				end if
				if skipped count of current_track < skipped_count then set skipped count of current_track to skipped_count
			end tell
		end if
	end repeat
on error number -39 -- eof
end try
close access file_id
say "Finished"