Folder Action Script Mac Tip

Discussion of Automation, Image Workflow and Raw Image Workflow

Moderators: Tom, Kukurykus

larsen67

Folder Action Script Mac Tip

Post by larsen67 »

Creating a 'Folder Action Script' on the mac OS (requires AppleScript).

Firstly we need to create a folder that we want to be Apple events enabled. (I've just created one called 'Image Drop' on my desktop). Along with my very simple test JavaScript 'AlertFileName.jsx' which needs to be on the desktop for this example to work.



Next we will need some base AppleScript that will handle the file(s) 'added' events. Apple gives you a basic AppleScript application 'Script Editor'. This can be found in your applications folder or you can just use spotlight and have it locate it for you… Just launch it and it will create you a new blank script like so…



Next we need to add our basic AppleScript syntax… Here is a my example code…

property JavaScript : ""
set JavaScript to (path to desktop as text) & "AlertFileName.jsx" as alias

-- This handler creates a list of added items
on adding folder items to This_Folder after receiving these_items
try
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items
process_item(this_item)
end repeat
on error error_message number error_number
if the error_number is not -128 then
tell application "Finder"
activate
display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
end tell
end if
end try
end adding folder items to

-- This handler processes each item
on process_item(this_item)
try
tell application "Adobe Photoshop CS2"
set display dialogs to never
open this_item
-- Do our Javascript(s) or Action(s) here…
do javascript JavaScript
end tell
on error error_message
tell application "Finder"
activate
display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
end tell
end try
end process_item

Cut 'n' paste the above into Script Editor's open document window. AppleScript is complied code so you will be required to make a couple of basic edits.

Firstly edit the name of the version of Photoshop your are using… My example is CS2. If you get this wrong then you will be prompted to locate the correct application when you come to 'compile' the code…

Secondly the name of the 'JavaScript' file that we want Photoshop to execute once a file is opened. Mine is 'AlertFileName.jsx' and it is located on my desktop. The location of this file can be just about anywhere… but that is where this example is looking for it.

Then compile the code and save the file to the desktop as File Format 'Script' and whatever you want to call this…



Once saved you will need to place this script file in a specific location so that it can be used which is…

Library/Scripts/Folder Action Scripts/

Then locate the application 'AppleScript Utility' using spotlight is easiest… Like so…



Launch it so we can set the option to enable the 'Folder Action Scripts'…



Click the check box to 'Enable Folder Actions'… Then using the + button navigate to the Folder that you wish to add the AppleScript to…
Then with your folder selected in the first column use the + button in the next column to locate the AppleScript you just created to run.



Quit the application once done and it should be ready to test… (Well I hope so)…

Important NOTE once a file is opened AppleScript can perform either JavaScript or Actions. Which ever you choose to do you must NOT have the file save back to its current location as this WILL trigger an endless loop (saving back as new/modified file will trigger the file added event over again). Either save outside of this enabled folder or to sub directories of this folder.

I think I've covered most of the basics and I hope the image links work…