Filed under: Features, How-tos, AppleScript
AppleScript: Exploring the power of Folder Actions, part III
So far in this AppleScript feature we've covered what folder actions are and how to create them. In this AppleScript post, I'm going to tell you how to create your own custom scripts and add them to your folder actions list. If you work with file permissions a lot, then you know how crazy it can get when you need to change a ton of files to their correct permission types. With this AppleScript folder action, you can easily change the permissions just by dragging and dropping files in their correct folder.
Creating the Script
To get started, we'll open the Script Editor (located in /Applications/Utilities). Once you have the editor opened, copy/paste the following script:
on adding folder items to this_folder after receiving added_items
tell application "Finder"
set fold_name to the name of this_folder
try
repeat with i from 1 to number of items in added_items
set new_item to item i of added_items
set the item_path to the quoted form of the POSIX path of new_item
do shell script ("/bin/chmod -R +r " & item_path)
end repeat
end try
end tell
end adding folder items to
This script will change the dropped files to a permission of "0644" meaning that everyone can read the file. For information about chmod and command line permissions strings, visit the Wikipedia page. Continue reading to learn more about this AppleScript and folder actions.
Saving the Script
To save your AppleScript and get it ready to attach to a folder, select File > Save. Type in a file name of your choosing, then select "Script" from the File Format drop-down box. Remember your save location, and click Save.
Attaching the Script
There are two good options that you can choose between for attaching your script to the folder. First solution is simple: If you want the script to show up in the regular Folder Actions Setup utility, then you will need to place your .scpt file in the /Library/Scripts/Folder Action Scripts/ directory. You will then see the script show up in the pop-up dialog when you setup your folder (as described in part one of the folder actions feature).
Additionally, you can right-click on your folder and select More > Attach a Folder Action. From the pop-up dialog, you will have the ability to select your custom .scpt file and have it attached to the selected folder.
Running the Script
To start your custom script, just drag and drop files that you want to be "chmodded" to the folder ... they will be magically transformed.
Almost any AppleScript can be converted into a folder action. A great place to start looking for folder action ideas is the /Applications/AppleScript/Example Scripts folder. There are a ton of examples for you to look at in this folder.

![TUAW [Cafepress]](http://www.blogsmithmedia.com/www.tuaw.com/media/tuaw-cafepress-promo.png)


Reader Comments (Page 1 of 1)
InsanelyOne said 2:49PM on 3-26-2009
Just a minor correction to your notes. The script will not set the permissions to 0644. It will add read permission to all users (owner, group, other).
e.g. if the existing permissions were 0755, they would remain 0755.
Reply
ortho said 3:40PM on 3-26-2009
Cool!
I changed "/bin/chmod -R +r " to "/bin/chmod -R ugo+wr " to make any file readable or writable by everyone. I can't wait to put this on our FTP folder... i'm so bloody sick of the Get Info Permissions shuffle.
Reply
ortho said 3:54PM on 3-26-2009
Is there anyway to have a folder action to set RWX permissions when I create a new folder inside the configured folder? or will it only work on items that are Drag-N-Dropped?
Reply
Maxwell said 9:44PM on 3-26-2009
Curious: Just what does the line
set fold_name to the name of this_folder
do?
Reply
Clay said 3:07PM on 5-28-2009
Hey. These articles are great. I'm not a "computer geek" but I have a macbook pro and I like learning how to use the utilities.
I'm wondering if an applescript can be made to require a password upon opening up a folder. I'm working on a very important document that I don't want read until nearly complete. For fun I'd like to enter a password everytime I try to open the folder in which the document is located. Is it possible to create an applescript that does this?
Reply