Skip to Content

AppleScript: Exploring the power of Folder Actions, part I

Welcome to Part I of this mini AppleScript feature on creating useful folder actions. We'll have more posts on this topic coming up, but for now, let's introduce what a folder action is. Have you ever wanted to just drop a file into a folder and have something magically happen? Say, have a file printed, whisked to a remote site via FTP, or perhaps have an image be automatically flipped from horizontal to vertical? With Mac OS X's built-in folder actions, you can easily do this with a simple drag and drop.

Creating the folder
First, let's talk about how we can enable these "magical folders" that perform actions on files dropped within them. For this example, I'll show you how to make a folder on your desktop that, when a file gets dropped into it, will display a dialog letting your know that the file was placed there.Start by creating a regular folder on the desktop; you can either right-click and select "New Folder," or select "New Folder" from the Finder's File menu.
After you name the folder, you can right-click on it and select More > Enable Folder Actions. Right-click again and select More > Configure Folder Actions. This will launch an AppleScript utility called Folder Actions Setup.app. You will now be able to add the appropriate actions to your newly created folder. On the left hand side of the window, you will see a plus (+), minus (-), and "Show Folder" button at the bottom. Pressing the plus (+) button will allow you to select an appropriate folder to add an action to -- select the folder on your desktop that you just created.

A menu will roll down asking you to specify an Apple-created script. We'll choose the "add - new item alert.scpt." This script will alert the user via a dialog when an item is placed in a specific folder. In the right hand side of the window you will now notice that the add script has been placed there. You can add multiple scripts to a folder action and they will be performed in consecutive order until the end of the list is reached. Once you have created the actions, you can close the window.

It's just a drag to the left.... activating the scripts
With the folder now set up to accept drag and drops, you can drag a file onto it. In a few seconds (depending on the copy time), you'll see a dialog popup on your screen alerting you that your file has just been copied into this folder. Pretty neat, huh?

I tend to use this same folder action for my Drop Box located in the public directory of my user account. Since this folder is available to the public via a LAN connection, I am alerted whenever someone drops a file into it. There are literally tons of uses for the add file folder action. Why not play around with it and let us know your favorite uses for it?

Coming up in Part II: we'll go through Apple's supplied folder scripts and identify which of them provide the maximum automation payoff.

Categories

Features AppleScript

Welcome to Part I of this mini AppleScript feature on creating useful folder actions. We'll have more posts on this topic coming up, but...
 

Add a Comment

*0 / 3000 Character Maximum

22 Comments

Filter by:
Satyam

on adding folder items to this_folder after receiving added_items

repeat with file_ in added_items
tell application "Finder"
set file_ to POSIX path of file_
--do shell script "touch -c " & quoted form of file_
display dialog file_
end tell
end repeat

end adding folder items to


I copied and pasted above code in "Script Editor". But Will I have to save it as script or "run only app". I saved both the forms of it.
After than I right clicked a folder and selected "Attach Folder action" and selected script. Then I dragged a file. But it didn't popup any dialog for me.
Also I repeated the same with run only app. It also didn't display any dialog.
why?

May 15 2009 at 4:02 AM Report abuse rate up rate down Reply
Joseph

I am a lazy designer and store all of my files on my desktop. At the end of the week, I run Hazel w/ my desktop folder actions and move screenshots to a folder and change their name to screenshot-yyyy-mm-dd-hh-mm-ss.png and move all my istock images to my stock photography folder. etc...
It is a great way to keep organized. Theoretically you could do the same thing w/ folder actions.

February 17 2009 at 11:56 AM Report abuse rate up rate down Reply
jxn

I wrote a similar article about file sharing and notifications just one day before yours. Weird.

http://www.makeuseof.com/tag/establish-file-sharing-notifications-between-networked-macs/

February 17 2009 at 7:38 AM Report abuse rate up rate down Reply
jacjos7

Can same folder action be configured for all the child (nested) folders as well. I mean for example, I want to add files to iTunes when I copy those files to a particular folder or its existing or new child folder/s... can I achieve this using applescript/folder actions??

February 16 2009 at 9:51 PM Report abuse rate up rate down Reply
julian

yay, i like learning new things. thanksssss!

February 16 2009 at 7:58 PM Report abuse rate up rate down Reply
Flo

The problem with those folder actions (and with Hazel afaik) is, there is no way to include (i.e. extend the action to) sub folders. So if you create a sub folder in your desktop folder and a file is being added there, you won't get any alert box. Or am I missing something?

February 16 2009 at 7:18 PM Report abuse rate up rate down Reply
Greg

I use folder actions for two thinks :

- Adding Google Analytics code to the websites created with iWeb once uploaded to mac web server. I use an automator script for that purpose (http://www.apple.com/downloads/macosx/automator/addgoogleanalyticsaction.html)

- Downloading Linux distributions remotely using dropbox : First I download the .torrent file with my laptop at work. Once downloaded, the .torrent is detected by a folder action then moved to a special folder in my dropbox. When synced to my mac at home, another folder action detects the .torrent file and moves it to another folder whatched by Transmission. The download starts and I can burn the Linux DVDs once back home. (And Yes, I like chain reactions ;-)

February 16 2009 at 3:45 PM Report abuse rate up rate down Reply
1 reply to Greg's comment
Kevlar

Why not just enable the Transmission web interface? Then you can see how far your download is at any given point.

February 16 2009 at 4:14 PM Report abuse rate up rate down Reply
dada

auto add mp3 to itunes when dropped into a folder. Use the 2nd script its sweet.

http://dougscripts.com/itunes/itinfo/folderaction01.php

February 16 2009 at 3:20 PM Report abuse rate up rate down Reply
Jash Sayani

Great!! This is giving me tons of innovative ideas! BTW, where do I get the script to upload the dropped files to FTP ??

February 16 2009 at 3:02 PM Report abuse rate up rate down Reply
John

I didn't know about this use of applescript and I've been a Mac user for a couple years. Thanks for the tip!

February 16 2009 at 2:20 PM Report abuse rate up rate down Reply
Buy an ad here

Hot Apps on TUAW

Tweets

© 2012 AOL Inc. All Rights Reserved.