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
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
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/"
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?
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