Using Applescript to set up folder actions?

I am trying to hack a method by which I can automate the set up of folder actions.

Here is the scenario: I have some folder, say “Parent Folder”. Once in a while, a new folder is added to this folder. I want to have a folder action applescript on “Parent Folder” such that, when a new folder is added, it will set up a particular folder action on the added folder (always the same action).

I realize that the Folder Action Setup application is not scriptable, so I am working on a workaround hack. I am somewhat stuck. I imagine that this was not functionality Apple wanted us to have (as it could be dangerous in malicious hands), but I am interested in solving this problem.

It is not hard to write the script for “Parent Folder,” so take that as a given. It simply watches for new folders, and when it sees one, it executes some shell command.

The preferences for Folder actions are stored in ~/Library/Preferences/com.apple.FolderActions.plist. It would be rather straightforward to use Python and it’s XML parser to update that file in order add a new folder action, if not for the entry:

  <array>
	<dict>
		<key>alias</key>
		<data>
		AAAAAADiAAIAAQxNYWNpbnRvc2ggSEQAAAAAAAAAAAAAAAAAAAC7
		vvXQSCsAAAADZpAGMzMzICM1AAAAAAAAAAAAAAAAAAAAAAAAAAAA
		AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABJb
		lb5chG0AAAAAAAAAAP////8AAAkgAAAAAAAAAAAAAAAAABAACAAA
		u78uEAAAABEACAAAvlzKvQAAAA4ADgAGADMAMwAzACAAIwA1AA8A
		GgAMAE0AYQBjAGkAbgB0AG8AcwBoACAASABE//8AAA==
		</data>
		<key>scripts</key>
		<array>
			<dict>
				<key>name</key>
				<string>XXXX.scpt</string>
			</dict>
		</array>
	</dict>
</array>

I know it’s base64 encoded, but when I decode it, it is not immediately recognizable. I assume it is some form of the path to the folder on which the actions in the array are placed. Anyone know how to decode the data in the tag?

This doesn’t work for me, but I think it disproves the theory that Apple doesn’t want you to be able to do this, as this is an Appleâ„¢ script:


--Attach script to folder
property ChooseScriptPrompt : "Select a compiled script file containing folder actions"
property ChooseFolderPrompt : "Select a folder to attach actions"
property ErrorMsg : " is not a compiled script. (Ignored)."

on open DroppedItems
	choose folder with prompt ChooseFolderPrompt
	set TargetFolder to the result
	repeat with EachItem in DroppedItems
		set ItemInfo to info for EachItem
		if not folder of ItemInfo then
			set FileTypeOfItem to file type of ItemInfo
			set FileExtensionOfItem to name extension of ItemInfo
			if FileTypeOfItem is "osas" or FileExtensionOfItem is "scpt" then
				tell application "System Events" to ¬
					attach action to TargetFolder using EachItem
			else
				set ItemName to name of ItemInfo
				display dialog ItemName & ErrorMsg with icon caution
			end if
		end if
	end repeat
end open

on run
	my ChooseFileFromFAScriptFolder()
	open the result
end run


to ChooseFileFromFAScriptFolder()
	try
		set LibraryScripts to list folder (path to Folder Action scripts folder from local domain) without invisibles
	on error
		set LibraryScripts to {}
	end try
	try
		set UserScripts to list folder (path to Folder Action scripts folder from user domain) without invisibles
	on error
		set UserScripts to {}
	end try
	if (count LibraryScripts) + (count UserScripts) > 0 then
		set ChosenScript to choose from list LibraryScripts & UserScripts with prompt ChooseScriptPrompt
		if ChosenScript is in LibraryScripts then
			return {alias ((path to Folder Action scripts folder from local domain as Unicode text) & ChosenScript)}
		else if ChosenScript is in UserScripts then
			return {alias ((path to Folder Action scripts folder from user domain as Unicode text) & ChosenScript)}
		end if
	end if
	return {}
end ChooseFileFromFAScriptFolder

Found this in “Folder Actions” folder

The base command and this does work:


set TargetFolder to (choose folder)
set ThisScript to (choose file)

tell application "System Events" to attach action to TargetFolder using ThisScript

SC

Thanks a lot!

Use it wisely; With power comes great responsibility…
:lol:
SC

In my script menu the two first scripts are my custom “Add FA” and apple’s “Remove FA”.

Here is my custom “Add FA”, and anyone will need a custom path to the Folder Actions folder in thier Scripts folder. It sets your folder actions into a quick choose list instead of using the Finder to add them:


set FA_Folder to alias "Disk:Users:Who:Library:Scripts:Folder Action Scripts:"
tell application "Finder" to set FolderActions to name of (files of FA_Folder)

set TargetFolder to choose folder with prompt "Select Folder To Attach Action"

set ThisScriptsName to choose from list FolderActions with prompt "Select The Action"

set ThisScript to alias (FA_Folder & ThisScriptsName as string)

tell application "System Events" to attach action to TargetFolder using ThisScript

I was trying this yesterday, to get a folder action attached to the “/Volumes” folder in order to detect the iPod being plugged in (that works, BTW). It turns out that the Folder Action Scripts Folder is recognized by AppleScript:

path to Folder Action scripts folder

Cool!

Heh - I was almost as amazed at that, as I was at the fact that “/Volumes” can have a folder action even though you can’t see it in the GUI of Folder Action Setup. Once you attach something to /Volumes programmatically, however, it shows up in the Folders column of Action Setup.

Also, you can use “Choose File” with a type specifier of “osas” to let the user choose the script to be attached from the Folder Actions Folder, and it will only show the scripts in the folder for them to choose from:

set theAttachedScriptAlias to (choose file with prompt "Choose script to attach to /Volumes." of type "osas" default location path to Folder Action scripts folder without invisibles)

I had to have Finder tell me what the “type” was to learn it. Apparently “scpt” isn’t the type. :stuck_out_tongue:

Been coding AppleScript from its inception, and I still can’t get any useful information from those “dictionaries”. What the Script Editor needs more than anything is a separate window for issuing test commands.

That, or somebody write a text that gives examples of the parameters to commands. AppleScript could really be enormously powerful if it only had a straightforward, comprehensive reference manual that gave the command, the parameters with their required types (instead of the ubiquitous “anything”), and an example. Apple has not updated the reference manual in 6 years, and I have purchased all of the third-party books over the past 8 years or so, and none of them have helped very much.

Anyway, this Folder Action for the /Volumes folder looks as if it could have some very useful applications.