Add to iTunes, delete from Downloads, add Comment to ID3?

I’m trying to make an Applescript that can be used as a Folder Action to watch the Downloads folder and:

– Open .mp3s in iTunes and append #single to track Comments, then delete original .mp3s.

I’m stuck at the appending to Comments part right now. Here’s what I have so far:


property myFiletypes : {".mp3"}
on adding folder items to this_folder after receiving added_items
	
	set myFinderItems to added_items
	tell application "System Events"
		set allItems to every item of myFinderItems
	end tell
	
	repeat with i from 1 to the count of allItems
		try
			-- get an item
			set myFinderItem to (item i of allItems as alias)
			-- check if it is a folder
			tell application "Finder" to set myItemIsFolder to (kind of myFinderItem = "Folder")
			-- if it isn't a folder...
			if myItemIsFolder is false then
				-- see if the file has the right extension
				tell application "Finder" to set myItemExtension to "." & (name extension of myFinderItem) as string
				if myFiletypes contains myItemExtension then
					set mySongName to myFinderItem as alias
					-- add track to iTunes
					tell application "iTunes"
						add mySongName
					end tell
					tell application "Finder" to delete file myFinderItem
				end if
			end if
			
		on error
			display dialog "Error"
		end try
	end repeat
	
end adding folder items to

Any help is very appreciated!

Hi,

try this, I cleaned up your code a bit :wink:


property myFiletypes : {"mp3"}

on adding folder items to this_folder after receiving added_items
	repeat with anItem in added_items
		try
			-- see if the file has the right extension
			if myFiletypes contains (name extension of (info for anItem)) then
				-- add track to iTunes
				tell application "iTunes"
					set addedTrack to add anItem
					set trackComment to comment of addedTrack
					if trackComment is "" then
						set comment of addedTrack to "#single"
					else
						set comment of addedTrack to trackComment & return & "#single"
					end if
				end tell
				tell application "Finder" to delete anItem
			end if
		on error e
			display dialog "Error: " & e
		end try
	end repeat
end adding folder items to

Thank you, but it’s giving an error and not working consistently at all. Sometimes it won’t add all the songs, sometimes it comments and sometimes not. Odd.

edit.

This is working in every way EXCEPT it is not deleting the .mp3 file from the ‘Downloads’ folder. Any help?


property myFiletypes : {"mp3"}

on adding folder items to this_folder after receiving added_items
	repeat with anItem in added_items
		try
			-- see if the file has the right extension
			if myFiletypes contains (name extension of (info for anItem)) then
				-- add track to iTunes
				tell application "iTunes"
					set addedTrack to add anItem
					-- add grouping to track
					set trackGrouping to grouping of addedTrack
					if trackGrouping is "" then
						set grouping of addedTrack to "singles"
					else
						set grouping of addedTrack to trackGrouping & "singles"
					end if
				end tell
				tell application "Finder" to delete anItem
			end if
		end try
	end repeat
end adding folder items to

Thank you!

You could always try this. (Due to time restrictions I’ve not managed to test it out yet)


property myFiletypes : {"mp3"}

on adding folder items to this_folder after receiving added_items
	repeat with anItem in added_items
		try
			-- see if the file has the right extension
			if myFiletypes contains (name extension of (info for anItem)) then
				-- add track to iTunes
				tell application "iTunes"
					set addedTrack to add anItem
					-- add grouping to track
					set trackGrouping to grouping of addedTrack
					if trackGrouping is "" then
						set grouping of addedTrack to "singles"
					else
						set grouping of addedTrack to trackGrouping & "singles"
					end if
				end tell
				do shell script ("rm " & anItem)

				(* Please note that this command needs a path in the form 
				/Users/[username/Downloads...
				and NOT 
				Macintosh HD:Users:username...
				so adapt 'anItem' to suit if it dosen't work.*)

			end if
		end try
	end repeat
end adding folder items to

Hello.
I have looked at your code, and can’t really believe why it won’t delete that file. Maybe Finder is specially picky about
getting a reference for the delete command, or maybe it is something else.

I have “instrumented” your code to return an error message, if something goes wrong during deletion, and also
made a reference out of the argument to the delete command.

Please post the error message and error number if it still doesn’t work. If it does work, then you can remove the
inner try block expect the new delete statement.


property myFiletypes : {"mp3"}

on adding folder items to this_folder after receiving added_items
	repeat with anItem in added_items
		try
			-- see if the file has the right extension
			if myFiletypes contains (name extension of (info for anItem)) then
				-- add track to iTunes
				tell application "iTunes"
					set addedTrack to add anItem
					-- add grouping to track
					set trackGrouping to grouping of addedTrack
					if trackGrouping is "" then
						set grouping of addedTrack to "singles"
					else
						set grouping of addedTrack to trackGrouping & "singles"
					end if
				end tell
				try
					tell application "Finder" to delete file (anItem as Unicode text)
				on error e number n
					display dialog e & " : " & n & " File name : " & (anItem as Unicode text)
				end try
			end if
		end try
	end repeat
end adding folder items to

McUsr, I have been trying your script for a few days and not only does it not delete the file, it also does not edit the ‘Grouping’ tag OR show any errors?

Fistoprince, I have not tried yours because I do not really understand the specifics of what I am to do?

Hello dozens.

It is really your script, I have just instrumented it to pinpoint out where the error is. Since the previous didn’t give any error messages, please try the new one.
Try to install this one, and watch what happens when you add an mp3 file. And please report back any error messages. If this script really doesn’t show up with any error messages, then everything should be all right.



property myFiletypes : {"mp3"}

on adding folder items to this_folder after receiving added_items
	repeat with anItem in added_items
		try
			-- see if the file has the right extension
			if myFiletypes contains (name extension of (info for anItem)) then
				-- add track to iTunes
				tell application "iTunes"
					try
						set addedTrack to add anItem as list
					on error
						tell me
							activate
							display dialog "There were errors adding the track " & addedTrack as text
						end tell
					end try
					-- add grouping to track
					set trackGrouping to grouping of addedTrack
					if trackGrouping is "" then
						set grouping of addedTrack to "singles"
					else
						set grouping of addedTrack to trackGrouping & "singles"
					end if
				end tell
				try
					tell application "Finder" to delete file (anItem as Unicode text)
				on error e number n
					tell me
						activate
						display dialog e & " : " & n & " File name : " & (anItem as Unicode text)
					end tell
				end try
			else
				tell me
					activate
					display dialog "No items to  add to the Itunes library were found."
				end tell
			end if
		end try
	end repeat
end adding folder items to

Okay, this seems to work correctly, BUT I get that report every time I add anything at all… Even when it adds, tags and deletes it correctly…

More specifically, I get the report: “No items to add to the Itunes library were found.” each time.

Hello, and that would be when there are different items than mp3 files that are added to the Downloads folder?

Silence is consent or what they say, so I just post this, positively assuming that everything will work fine from now;)

The main error in your script was that iTunes expected the item to be added of type list.


property myFiletypes : {"mp3"}

on adding folder items to this_folder after receiving added_items
	repeat with anItem in added_items
		try
			-- see if the file has the right extension
			if myFiletypes contains (name extension of (info for anItem)) then
				-- add track to iTunes
				tell application "iTunes"
					
					set addedTrack to add anItem as list
					-- add grouping to track
					set trackGrouping to grouping of addedTrack
					if trackGrouping is "" then
						set grouping of addedTrack to "singles"
					else
						set grouping of addedTrack to trackGrouping & "singles"
					end if
				end tell
				tell application "Finder" to delete file (anItem as Unicode text)
			end if
		end try
	end repeat
end adding folder items to

Yes, it does display “No items to add to the Itunes library were found” when something other than an .mp3 is added, BUT it displays it also when an .mp3 file is added.

In other words, every time.

Also, it fires up when downloading begins, and not after a downloaded file is completed.

Hello.

I really doesn’ understand why it gives a message about not being added when it is being added, if you wait a little moment, I’ll add a handler to which takes care of the busy issue.

Here you are, you should not receive any error messages regarding “download not completed” or whatever with this one. :wink:



property myFiletypes : {"mp3"}

on adding folder items to this_folder after receiving added_items
	repeat with anItem in added_items
		try
			-- see if the file has the right extension
			if myFiletypes contains (name extension of (info for anItem)) then
				-- wait until the file is downloaded
				repeat while isBusy(anItem)
					delay 1
				end repeat
				tell application "iTunes"
					
					set addedTrack to add anItem as list
					-- add grouping to track
					set trackGrouping to grouping of addedTrack
					if trackGrouping is "" then
						set grouping of addedTrack to "singles"
					else
						set grouping of addedTrack to trackGrouping & "singles"
					end if
				end tell
				tell application "Finder" to delete file (anItem as Unicode text)
			end if
		end try
	end repeat
end adding folder items to

to isBusy(f) ” © Julio, Tinkered by McUsr
	set f to f as Unicode text
	if f does not contain ":" then set f to POSIX file f as Unicode text
	try
		open for access file f with write permission
		close access result
		return false
	on error
		return true
	end try
end isBusy

This seems to work! Thank you! It sometimes misses deleting a few if you add a bunch at once, but other than that it’s good. THANK YOU!

Hello.

It would be nice to know if this version works so that all the mp3 files gets deleted from your downloads folder after having been added to iTunes.
I couldn’t help myself, but make a version in order to make the deletions work properly.





property myFiletypes : {"mp3", "m4a"}
property skippedFiles : {}
property alertVolume : 0
on adding folder items to this_folder after receiving added_items
	set {output volume:theVol, output muted:isMuted} to (get volume settings) -- Bruce Phillips - Thanks!
	
	
	repeat with anItem in added_items
		try
			-- see if the file has the right extension
			if myFiletypes contains (name extension of (info for anItem)) then
				-- wait until the file is downloaded
				repeat while isBusy(anItem)
					delay 1
				end repeat
				tell application "iTunes"
					set addedTrack to add anItem as list
					-- add grouping to track
					set grouping of addedTrack to "singles"
				end tell
				try
					with timeout of 1 second
						tell application "Finder"
							tell me to set volume output volume 0
							delete file (anItem as Unicode text)
							tell me to set volume output volume theVol output muted isMuted
						end tell
					end timeout
					
				on error -1712 -- timeout happened 
					set volume output volume theVol
					set end of skippedFiles to (anItem as Unicode text)
				end try
			end if
			-- post or until the next time processing of not deleted sound files.
			if skippedFiles is not {} then set skippedFiles to skippedFiles's Unicode text
			if skippedFiles is not {} then
				repeat
					repeat with i from (count skippedFiles) to 1 by -1
						try
							with timeout of 1 second
								tell application "Finder"
									tell me to set volume output volume 0
									delete file (anItem as Unicode text)
									tell me to set volume output volume theVol output muted isMuted
									set contents of item i of skippedFiles to missing value
								end tell
							end timeout
						on error -1712 -- timeout happened 
							set volume output volume theVol output muted isMuted
							exit repeat -- must continue later.
						end try
					end repeat
					set skippedFiles to skippedFiles's Unicode text
					if skippedFiles is not {} then
						delay 1
					else
						exit repeat -- done processing left overs.
					end if
				end repeat
			end if
		on error e number n
			tell me
				activate
				display alert e & " : " & n
			end tell
		end try
	end repeat
end adding folder items to
if alertVolume is not 0 then set alert volume of (get volume settings) to alertVolume
to isBusy(f) -- © Julio, Tinkered by McUsr
	set f to f as Unicode text
	if f does not contain ":" then set f to POSIX file f as Unicode text
	try
		open for access file f with write permission
		close access result
		return false
	on error
		return true
	end try
end isBusy

Awesome! …but it won’t compile. It catches a Syntax Error at: display alert e & " : " n

Hello Dozens, I’m sorry, I edited the code online, while adding the on error statement, I have added the missing ampersand in the code above. Please try it, I’m very anxious about this! :slight_smile:

Hello.

I turned off sound while Finder deletes files.

Okay, I’m sorry to report that this script also is bad. Two instances tried, two failed:

  1. Added mp3 to folder, it added it to iTunes but didn’t delete the file OR tag the Grouping field, no error given.
  2. Added mp3 to folder, it added to iTunes, but no delete or tagging, plus this: “iTunes got an error: File permission error. : -54”

Also, whenever anything other than an mp3 is added to the folder i get: “Can’t get Unicode text of {}. : -1728”