Script to add files to iTunes crashing unpredictably

Hi,

In order to make it easy to use my music in both MP3 players with file-system navigation and ID3 based navigation, I’m using the following AppleScript code to add MP3 files to iTunes and change their ID3 tags based on their position in the file system (name equals file name, album equals the name of the folder containing the file and the artist equals the name of the folder above the album folder). I’m actually using the code in Automator, but I modified it slightly here so it can be tested on its own:


if not isAppActive("iTunes") then
	tell application "iTunes" to activate
	delay 2
end if

set the the_folder to "Path:To:Folder:Being:Added:"
set the the_folder_list to list folder the_folder without invisibles

repeat with f in the_folder_list
	
	set fileName to the_folder & f as text
	set AppleScript's text item delimiters to {":"}
	set songName to last text item of fileName
	set songAlbum to text item -2 of fileName
	set songArtist to text item -3 of fileName
	set AppleScript's text item delimiters to {""}
	set songName to (items 1 thru -5 of songName) as text
	
	tell application "iTunes"
		add fileName
		set song to result
		tell song to set {name, album, artist, genre} to {songName, songAlbum, songArtist, "Radio"}
	end tell
	
end repeat

on isAppActive(appName)
	tell application "System Events" to (name of processes) contains appName
end isAppActive

on replaceString(theText, oldString, newString)
	set oldDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to oldString
	set tempList to every text item of theText
	set AppleScript's text item delimiters to newString
	set theText to the tempList as string
	set AppleScript's text item delimiters to oldDelimiters
	return theText
end replaceString

I apologize if the code is inelegant. I’m very much an AppleScript newbie.

Anyway, the problem is that this code, repeatedly given the same input, randomly crashes in the part where song properties are modified in iTunes, reporting either “iTunes got an error: Parameter error.” or “iTunes got an error: File permission error.”. Sometimes it even succeeds. Also, it seems that it doesn’t crash twice on the same file - once the file was added to iTunes, its properties will be properly set during the next round without a problem. Therefore, repeatedly running the script will eventually complete the job.

The fact is also that the exact same code actually worked without a problem until recently (maybe before iTunes 8?).

I would really appreciate it if anyone could illuminate the problem in any way, or possibly propose a way to set ID3 tags without actually using iTunes, if there is no workaround for this situation.

Thanks.

Model: MacBook
AppleScript: 2.0.1
Browser: Firefox 3.0.3
Operating System: Mac OS X (10.5)

Hi,

according to the dictionary of iTunes the add command expects an alias or alias list, not a string path

Hi Stefan, thank you for your response.

That might be so, but the add statement is not the problem. If I remove or comment

tell song to set {name, album, artist, genre} to {songName, songAlbum, songArtist, "Radio"}

from the script, everything works fine. In fact, to solve the problem I simply wrapped this statement in a try block, and now the script works. Of course, that still doesn’t answer the original question why this particular line fails so often and what changed in the latest version of iTunes that makes it fail (because it worked before for a long time).