Novice Needs someone to write script(s) for iTunes

I’m new to Mac and need some help. I’m willing to pay for it. Anyone know a site or scriptbiulder who can help? I did a “consolidate library” with nearly 5,000 songs. Only what happened is it copied my backup iTunes folder from another external drive, so I have a good library of about 80 gigs, but the actual folder takes up twice that space as there are dups of nearly every tune with an “01” in the name. Dreading doing this one by one, I THINK I need to delete the copies with “01”, and redirect my library to the originals, or alternatively to delete the originals that do indeed have duplicates, then change the titles to delete the “01”, then redirect the library…?? In any case, I want to clean up the folder back to 80 gigs and to not lose any of my songs! Can anyone help?? Jeffe (jeffe@tpg.com.au)

Model: Powerbook G4 running iTunes 5.01 (4)
Browser: Safari 312.3.1
Operating System: Mac OS X (10.3.9)

first let me suggest the low-tech solution. erase everything from your itunes music folder (the path to that is ‘Home:Music:iTunes’), unmount the backup drive, and reconsolidate. when itunes consolidates it doesn’t move the originals, it just makes copies in the iTunes folder, so unless you’ve done something funky (like moved or stored music directly in the iTunes folder by hand) all your music will still exist. then when you consolidate without the backup drive connected, it will copy only one version of the file, and your problems will be solved.

now the medium-tech solution. if you really want to pay for software, go get File Buddy (or one of the other disk and file maintenance applications that are out there). it will cost you $15 dollars or so, and it’s a whiz at finding and deleting duplicate files. you can download a copy from Pure Mac http://www.pure-mac.com/ in Disk and File section (look under Utilities).

if you REALLY want to do Applescript (which is what I would probably do - lol) then you’re missing all the fun by asking someone else to do it for you. I will point out that an applescript would be good if this were something you’d have to do repeatedly, but this seems like a one-time error, and so the only value to wrestling with AS is the intellectual challenge of it. the basic process is simple: make a list of the files in your iTunes folder (there are a few different ways to do that), then sort them by name and compare adjacent file names, or search for the ’ 01’ tag iTunes puts after duplicate filenames. these options can range from a mild headache to a super-breeze, depending on what scripting additions you have and how much thought you put into it.

hope this helps. :cool:

Ah! Now we’re gettin somewhere!..Still a bit of a problem, tho it seems you might be on the right track. I haven’t gone to the medium tech solution yet, tho it may indeed be the answer…however…re your “low tech solution”…

a). It’s a tad scary to delete 160 gigs of music from my HD, but I could live with that as long as the backups were good. BUT, the backup drive doesn’t have ALL my music…just a backup from a few months ago…hence, I’d be deleting a fair bit of music that exists nowhere else at present except my iTunes library on my HD. So that solution doesn’t really work…Perhaps what I’d really need is to delete the backup, redirect my “home” library there, and then consolidate??? One major problem with that tho, is that is on a partition that only has 75 gigs of space if I delete the backup…so I couldn’t really consolidate there anyway…and if I did a consolidate with the music on my HD , since it does have both original and dupe (with the “01”), wouldn’t that just copy over BOTH and leave me with the same problem?..hmmmmmm.

Can you (or anyone) think of a problem if I did indeed invest $15 (not really a big deal to me at present), and deleted the originals in the current iTunes library folder and just used the copies with the “01” (which is where iTunes is directing itself at present)? Lost quality? Wierd future problems due to the “01” in the name?? Also, will a program like that dupe finder find the dupes if everything about the file EXCEPT the name is the same (due to the added numbers)? I’m feelin better, but we’re not quite there yet…thanks so much for responding amigo…Jeffe

Jeffe:

Here are some ideas and some scripts to try. First, go to iTunes and choose from the toolbar File and then Add to Library. You will be directed to point iTunes to a folder. Choose the folder that has ALL your music tracks, including the 01 copies. That should increase your library to include all the tracks, duplicates as well.

This first script prompts you to choose a folder, and will PERMANENTLY delete all the files that have “1.m” in their names. I chose “1.m” in case you have any m4p files, and I assumed your music is stored as mp3 files. The reason for the “1.m” is to flag only those files that are copies of original files, and therefore have the 1 added just before the .mp3 or .m4p. It should not flag files that happen to have a 01 in them somewhere, which is possible for those tracks ripped from CDs. There are risks here, especially if you have tracks wherein the normal name ends in a 1. You will lose those as well.

With that in mind, if you have a rather large backup drive with plenty of space on it, we could re-redirect the script to simply move the files (thus removing them from your music folder) to the other drive, and you could hold them until you were sure that nothing was lost permanently.

Here is that script:

set a to choose folder
tell application "Finder" to set aa to every file of entire contents of folder a whose name contains "1.m"
set bb to {}
repeat with c from 1 to count aa
	set dbb to quoted form of POSIX path of (item c of aa as Unicode text)
	do shell script "rm " & dbb
end repeat

Once that is finished, you now have an iTunes library that is going to look for a bunch of tracks that do not exist. This script will clean it up for you:

tell application "iTunes"
	set b to count every track of first library playlist
	repeat with a from 1 to b
		if location of track a of first library playlist is missing value then
			delete track a of first library playlist
		end if
	end repeat
end tell

And that should do it. Be brave, be bold, and get a big backup drive.

casdvm

I can’t thank you enough my friend. I am on the river for the next 3 days (I’m a guide all over the world, including here where we live in Australia, and the water’s up!)…but will try your scripts as soon as I can and will let you know how it went. Can you briefly describe what your second script does?..I’m inferring a bit of it by trying to translate it, but just wanted to be clear…in any case…I’m going to give it a shot and again, thanks so much…can I make a “donation” to you??? (Or take you on a river trip?) Cheers…I’ll be back in touch…Jeffe:)

Jeffe:

The second script now examines all the tracks in your library, and deletes those that do not have a file associated with them. They are tracks that, if you tried to play them, would display a little exclamation point to the left, indicating iTune’s inability to locate the file.

If you would like to simply move the “1.m” files to a safe place outside your iTunes library, use the following script. Rather than permanently deleting the files, this will move them to a folder of your choosing.

set a to choose folder with prompt "Choose the source:" --This is where the mp3 files are located.
set b to choose folder with prompt "Choose the destination:" --This is the folder (on the remote drive or home drive) to hold the files before you delete them.
set mbb to quoted form of POSIX path of b
tell application "Finder" to set aa to every file of entire contents of folder a whose name contains "1.m"
set bb to {}
repeat with c from 1 to count aa
	set dbb to quoted form of POSIX path of (item c of aa as Unicode text)
	do shell script "mv " & dbb & " " & mbb
end repeat

I appreciate your generosity. Please make any donations directly here to Macscripter. This is a great forum and I would like to see it remain great. I believe most sections here carry the ‘Make Donation’ button near the top.

casdvm

Hey amigo…I just got that backup drive and tried your script…(I live in outback Australia and things take a while out here!)

The script had an error…timed out…

I tried the latest script where it copies the dupes and asks for a source and destination. It got as far as choosing the folders, then did nothing till it timed out…

Do I need to have the bold in the font? I copied the words exactly but>…??.. not the colors or the bolds … the 2 lines beginning with “set” are indented as shown…

any ideas?

thanks…cheers.s…Jeffe

Jeffe:

Welcome back! I should have figured we would have a timeout thing, since you said you are processing lots of files. Try this out:

set a to choose folder with prompt "Choose the source:" --This is where the mp3 files are located.
set b to choose folder with prompt "Choose the destination:" --This is the folder (on the remote drive or home drive) to hold the files before you delete them.
set mbb to quoted form of POSIX path of b
with timeout of weeks seconds
	tell application "Finder" to set aa to every file of entire contents of folder a whose name contains "1.m"
end timeout
set bb to {}
repeat with c from 1 to count aa
	set dbb to quoted form of POSIX path of (item c of aa as Unicode text)
	do shell script "mv " & dbb & " " & mbb
end repeat

This may take a long time to run, so be prepared to have something else to do (maybe hours) while it is working.

With that in mind, I have also put a timeout on the second script to clean up your iTunes after you are finished:

with timeout of weeks seconds
	tell application "iTunes"
		set b to count every track of first library playlist
		repeat with a from 1 to b
			if location of track a of first library playlist is missing value then
				delete track a of first library playlist
			end if
		end repeat
	end tell
end timeout

I look forward to your next report.

casdvm

Hey CASDVM:

Well…thing’s are lookin great…only have about 500 more dup songs after deleting dups with first successful script ( a few hours worth!)…been cleaning up with your second script…but hitting a snag…not sure what it’s coming up against, but I get a script error: “iTunes got an error. Can’t get location of track (random number here) of library playlist 1.”

I tried manually deleting a few songs, and sometimes the script gets a bit further before coming up with the same error message with a different track number, sometimes sticks where it was with the same track number. Not doing anything but restarting the script gives me a duplicate error message with same track number…

any ideas? (Failing any progress, at least it’s to where I can manually delete the dups left over now. They show about 500 in iTunes when I do a show duplicates. Some are “track 1, track 2, etc.”, some are name and artist dups where the time is different as it’s two different versions of the same song. Some are dups where one is missing (! file) and one is good (which are the ones we’re looking for)…

any idears?

PS…is $50 a good donation amount to the forum or is that chintzy? Thanks for all your help amigo…I feel tons better!

cheers…Jeffe

Jeffe:

Hey, sounds like progress to me. I am frustrated by the whole error issue, mainly because I can’t understand it. But, no worries, it is easy to fix. Could you please run the script again, and send me the error number it generates? I can easily modify the script to catch those errors, and delete the track info, but it would be cleaner, and help me out a little to have it look for a specific error number.

Just out of curiousity, how long (about) did it take the first script to analyze and move all your dup files? How many dup files did you end up with on your remote drive?

I don’t think $50 is chintzy; I am no pro, and am happy you would like to help out the bbs.

casdvm

there’s 3,464 dupe files so far…but there are still about 375 +/- more files in iTunes than I started with before I did the “add to library”…but I can live with that and tweak if need be…I did a show dups in iTunes and it showed about 535 duplicates (eg; original plus duplicate), but most of them were titled the same (like “track 1, track 2, etc…”) but were actually different songs (different length of time for the song) that Gracenote couldn’t find. I must admit to not exaclty timing the first moving procedure, but it was definitely a few hours. (Note the cheap backup drive I got to deal with this is a USB which probably has something to do with it…). Finally, there was no error number. All it says is:

AppleScript Error

iTunes got an error: Can’t get location of track 2325 of library playlist 1.

that’s it…then it stops and I have to restart the script…but it must start all over again instead of moving from that point forward, as it comes up with the same thing.

?

cheers…Jeffe

Jeffe:

No problemo. This modification of the second script should catch all errors of trying to find a non-existent track and delete them. I left the long timeout just in case.

with timeout of weeks seconds
	tell application "iTunes"
		set b to count every track of first library playlist
		repeat with a from 1 to b
			try
				if location of track a of first library playlist is missing value then
					delete track a of first library playlist
					
				end if
			on error
				delete track a of first library playlist
			end try
		end repeat
	end tell
end timeout

Let me know; this should get rid of all the ! tracks in the library.

casdvm

Thanks amigo…copied and pasted and started your latest script…got about 5 more ! songs deleted, then got another error: “iTunes got an error: track 4915 of library playlist 1 doesn’t understand the delete message.”

Went back and tried to delete some spaces you had in the on error line, and it came back with a syntax error: “delete track a of first library playlist is illegal as a formal parameter.” anyway…again…happy to keep trying as it’s kind of interesting and fun to play with and start learning this stuff…as well as hoping to finalize the cleanup…but don’t stress…let me know

cheers…Jeffe

Jeffe:

I hope you are having fun with this; it seems to be never ending. OK, let’s try another modification. This time, if it can’t delete the track, it will add that location to a list, then write that list to a file after the run. It should not crash (I hope) as it goes through, and if my guess is correct, there will not be too many tracks left to mess with. You can go ahead and email me the file, and I can see if I can work with it from there, or if there are only a dozen or so tracks left, you can delete them manually. I don’t care either way; I am having a pretty good time with this so far.

with timeout of weeks seconds
	set er_rors to {}
	tell application "iTunes"
		set b to count every track of first library playlist
		repeat with a from 1 to b
			try
				if location of track a of first library playlist is missing value then
					delete track a of first library playlist
					
				end if
			on error
				try
					delete track a of first library playlist
				on error eMessage
					set end of er_rors to eMessage
				end try
			end try
		end repeat
	end tell
end timeout
try
	set mainpath to path to documents folder as Unicode text
	set savepath to mainpath & "DangErrors"
	set sf to open for access file savepath with write permission
	write er_rors to sf starting at 0 as list --The zero writes from the beginning
end try
close access sf

The file with the errors should be in your Documents folder, entitled “DangErrors.” You can email me by clicking on my highlighted casdvm in the shaded portion to the left of this message, and then click the highlighted “send e-mail.”

Good Luck (again)

casdvm

if you still have a problem, i would recomend using the terminal to fix this.
if you say all of your dup’ songs start with 01 you could do this
cd “the location of all your music”;rm 01*.mp3

you need to be careful though if any of your songs have a 01 infront of them because it is their track number they will be zapped

good luck

fixed…done…thanks!..Jeffe