Folder Action for alias creation

So, I’m trying to make a folder action that, when running on a folder, will automatically make aliases of any item added to the folder in a second folder. (and remove the aliases when the original file is removed)

It’s so that when working on multiple projects, I can attach this script to the images folder in each project folder and I’ll have a master folder with aliases to every image from every project in one place.

I’m missing something, but I think it’s close… any suggestions?


(* Set up the destination folder where aliases will be placed *)

set destinationFolder to "Users:username:Desktop:Foldername:"

(* Add aliases to the destination folder for any item added to the original folder this script is attached to *)

on adding folder items to thefolder after receiving theAddedItems
	tell application "Finder"
		repeat with eachfile in theAddedItems
			make alias to eachfile at destinationFolder
		end repeat
	end tell
end adding folder items to

(* Remove aliases from the destination folder anytime a file is removed from the original folder this script is attached to *)

on removing folder items from thefolder after losing theRemovedItems
	tell application "Finder"
		repeat with eachfile in theRemovedItems
			delete eachfile in destinationFolder
		end repeat
	end tell
end removing folder items from

Thanks!

Hi,

as the folder is a subfolder of the desktop folder the folder name can be used directly.
Consider also that the setup of the folder is never called.

The class of a Finder alias is called alias file in AppleScript, you should also add the object specifier folder

on adding folder items to thefolder after receiving theAddedItems
	tell application "Finder"
		repeat with eachfile in theAddedItems
			make new alias file to eachfile at folder "Foldername:"
		end repeat
	end tell
end adding folder items to

the remove handler removes only the appropriate alias files

on removing folder items from thefolder after losing theRemovedItems
	tell application "Finder"
		repeat with eachfile in theRemovedItems
			set fileName to name of eachfile
			try
				delete alias file fileName in folder "Foldername:"
			end try
		end repeat
	end tell
end removing folder items from

Stefan, thank you for the help.

Is there any way to set a variable in the beginning of the script to use for the destinationFolder (as I had tried to do)?

I have almost no experience with AppleScript, but I was hoping I could use a posix path (since I can then drag the destination folder into the script editor from the Finder) and do something like this:


set destinationFolder to POSIX path of "/Users/username/Desktop/folder/"

I haven’t had any luck with this.

Thanks again for the help.

Mike

Folder Action Scripts are special because they respond only to event handlers.

You can hard-code the path like

property destinationFolder : "Macintosh HD:Users:myUser:Desktop:myFolder:"

properties are set at compile time.
The Finder works only with HFS paths (colon separated)

Stefan, so I’ve tried what you posted (using the property destinationFolder) and I’m still not having any luck. I’ve tried it with and without the hard drive name (I thought it wasn’t necessary when using AppleScript paths).

Even if I have to do it manually and change the add and delete parts to be the same, can you give me an example syntax for a folder on a mounted volume? i.e., “Volumes:Server:Images:” And should there be a colon at the end of the path given that it’s a folder?

Also, if there is a space in a folder name does it still work?

Thanks!

Mike

Try this:

(path to desktop as text) & "myFolder"

A colon separated HFS path starts always with the disk name, even if the disk is the startup disk

All external volumes follow the same rule: The HFS path starts with the disk name

POSIX: /Volumes/Server/Images
HFS: Server:Images

the colon at the end to specify a folder is a naming convention to be able to simply append a file name, it’s not mandatory in this case

Unlike the shell, spaces in paths don’t matter

I’m so close, but I can’t get the aliases to delete when an original file is deleted from the original folder.

I think it has to do with the fact that the files are deleted immediately (they’re on a Windows Server) and therefore are returned as a text list instead of a list of aliases.

I’m just not sure how to parse the list when it’s returned as text.

I’ve tried this:


on removing folder items from thefolder after losing theRemovedItems
	tell application "Finder"
		repeat with eachfile in theRemovedItems
			set fileName to name of eachfile
			try
				delete alias file fileName in folder "Server:Folder:Images:"
			end try
		end repeat
	end tell
end removing folder items from

but no luck.

Any suggestions?