Newbie needs help manipulating files in the Finder

Hello everyone,

I maintain two music libraries in iTunes. One is a lossless library, while the other is a lossy AAC library. Most of my AAC files are at 256Kbps and I’d like to re-covert my entire lossless library at 160Kbps VBR to save some hard drive space. Converting the files is the easy part. Getting them out of iTunes is not. The main problem appears to be iTunes’s inability to copy files with the same name. If I select all of my new AAC files and attempt to copy them to a folder (which I will then import into the other iTunes library), the copy operation fails. This is because some of my files have the same name (the same track by the same artist on two different albums, or a single, etc). Frustrating!!!

So, here’s my kludgey solution. First, create a Smart Folder in the Finder that scans the iTunes Music folder and looks for the new converted files. Then write an AppleScript that does the following:

  1. Assuming the Smart Folder is sorted by filename, start at the second file in the list.
  2. If the selected filename is the same as the previous file’s name, change the Finder label to red.
  3. If the Finder label is already red, change it to orange.
  4. Go to the next file.

This will basically distinguish the duplicate tracks from one another. At this point I will create a Smart Folder for each Finder label. Import the files with no labels first, then the red files, then the orange files.

Although the Smart Folder has no problem containing multiple files with the same name, when I try to import the files into iTunes, iTunes imports an incorrect number of files. I can only assume that it is having trouble with the duplicate file names. I think the labeling solution will allow me to separate these files and therefore not cause any confusion upon importing them.

Of course, if Apple only included support for multiple versions of the same track, I wouldn’t have this problem. But I digress…

So, being totally new to AppleScript, how do I do this? The logic seems obvious to me. I’ve been writing FileMaker Pro databases for years. If this was a set of FileMaker records, I could write this in five minutes. But, since I’m only marginally familiar with AppleScript, I’m a bit stumped.

Since Smart Folders aren’t real folders, how do I act upon the files? I’ve been able to count the files in the Smart Folder by counting the selected files. Next step would be initializing a counter variable, then looping through the files, counting up to the number of files in the selection before exiting the loop. This is where I am stumped.

Another option here would be to somehow manipulate the file names to ensure that they are unique (instead of using Finder labels). This seems like it might be a bit trickier to me, but perhaps not?

Any help, guidance, or references would be much appreciated.

Thanks!

-Rob

Well, I think I’ve solved my own problem. My first AppleScript. Woohoo. I first set the Finder label for all of the AAC files in my library to gray (using an AppleScript I found online somewhere). After that, my script seems to work.

Any comments? Suggestions on how I might improve it? Make it more efficient?

Thanks!

-Rob

tell application "Finder"
	
	set selectedItems to selection
	
	set songCount to count selectedItems
	set counter to 2
	
	repeat while (counter < songCount + 1)
		
		set prevFileName to get name of item (counter - 1) of selectedItems
		set fileName to get name of item (counter) of selectedItems
		set prevFileLabel to label index of item (counter - 1) of selectedItems
		set fileLabel to label index of item (counter) of selectedItems
		
		if (prevFileName = fileName) and prevFileLabel is 7 then
			set label index of item (counter) of selectedItems to 1
		else if (prevFileName = fileName) and prevFileLabel is 1 then
			set label index of item (counter) of selectedItems to 2
		else if (prevFileName = fileName) and prevFileLabel is 2 then
			set label index of item (counter) of selectedItems to 3
		else if (prevFileName = fileName) and prevFileLabel is 3 then
			set label index of item (counter) of selectedItems to 4
		else if (prevFileName = fileName) and prevFileLabel is 4 then
			set label index of item (counter) of selectedItems to 5
		else if (prevFileName = fileName) and prevFileLabel is 5 then
			set label index of item (counter) of selectedItems to 6
		end if
		
		set counter to (counter + 1)
		
	end repeat
	
end tell

display dialog "All finished!"