Pau Muñoz
Pau Muñoz

Reputation: 67

How to setup a folder action from the terminal(osascript)?

I'm trying to write an applescript that will prompt a dialog for selecting a folder, then another dialog for selecting an applescript file and then it will create a folder action attaching that script to that folder.

    set thisFolder to (choose folder with prompt "Choose the folder to which you want to attach the action." default location (path to desktop))
set thisScript to (choose file with prompt "Choose the action you want to attach to the folder." )

tell application "System Events"
    set folder actions enabled to true
    -- Only one 'folder action' can be attached to any one folder at any one time, but each may contain one or more 'scripts'. It's the scripts' folder action handlers which are triggered, not the scripts themselves. So a single script may contain different handlers for different triggers or you can have several scripts.
    -- The folder path specified when creating an action can currently be either an HFS path or a POSIX path, BUT the value actually set is a POSIX path without a trailing slash — which is revelant for any script which may test folder action 'path' values.
    set thisAction to (make new folder action at end of folder actions with properties {path:(POSIX path of thisFolder)})
    -- Only the script's name is specified as it's assumed to be in one of the (~)/Library/Scripts/Folder Action Scripts/ folders.
    make new script at end of thisAction's scripts with properties {name:(get thisScript's name)}
end tell

When I try to run this it works fine till the last line "make new script..." then it fails with a -1000 code

script.scpt:357:450: execution error: System Events got an error: AppleEvent handler failed (-10000)

The script that I'm trying to attach is the following:

on adding folder items to this_folder after receiving added_items
     display dialog "added"
 end adding folder items to

What could be the thing I'm doing wrong here?

Thanks for your time

Upvotes: 0

Views: 413

Answers (1)

pbell
pbell

Reputation: 3105

The syntax to attached folder action has been changed some time ago. "Attached action" is depreciated and "make" is now used. The correct syntax is as follow :

set thisFolder to (choose folder with prompt "select folder")
set thisScript to (choose file with prompt "Select script to attach")

tell application "Finder" to set ScripName to name of thisScript
set ScriptPath to POSIX path of thisScript

tell application "System Events"
    set thisAction to (make new folder action at end of folder actions with properties {path:(POSIX path of thisFolder)})
    tell thisAction to make new script at end of scripts with properties {name:ScripName, POSIX path:ScriptPath}
end tell

Warning : the make new folder action (1st instruction) will fail if this folder is already set for action (even if now script attached). Therefore, when testing, either you put it in try/end try block or you must delete action of this folder each time you are testing that script.

Upvotes: 1

Related Questions