Self propagating folder action

This method resides in my scripts library, along with some non-relevant code to tag files automagically so i can find my schoolwork easily

on attachScriptToNewFolder:scriptLocation forPath:myFolderPath
	set aURL to current application's |NSURL|'s fileURLWithPath:posixPath
	set {isFileResult, isDirectory} to aURL's getResourceValue:(reference) forKey:(current application's NSURLIsDirectoryKey) |error|:(missing value)
	if isDirectory then
		tell application "System Events"
			attach action to folder myFolderPath using scriptLocation
		end tell
	end if
end attachScriptToNewFolder:forPath:

This is the code i’ve tried to use to call it.

--set theFile to the folder
set theFile to "/Users/michaelbedwell/Library/Mobile Documents/com~apple~CloudDocs/Documents/School/test"
--set theWorkflow to the workflow to be applied
set theWorkflow to "/Users/michaelbedwell/Library/Workflows/Applications/Folder Actions/Tag Files For School.workflow"
myLib's attachScriptToNewFolder:(POSIX path of theWorkflow) forPath:(POSIX path of theFile)

My code executes with no error, but the Folder Action does not appear on the folders I call this on (the upside is all of my tagging code works beautifully, it would just be nice to have new folders automatically have the action applied).

I’m sure it’s something simple that i’m missing, any help hugely appreciated.

As far as I know, “attach action” is no longer the correct scheme to attach a script to a folder.

Attach action is not available in System Events dictionary as well as in Standard Additions.

Look at the scripts delivered by Apple in the folder /Library/Scripts/Folder Actions. It’s what we are urged to do in page 284 of AppleScript User Guide :

You can Control-click a folder to access some Folder Action features with the contextual menu in the Finder. Or you can use the Folder Actions Setup application, located in /Applications/AppleScript. This application lets you perform tasks such as the following:

Enable or disable Folder Actions.
View the folders that currently have associated scripts
View and edit the script associated with a folder.
Add folders to or remove folders from the list of folders. Associate one or more scripts with a folder.
Enable or disable all scripts associated with a folder.
Enable or disable individual scripts associated with a folder. Remove scripts associated with a folder.

Folder Actions Setup looks for scripts located in /Library/Scripts/Folder Action Scripts and ~/Library/Scripts/Folder Action Scripts. You can use the sample scripts located in /Library/Scripts/Folder Action Scripts or any scripts you have added to these locations, or you can navigate to other scripts.

Yvan KOENIG running El Capitan 10.11.0 in French (VALLAURIS, France) jeudi 20 août 2015 14:03:25

Thank you for the pointer to the script examples, using that, I was able to come up with a working solution. I’ve seen many people asking the question but not many finding their answers in my googling, so I will post it here. Some of the working code comes straight out of the example script in the script library, but the following code will allow you to pass a path the workflow you want to attach, and a path to the folder you want to attach it to, so that you can script it rather than using file pickers. My original code was only partially right, there are two steps required. 1) create the folder action on the folder. 2) attach the script to the action. Thanks again for the pointer to the example.

on attachScriptToNewFolder:scriptLocation forPath:posixPath
	set aURL to current application's |NSURL|'s fileURLWithPath:posixPath
	set {isFileResult, isDirectory} to aURL's getResourceValue:(reference) forKey:(current application's NSURLIsDirectoryKey) |error|:(missing value)
	if isDirectory then
		log "It's a directory"
		set myScript to POSIX file scriptLocation as alias
		set myTarget to POSIX file posixPath as alias
		set TargetScript to myScript as text
		set TargetFolder to myTarget as text
		tell application "Finder" to set scriptName to name of alias TargetScript
		tell application "Finder" to set FAName to name of alias TargetFolder
		tell application "System Events"
			if folder action FAName exists then
				--Don't make a new one
				make new folder action at end of folder actions with properties {path:TargetFolder} --just makes the action
			end if
		end tell
		tell application "System Events"
			tell folder action FAName
				make new script ¬
					at end of scripts ¬
					with properties {name:scriptName}
			end tell
		end tell
		log "It's not a directory"
	end if
end attachScriptToNewFolder:forPath:

When we use the correct syntax the code behave well, what a surprise !

This said, I must recognized by the fact that “attach action to” is always displayed as a valid command by the Script Editor.
Where is it grabbing the needed information to do that ?

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) jeudi 20 août 2015 21:19:07

I’m guessing the script editors code dictionary needs to be updated (assuming it even has one) so that it doesn’t show valid anymore. The difficult part is in searching for a solution, that was the syntax I found in every ‘answered’ forum post. I’m sure this code is inelegant as I’m very unfamiliar with AppleScript still, but it does do the job, and is definitely not of the nature ‘attach action to folder’. Hopefully it helps some of the others who are searching for this to do the same.
As for myself, if I find myself scripting very many more things, I will probably be adding to my bookshelf of programming languages an AppleScript book. I love how easy it is to read and understand the intent of the code, but that seemingly free-form flexibility makes it difficult to discern actual syntactic structures. The code I kludged together for this surely demonstrates that I’ve assembled it using lots of google searching, and reading code examples, dictionaries, etc, rather than any real experience with it.

My understanding is that the Editors and AppleScript itself, grabs the functions/commands names :
from the language itself
from the applications dictionaries
from extensions like scripting additions or libraries.
I searched in AppleScript Language guide which is supposed to describe what is known by the language, in the applications targeted by your original script and in my set of scripting additions. I didn’t found something resembling to “attach action to” in 10.10.x as well as in 10.9.5.
Maybe it’s something the old string introduced during the Hypercard era when we call one of the numerous “path to” targets.

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) jeudi 20 août 2015 23:22:21