dougscripts.com

Folder Actions and iTunes

Set up a Folder Action in OS X to automatically add files to iTunes when they are added to a particular folder.

 

Folder Actions are AppleScripts that you "attach" to a folder in the Finder. When particular actions occur, such as opening the folder, resizing the folder, adding or removing files, the attached AppleScripts are activated and perform their tasks in the background.

The particular script listed below will attempt to add any files to iTunes that have been added to a folder to which it is attached. You first compile and save the script with Script Editor. Name it "add files to iTunes" and save it to Library > Scripts > Folder Actions.

As of Snow Leopard (OS 10.6), Script Editor.app has been renamed AppleScript Editor.app and is located in your /Appplications/Utilities/ folder. This article refers to Script Editor.app but AppleScript Editor.app also applies.

Create a folder to which you will be adding audio files. For discussion, let's call it "New Music" and place it in your "home" folder. Open "New Music" and Control-Click inside its window. From the pop-up click on "Enable Folder Actions" (if you see "Disable Folder Actions", then you are all set). Next, Control-Click again to "Attach a Folder Action...". This will bring up a dialog so you can locate the "add files to iTunes" and select it.

Now, whenever you add a file to "New Music", it will be added to iTunes. Only audio, video and PDF files (and ePub book files as of iTunes 9.1--see here) can be added to iTunes, so don't worry if you drop a Word file or something else in there.

Internet Downloads: If you attach a Folder Action to a downloads folder, say, for example, your "Amazon MP3" downloads folder, you may not get the results you expect. Folder Actions kick in pretty quick once a file is added, sometimes faster than before a file can be completely downloaded (see "One other more thing" below). Your results may vary.

If you want more information on Folder Actions, be sure to read about them at Apple and in the AppleScript Language Guide.

One more thing: if you have your iTunes Preferences set to "Copy files to iTunes Music folder when adding to library" checked, you may want to delete the original file in "New Music" after it is added to iTunes. In that case, remove the block comment tags from the script—the "(*" and "*)"—so that the UNIX shell script commands can be active. These commands will remove the audio file from "New Music"

One other more thing: Since early 2008, I occasionally receive reports that Very Large Files fail to add correctly. Correspondent Kevin Laws describes the issue (see Kevin's fix after the original script listing below):

"AppleScripts attached to folders trigger the file received action when the new file first appears, not when it finishes copying. Your script works great if the file copy to the folder works faster than iTunes can fire up (most songs, for example). When copying movies or copying things over the network, however, iTunes starts importing before the file is finished copying. When I tried your script, it kept triggering as soon as the network copy started, which resulted in a failed import to iTunes (and a deleted file, since I'd put the delete in afterwards).

There are many threads on this topic as a bug in AppleScript. Just Google the following phrase: 'on adding folder items' wait until done.

Here's a thread from Apple:http://lists.apple.com/archives/Applescript-users/2007/Mar/msg00056.html."

NOTE: This behavior is fixed in OS 10.6: "Folder Actions now attempts to delay calling "files added" actions on files until they are done being copied. Previous versions called "files added" actions on new files as soon as they appeared in the file system. This was a problem for files that were being copied into place: if the file was sufficiently large or coming over a slow server link, the file might appear several seconds before it was done being copied, and running the folder action before it finished copying would behave badly. Folder Actions now watches if the file is changing size: if it stays the same size for more than three seconds, it is deemed "done", and the action is called."

Below is the original script. Kevin's fix (for pre-Snow Leopard systems) is listed just after.

on adding folder items to my_folder after receiving the_files
	repeat with i from 1 to number of items in the_files
		tell application "iTunes"
			launch
			try
				set this_file to (item i of the_files)
				
				add this_file
				
				(*
				-- if you have iTunes set to 
				--"Copy files to iTunes Music folder when adding to library"
				-- then you might want to delete the original file...
				-- if so, remove comments from this block and 
				-- use the UNIX commands below to delete the file
				
				set the file_path to the quoted form of the POSIX path of this_file
				do shell script ("rm -f " & file_path)
				
				*)
			end try
		end tell
	end repeat
end adding folder items to

Kevin says: "This is what I finally did to the script to make it work properly with large files. Basically, after the file appears it starts checking every 5 seconds to see if the file is still copying, which it can tell because the file size is still growing from the last time it checked. After the file size stops growing, it does the part of the script you suggested which imports into iTunes (and optionally removes the file)":

property DELAY_TIME_SECONDS : 5 -- How long to wait between checking file size.

on adding folder items to thisFolder after receiving theItems
	repeat with f in theItems
		set oldSize to 0
		set newSize to -1
		-- When newSize equals oldSize, it means the copy is complete because the size hasn't changed.
		repeat while newSize is not equal to oldSize
			-- Get the file size.
			set oldSize to size of (info for f)
			delay DELAY_TIME_SECONDS
			-- Sample the size again after delay for comparison.
			set newSize to size of (info for f)
		end repeat
		
		-- HERE BEGINS THE ITUNES SPECIFIC STUFF
		tell application "iTunes"
			launch
			try
				add f
				(* UNCOMMENT OUT NEXT 2 LINES IF YOU WANT THE FILE REMOVED AFTER IMPORT
				set the file_path to the quoted form of the POSIX path of f
				do shell script ("rm -f " & file_path)
                        *)
			end try
		end tell
		-- HERE ENDS THE ITUNES SPECIFIC STUFF
		
	end repeat
end adding folder items to

Site contents © 2001 - 2024 (that's right: 2001) Doug Adams and weblished by Doug Adams. Contact support AT dougscripts DOT com. About.
All rights reserved. Privacy.
AppleScript, iTunes, iPod, iPad, and iPhone are registered trademarks of Apple Inc. This site has no direct affiliation with Apple, Inc.
The one who says "it cannot be done" should not be interrupting the one who is doing it.