script to attach folder action

I was wondering if it is possible to write a script that attaches/detaches itself as a folder action to a folder.

The reason I want to do this is because I don’t want the folder action to be active all the time (this strikes me as being rather unelegant, programming wise). I think it is more elegant to write a script which attaches itself to a folder, does whatever it needs to do, and then detaches itself again when it has done enough, say after n iterations of the script. It also has the advantage that you keep full control over when the body of the script gets executed (and how often it iterates).

I assume there is some folder attribute that can be set. Question is: which one?

p.s. I realise that you can test for certain filenames to added/removed but like I said, I don’t think that’s very elegant.

Daniel

How would the script be triggered when it was not attached?

Manually in first instance. I might want to create a ‘metascript’ which attaches different scripts to folders depending on the values of some parameters. This might require the passing of arguments between scripts if that is possible.

If anything, the option to detach a script (stop the folder from being watched) could be useful to prevent events from being triggered when you don’t want it.

I assume a folder has an attribute that stores the script (if anything) attached to that folder. If this attribute can be modified using a script, you could attach/detach scripts dynamically

If you inspect the AppleScript dictionary of the “Folder Actions” extension (pre-OS X), you might find something useful. :slight_smile:

– Rob

The “Folder Actions” dictionary contains the following which should accomplish your desires.


attach action to: Attach an action to a folder
	attach action to  alias  -- the folder to attach the action to
		using  alias  -- a file containing the script to attach

remove action from: Remove a folder action from a folder
	remove action from  alias  -- The folder to remove the action from
		[action number  integer]  -- the index number of the action to remove…
		[action name  string]  -- …or the name of the action to remove

Just open this dictionary located in the extensions folder of the system folder of your startup disk.

Thanks, that’s the thing I’m after.