Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

wrldwzrd89

macrumors G5
Original poster
Jun 6, 2003
12,110
77
Solon, OH
This is one of my more elaborate AppleScripts. It's based off of:

Tags Mirror Artist-Album-Filename v2.1 12/17/11

The latest version of this script can be found at:
http://dougscripts.com/216

However, I am experiencing two problems:
  1. Tracks with apostrophes and periods in their names cause the script to fail
  2. On tracks that DO succeed, the name is getting set to the FULL path of the file instead of just the name less the extension at the end

I believe I have fixed both of these problems in the current version, but since I screwed things up so badly the last time I tried this, I want to be sure I haven't done something stupid. Anyway, the code is below:

Code:
tell application "iTunes"
	set s to selection
	if s is not {} then
		display dialog "\"Fix Module Tags\"" & return & return & ¬
			"This script will copy each selected track's filename, Album folder and Artist folder to its respective tag." & return & "The folders must be arranged in this manner:" & return & return & "Artist / Album / Songfile.xxx" buttons {"Cancel", "Continue..."} default button 2
		
		set oldfi to fixed indexing
		set fixed indexing to true
		repeat with i from 1 to length of s
			set t to item i of s
			if class of t is file track then
				tell t
					try
						if location is not missing value then
							set tn to t's name
							set aloc to (my get_location_for_track(tn) as text)
							set b to my text_to_list(aloc, "/")
							set n to my fixextension(tn)
							try
								set name to n
							end try
							try
								set album to item -2 of b
							end try
							try
								set artist to item -3 of b
							end try
						end if -- is file track and not missing?
					end try
				end tell
			end if -- file track?
		end repeat
		-- done
		set fixed indexing to oldfi
		try
			display dialog "Done!" buttons {"Thanks"} default button 1
		end try
	else -- no tracks selected
		display dialog "You must select some tracks first." buttons {"Cancel"} default button 1 with icon 0 giving up after 30
	end if
end tell

to fixextension(loc)
	set saveD to AppleScript's text item delimiters
	set my text item delimiters to "."
	set fixedloc to reverse of (rest of (reverse of (text items of loc))) as string
	set AppleScript's text item delimiters to saveD
	return fixedloc
end fixextension

on text_to_list(txt, delim)
	set saveD to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to {delim}
		set theList to every text item of txt
	on error errStr number errNum
		set AppleScript's text item delimiters to saveD
		error errStr number errNum
	end try
	set AppleScript's text item delimiters to saveD
	return (theList)
end text_to_list

on get_location_for_track(n)
	set formatfolder to upperCase(text item -1 of my text_to_list(n, "."))
	set basefolder to "/Users/miniwrld/Music/Source/Module/Current/"
	set searchfolder to basefolder & formatfolder
	return do shell script "mdfind -onlyin " & searchfolder & " -name " & (quoted form of n)
end get_location_for_track

on upperCase(s)
	set uc to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	set lc to "abcdefghijklmnopqrstuvwxyz"
	repeat with i from 1 to 26
		set AppleScript's text item delimiters to character i of lc
		set s to text items of s
		set AppleScript's text item delimiters to character i of uc
		set s to s as text
	end repeat
	set AppleScript's text item delimiters to ""
	return s
end upperCase
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.