Get attached scripts of folder?

Hello,

I’ve been using this bit of script for a few years now (it was supplied by StefanK):


tell application "System Events"
			launch
			set folder actions enabled to true
			set curScripts to (get attached scripts of folder myFolder)
end tell

It seemed to work successfully up to now. But in the Yosemite public beta, “attached scripts” fails.

Is there an alternative? I can’t find anything in the dictionaries that helps me figure this out, but that’s presumably the effect of my ignorance.

I can’t find an attached scripts property for ‘folder’ or in the folder actions suite here in Mavericks also. You’ll probably need to rewrite your own attached scripts routine. The dictionary:

Try running this:

set f to choose folder
tell application "System Events"
	every script of every folder action
end tell

This seems to do it:

set f to choose folder

tell application "System Events"
	set fa_name to name of f
	set fa to first folder action whose name is fa_name
	every script of fa
end tell

As far as I know, the answer is available in every mac.

In the script : Remove Folder Actions which is available on my Yosemite device, the syntax used to get the name of the scripts attached to a folder is :

tell application “System Events” to ¬
set FAScripts to name of every script of folder action FolderActionName
where FolderActionName is the name of a folder to which scripts are attached.

I double checked, the script is exactly the same on my Mavericks device.

Yvan KOENIG (VALLAURIS, France) jeudi 16 octobre 2014 20:27:58

I can’t get using the path property of a folder action. Don’t know why. You probably need to use the name property in conjunction with the reference to the folder.

Thank you, Yves and kel, for these extremely helpful replies. Your answers solved my problem entirely.

The only problem which is a big one is that you might have more than one folder with the same name. That’s why you need to do something to check the references.

Just thought of this. You might use a double tell statement.

No, that was just a far fetched theory. :slight_smile:

Actually kel1 has found a real problem. Yvan’s code relies on the name of the folder, but I actually three or four folders with the same name (WPDocs) on my system, and the users I’m writing for may also have multiple folders with the same name.

Kel1 or anyone - is there a way to make Yvan’s code restrict itself to a specific path?

Many thanks for your help with this.

Hi emendelson,

I think there might be a way to compare the folder paths. The folder action script has a variable whose value is the same as the chosen folder. For instance:

on adding folder items to thisFolder after receiving these items
-- code here
end on adding folder items to

The variable thisFolder is a reference to the folder. So we need to read the script and find what the value is. The script would have had to be run at least once.

I cannot test this out though. I have a screw in my tire and need to fix it soon. Maybe Yvan or someone else can try to do it. I’ll try to do it when I get back also.

gl,
kel

When I wrote :

[i]In the script : Remove Folder Actions which is available on my Yosemite device,.

I double checked, the script is exactly the same on my Mavericks device.[/i]

I assumed that you would be curious enough to look at the named script which is available on every boot volume.
Here is a subset which show the way to select a folder with attached scripts.
I don’t know how it behaves when several folders with the same name have attached script.
It don’t hurt to run it for see.


# © Apple

property ChooseFolderPrompt : "Select folder(s) to remove folder actions from "


tell application "System Events" to ¬
	set FolderActionNames to name of every folder action

if FolderActionNames is not {} then
	set ChosenFolders to choose from list FolderActionNames ¬
		with prompt ChooseFolderPrompt
end if

Yvan KOENIG (VALLAURIS, France) vendredi 17 octobre 2014 19:00:32

Yvan,

Ah - I misunderstood (my fault entirely) what you meant by the Remove Folder Actions script being available on the device. It doesn’t matter what I thought you meant - but I stupidly didn’t realize that you meant that the script was one of the sample scripts supplied by Apple. Obviously that’s what you meant, but I didn’t see it.

I’ll experiment with what happens with multiple folders that have the same name and will report back.

Thank you for having had the patience to continue with this.

Hi emendelson,

This is a hard problem. I don’t know how to get the value of the variable by reading the script.

I’m thinking that maybe you can modify the scripts. If you give the scripts run handlers, then you can run the script and somehow it tells you what folder it is attached to. Something like that.

gl,
kel

Not a problem with patience, just the fact that, as English is not my main language, I assumed that maybe my wording was not clear enough.

Those who live around me know that patience is not a feature widely available in me.

Yvan KOENIG (VALLAURIS, France) vendredi 17 octobre 2014 21:47:57

Hi emendelson,

I think you can read the plist com.apple.FolderActions. Try looking at: ~/Library/Preferences/com.apple.FolderActions.plist. Not sure how to read the alias key right now.

Edited:

do shell script "defaults read com.apple.folderactions"

gl,
kel

Hi emendelson,

Got the basic parts. Try running this and look at the results:

tell application "System Events"
	set plistFile to (POSIX path of preferences folder) & "/com.apple.folderactions.plist"
	tell property list file plistFile
		tell contents
			set scriptList to value of property list item "folderActions"
		end tell
	end tell
	set scriptCount to count scriptList
	repeat with i from 1 to scriptCount
		set thisScript to item i of scriptList
		-- get folder path from data (|alias|)
		-- get attached scripts for this folder action (|scripts| list)
	end repeat
end tell
scriptList

scriptList is a list of records. Each record holds info on every folder action. You can get the folder path by parsing the data as text (the |alias| label). Write the data to file and read as text. You can get the attached scripts through the |scripts| label.

If you’re interested I’ll be back tomorrow or maybe you can do it. It’s getting late (yawning).

Edited: here’s an example result of the scriptList where I have two folders with the same name:

Edited: I deleted the quote. There was too much info.

Edited: think there is a good way to parse the text for the folder path.
get startup disk name and folder action name
your delimiters might look like this “Macintosh HD:” and “:untitled folder”
get text that is between that and concatenate the ends back
you have your folder action folder path

You can also get the posix style path.

Edited: first need to strip non-visible characters. Darn.

gl,
kel

Thank you for this, which I’ve been experimenting with.

But there may be a workaround, at least for the script I’m working on.

What I want to do is test whether a folder action script is attached to a specific folder, and then attach the folder action script if it is not already attached.

If the script is already attached, then a “duplicate script” error occurs. It should be possible to trap that by number and not display the error. If the script is not already attached, it gets attached correctly.

Will report back later.

You may try this edited version of Apple delivered script.

(*
Remove Folder Actions

This script brings up a dialog with a list of attached folder actions for the selected
folder and lets you remove any of those actions.
This script uses the object model of Folder Actions.

Copyright © 2007 Apple Inc.

You may incorporate this Apple sample code into your program(s) without
restriction.  This Apple sample code has been provided "AS IS" and the
responsibility for its operation is yours.  You are not permitted to
redistribute this Apple sample code as "Apple sample code" after having
made changes.  If you're going to redistribute the code, we require
that you make it clear that the code was descended from Apple sample
code, but that you've made changes.

CAUTION :
On 2014/10/18, Yvan KOENIG added the ability to select the folder actions by their path
*)

property ChooseFolderPrompt : "Select folder(s) to remove folder actions from "
property ChooseScriptPrompt : "Select script(s) to remove from "
property NoFoldersActionsExist : "There are no folder actions."

tell application "System Events"
	set FolderActionNames to name of every folder action
	
	set FolderActionPaths to path of every folder action
end tell
repeat with i from 1 to count FolderActionPaths
	set item i of FolderActionPaths to item i of FolderActionPaths as text
end repeat
if FolderActionNames is not {} then
	set ChosenFolders to choose from list FolderActionPaths ¬
		with prompt ChooseFolderPrompt # returns a list of strings
	if class of ChosenFolders is boolean then error number -128 # No path selected, exit.
	set eachFolder to item 1 of ChosenFolders
	repeat with theFolderAction from 1 to count FolderActionPaths
		if item theFolderAction of FolderActionPaths as text is eachFolder then exit repeat
	end repeat
	
	tell application "System Events"
		set FAScripts to name of every script of folder action theFolderAction
	end tell
	
	set ChosenScripts to choose from list FAScripts ¬
		with title ChooseScriptPrompt with prompt eachFolder with multiple selections allowed
	
	if class of ChosenScripts is boolean then
		error number -128 # user chose no scripts, skip this folder action
	else
		repeat with EachScript in ChosenScripts
			set ScriptName to contents of EachScript
			tell application "System Events" to ¬
				delete script ScriptName of folder action theFolderAction
		end repeat
		
		--If we deleted all the scripts, delete the folder action object as well
		if (count of FAScripts) is (count of ChosenScripts) then ¬
			tell application "System Events" to ¬
				delete folder action theFolderAction
	end if
else
	activate
	display dialog NoFoldersActionsExist with icon note
end if

As I am lazy, I dropped the ability to select several folder actions in the first choose from list dialog.

Yvan KOENIG (VALLAURIS, France) samedi 18 octobre 2014 17:41:27