Adding tags in iTunes

Help would be great. I already have a script that has converted a flac file to a wave and extracted the metadata. What I am trying to do is to add the meta data to the track that was just imported to iTunes as a ALAC file. Everything seems to work upto and including the delete orig_Track. I can’t get iTunes to modify the track. What is going on?

Thanks,
Andy

	tell application "iTunes"
		try
			set orig_Track to (add import_file)
			set file_after_added_to_iTunes to (location of orig_Track)
			set new_Track_file to item 1 of (convert file_after_added_to_iTunes)
			set file_after_converted to location of new_Track_file
			delete orig_Track -- deletes from iTunes library only, not from disk
			set new_Track to track of new_Track_file
			set mdata to process_metadata(mfile)
			display dialog (artist of mdata) as string buttons {"Cancel", "Ok"} default button "OK"
			--set new_Track_ref to reference of new_Track
			set new_Track's name to (title of mdata)
			set new_Track's artist to (artist of mdata)
			set new_Track's album to (album of mdata)
			set new_Track's track number to (tracknumber of mdata)
			set new_Track's genre to (genre of mdata)
			set new_Track's comment to (comment of mdata)
			set new_Track's year to (date of mdata)
			
		end try
	end tell

What’s “mfile” and where’s the “process_metadata()” handler? This handler is custom and not part of iTunes so to call it wintin an iTunes block, you’ll need to preface it with “my”: “set mdata to my process_metadata(mfile)” --this tells iTunes to look elsewhere in the script for this subroutine. Also, the try block will return the error if you ask it to:

try
  --do something
on error the_error
  display dialog the_error
end

Jon

Mfile is the txt file output from metaflac of the FLAC file

		set metadata_file to "/tmp/" & name of file_name & ".meta"
		set mfile to alias (POSIX file metadata_file)

the file being read looks like this…

artist=Cowboy Junkies
title=Mining For Gold
album=The Trinity Session
date=1988
tracknumber=1
genre=Alternative
COMMENT=I ripped this.

process_metadata looks like this…

on process_metadata(metafile)
	open for access metafile
	set fileContents to read metafile using delimiter {"
"}
	close access metafile
	
	set new_name to fileContents
	--set new_name to replace_char(fileContents, "=", ":")
	--display dialog new_name as string buttons {"cancel", "ok"} default button "ok"
	
	set txtvar1 to item 1 of new_name
	set txtvar2 to item 2 of new_name
	set txtvar3 to item 3 of new_name
	set txtvar4 to item 4 of new_name
	set txtvar5 to item 5 of new_name
	set txtvar6 to item 6 of new_name
	set txtvar7 to item 7 of new_name
	
	set artiststr to trim_line(txtvar1, "artist=", 0)
	set titlestr to trim_line(txtvar2, "title=", 0)
	set albumstr to trim_line(txtvar3, "album=", 0)
	set datestr to trim_line(txtvar4, "date=", 0)
	set tracknumberstr to trim_line(txtvar5, "tracknumber=", 0)
	set genrestr to trim_line(txtvar6, "genre=", 0)
	set commentstr to trim_line(txtvar7, "COMMENT=", 0)
	
	return ({artist:artiststr, title:titlestr, album:albumstr, year:datestr, tracknumber:tracknumberstr, genre:genrestr, comment:commentstr})
end process_metadata

This I got around by defining mdata before the try. This is much more appropriate. Great tip. Thanks!

My real problem is still with figuring out the difference between track, item, file track. I want to manipulate the track tags. I think I am still trying to set values on the file and not the track, but I am not sure how to make the distinction…

There may be something useful in my “Make MP3 Audio Book” script:

http://homepage.mac.com/jonn8/as/html/Make_MP3_Audio_Book.html

Jon