Organik,
OK, let’s look at the simpler case I talked about previously, that of creating a folder action that will add tracks to iTunes when files are dropped onto a Finder folder.
First, let’s go from simpler to harder. We need the standard “on adding items” handler. It’s run when items (files or folders) are added to a Finder folder window. Here’s the basic handler:
on adding folder items to this_folder after receiving added_items
addTunes(added_items)
end adding folder items to
Part of what you get with an adding folder items handler is a list of items. Here we’ll call it added_items (we could have called it scoobydoo if we wanted to, AppleScript doesn’t care what we call it, just that it’s there).
Now the addTunes handler doesn’t exist (yet) so let’s write that. We need to tell iTunes we want to talk to it, then tell it what to do. During that, we need to take the added_items list and individually add each one, so we need a repeat loop. The handler looks like this:
on addTunes(added_tunes)
tell application "iTunes"
activate
if not (exists user playlist "AutoAdd") then ¬
make new user playlist with properties {name:"AutoAdd"}
set theCount to 0
repeat with myItem in added_tunes
set theTrack to add myItem as alias to user playlist "AutoAdd"
copy theCount + 1 to theCount
end repeat
display dialog "Added " & theCount & " tracks to iTunes"
end tell
end addTunes
Keep in mind, that while the “adding folder items” handler is something AppleScript understands, the addTunes handler is OUR creation, so we could call it Snoopy if we wanted to. Notice, though, that when we passed added_items we accepted it in our handler with a different name (just to prove that handler parameters are local to the parameter, just making a point). So when we get the list in addTunes, the added_items list is renamed to added_tunes.
First we get iTunes attention with the “tell application iTunes” line and tell it to launch if it isn’t already running with “activate”.
Then we check to see if there is a playlist called “AutoAdd” (could be SpongeBob, if we wanted). I thought it would be nice to have a playlist that keeps track of items we’ve added with the folder action. If the folder doesn’t exist, we make one.
Then we set a counter because we want to keep track of how many items get added. We’ll use it later, you’ll see.
Then we repeat through the items in added_tunes and add each one. The reason we say “set theTrack to add myItem” instead of just “add myItem” is that the add command in iTunes expects to return to you a reference to the added track, just to prove it did it. We’re not going to use the variable theTrack in anything, it’s kind of a throw-away.
During the repeat and after the add, we update the counter.
Then at the end, we display a message to the user telling them how many tracks were added. This serves two purposes: 1) it lets the user know we’re done adding, which is nice if you add a lot of tracks because it may take a minute or two and 2) it gives them an idea of how many files were added.
OK, so far we have a nice folder action. But what if we think “Gee, it’d be nice if I could use the same script to add files or folders of files without having to open the scripted folder.” For that reason, I added an “on run” handler.
Most AppleScripts have an implied “on run” handler–the whole script is assumed to be in an “on run” handler! But in a folder action script, we have to be specific, so we say:
on run
display dialog ¬
"Add a single file or a whole folder?" buttons {"File", "Folder", "Cancel"} default button 1 with icon "alert"
copy button returned of the result to myBtn
if myBtn is "File" then
set myList to (choose file with prompt ¬
"Select an audio file" without invisibles) as list
addTunes(myList)
else if myBtn is "Folder" then
tell application "Finder"
set myList to every file of (choose folder with prompt ¬
"Select a folder of audio files") whose name extension is in {"mp3", "aiff", "wav", "m4a"}
end tell
addTunes(myList)
end if
end run
Here we ask the user if he wants to add a single file or a folder of them, and let him navigate to the file/folder, then we add the file or folder of files to iTunes using the same addTunes handler we created earlier!
Note: The part that reads whose name extension is in {“mp3”, “aiff”, “wav”, “m4a”} specifies what kind of audio files to add. If you have other types, just add their file extensions to the list.
Hope this helps. Just take the 3 handlers and put them in a script, then save it to either “/Library/Scripts/Folder Action Scripts” or put it into your own Library folder’s Scripts folder in a folder named “Folder Action Scripts” (create it if it doesn’t exist). Then use the Script Menu and select the “Folder Actions” submenu and then “Attach Script to Folder”. It will ask you to select a script (hopefully this one will be listed!) and a folder.
Then you’re Done!