folder action: on adding, don't add but make alias files

I found some related topics in this forum but none could really help me out, and neither can the applescript user guide or mac help.

This is what i want:
A folder action which, when folder items are added to the attached folder,

  • will cancel the adding of the items to the folder;
  • will make aliases to those items instead (preferrably without an “alias” suffix), in the attached folder.

I have no clue how to solve the first point. I had my ideas about how to meet the second requirement, as shown below, but when i attach this script to a folder it doesn’t have any effect… items added to the folder will stay there and there is no alias file.

on adding folder items to this_folder after receiving these_items
	repeat with item_i in these_items
		tell application "Finder"
			make new alias to item_i at this_folder
		end tell
	end repeat
end adding folder items to

Can anyone tell me what to do? Many thanks in advance!

AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hi Julian,

the proper syntax to create an Finder alias is

make new alias file to item_i at this_folder

but consider that creating new items in a hot folder can cause an infinite loop,
because the adding handler is called again (and again and…)

what do you mean with - will cancel the adding of the items to the folder; ?

I tried it with and without “new”, and also with and without “file”, none of the four possibilities has any effect…
But i’ll stick to your version for future versions of the script. Do you have any suggestions how to prevent the script from falling in an endless loop?

On cancelling the adding:
Suppose you drop item A from folder B on the attached folder (C). Normally, item A will be moved from folder B to C (if B and C are on the same volume). But i want item A to stay in folder B and there should be no copy of A in C, only an alias to it.

As a workaround, i made a script app that behaves pretty much like i wanted the attached folder to do:

on run
	set the target_folder to name_helper()
	tell application "Finder"
		open the target_folder
	end tell
end run

on open these_items
	set the target_folder to name_helper()
	repeat with item_i in these_items
		tell application "Finder"
			make new alias file to item_i at the target_folder
		end tell
	end repeat
end open

on name_helper()
	return "Ariadne:Users:juliangonggrijp:Testmap:A" as alias
end name_helper

Now i’m going to figure out how i can make this script more convenient. :slight_smile:
Stay tuned!

Because i want the script app to behave as much as a folder as possible, i changed the subroutine name_helper to the following:

on name_helper()
	tell application "Finder"
		set the tag_name to my name as Unicode text
		set the container_path to "Ariadne:Users:juliangonggrijp:Testmap:" as Unicode text
		try
			set the tag_path to (the container_path & the tag_name) as alias
		on error
			set the tag_path to (make new folder at the container_path with properties {name:the tag_name})
		end try
	end tell
	return the tag_path
end name_helper

But it doesn’t behave as i expected; the script is apparently searching for a member “name” in the script itself, and i can’t use the script’s name as a string.
If i initialize the tag_name differently, like below, the script works fine:

set the tag_name to "C" as unicode text

But i want the script to be able to look up it’s own name in the finder.
Any ideas?

If you do not actually want to trim off the “.app” from your droplet’s name (I presume it is going to be a droplet), then try this:

tell application "Finder"
	set tag_name to name of item (path to me)
	-- Other stuff
end tell

But, I think you probably do not want your target folder to have a “.app” extension, so you might want something like this instead:

tell application "Finder"
	set tag_name to trimExtension of me from name of item (path to me)
	-- Other stuff
end tell

to trimExtension from nameString
	local otid
	set otid to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to {"."}
		set nameString to text items of nameString
		if length of nameString is greater than 1 then
			set nameString to items 1 through -2 of nameString
		end if
		set AppleScript's text item delimiters to ""
		--set nameString to nameString as text -- This might mangle strings that were originally Unicode text, use the following instead.
		tell nameString to set nameString to beginning & rest
		set AppleScript's text item delimiters to otid
	on error m number n from o partial result r to t
		set AppleScript's text item delimiters to otid
		error m number n from o partial result r to t
	end try
	nameString
end trimExtension

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Thanks. The script is indeed going to be a droplet.

But unfortunately, your script doesn’t work either; i get an error message that the name of the item can’t be retrieved.

Btw, i think the name without the “.app” extention can also be retrieved with the property “displayed name” instead of “name”. :wink:

Hi Julian,

save this script as application bundle.
The destination folder will be created in the folder Contents in the folder package

global target_folder

on run
	set these_files to choose file with multiple selections allowed without invisibles
	process_files(these_files)
	tell application "Finder" to open the target_folder
end run

on open these_items
	process_files(these_items)
end open

on process_files(theFiles)
	set the target_folder to name_helper()
	repeat with item_i in theFiles
		tell application "Finder" to make new alias file to item_i at the target_folder
	end repeat
end process_files

on name_helper()
	set tag_name to text 1 thru -5 of (name of (info for (path to me)))
	set myContents to ((path to me as Unicode text) & "Contents:")
	try
		return myContents & tag_name as alias
	on error
		tell application "Finder" to return (make new folder at myContents with properties {name:tag_name})
	end try
end name_helper

Thanks, that script provided the solution for my problem!

I decided to choose for the old script and only use that crucial part of yours, it looks now like this:

on run
	set the target_folder to name_helper()
	tell application "Finder"
		open the target_folder
	end tell
end run

on open these_items
	set the target_folder to name_helper()
	repeat with item_i in these_items
		tell application "Finder"
			make new alias file to item_i at the target_folder
		end tell
	end repeat
end open

on name_helper()
	set the tag_name to text 1 thru -5 of (name of (info for (path to me)))
	set the container_path to "Ariadne:Users:juliangonggrijp:Testmap:" as Unicode text
	tell application "Finder"
		try
			set the tag_path to (the container_path & the tag_name) as alias
		on error
			set the tag_path to (make new folder at the container_path with properties {name:the tag_name})
		end try
	end tell
	return the tag_path
end name_helper

When i’ve configured the script to it’s final form, i’ll post again. :slight_smile:

Odd, it works for me when I add a display dialog line and save it as an app bundle.

That only works if the user has “Show all file extensions” in the Advanced tab of the Finder preferences turned off (I have it turned on). Also, you should be aware that the displayed name of an application bundle can be customized to be different from the folder name used to hold the contents of the bundle. The Quicksilver application exhibits this kind of customization.

tell application "Finder" to tell item (path to application "Quicksilver") to {name, displayed name}

Yeilds {“Quicksilver.app”, “Quıcĸsı?ⅴε?.app”}. The actual displayed name is " Quıcĸsıɩⅴεʀ.app", which has Unicode characters that are mangled when Script Edtior displays the results, but even with the mangled characters, it is plainly not the same as the folder name.

Anyway, as long as Apple does not change the app bundle extension (seems quite unlikely) you should be fine just trimming off the last 4 characters (using text 1 through -5, like StefanK demonstrated along with the using info for from StandardAdditions). :slight_smile:

I guess you’re right chrys, i just didn’t try all possibilities. Hey, i’m a total n00b to applescript. :slight_smile:

Thanks alot to both of you!

The script has reached it’s final status now. As you probably already guessed, i want to use it for tagging as a flexible alternative to putting files in folders.
It’s more like StefanK’s last suggestion afterall: the alias files are stored in the application bundle. This makes the system VERY flexible: you can save the tags anywhere and you can tag anything - including other tags, which is quite revolutionary compared to existing tagging systems such as those of gmail, last.fm and del.icio.us! And then to realise that this was already possible on the mac under mac os 8.5, and maybe even earlier (if you use an external folder to store the aliases, that is). :wink:

Of course i’ll post this script to the code exchange section as well, when i’ve made a good manual. However, i consider to give it some more flair with extras like a special icon and so on, and then post it to sourceforge… this might become quite popular. :slight_smile:

-- this script should be saved as an application bundle
-- (c) 2007 Julian, StefanK @ MacScripter.net
-- credit: chrys @ MacScripter.net
-- license: public domain
on run
	set the target_folder to name_helper()
	tell application "Finder"
		open the target_folder
	end tell
end run

on open these_items
	set the target_folder to name_helper()
	repeat with item_i in these_items
		tell application "Finder"
			make new alias file to item_i at the target_folder
		end tell
	end repeat
end open

on name_helper()
	set the tag_name to text 1 thru -5 of (name of (info for (path to me)))
	set the container_path to (the path to me as Unicode text) & "Contents:"
	tell application "Finder"
		try
			return (the container_path & the tag_name) as alias
		on error
			return (make new folder at the container_path with properties {name:the tag_name})
		end try
	end tell
end name_helper

Oh well, let’s just proceed directly with the visual extras. :slight_smile:

I extended the run handler abit:

on run
	set the target_folder to name_helper()
	tell application "Finder"
		make new Finder window with properties {target:target_folder, current view:icon view}
	end tell
end run

But it seems to ignore the “target” property of the window, the tag launches a window with the root folder of the computer… is it a bug or am i doing something wrong?

Hi Julian,

the correct syntax is

on run
	set target_folder to name_helper()
	tell application "Finder"
		tell (make new Finder window to target_folder)
			set current view to icon view
		end tell
	end tell
end run

Thanks man, without you this script would never work. :slight_smile:
How do you know all this stuff? When i search for those things by myself, i always seem to be unable to find the answer…

By the way, i’m gonna change the name of this topic to “Tags”.

Edit Here’s really the last version (for the time being) :slight_smile:

-- this script should be saved as an application bundle
-- (c) 2007 Julian, StefanK @ MacScripter.net
-- credit: chrys @ MacScripter.net
-- license: public domain

on run
	set target_folder to name_helper()
	tell application "Finder"
		tell (make new Finder window to target_folder)
			set current view to icon view
		end tell
	end tell
end run

on open these_items
	set the target_folder to name_helper()
	repeat with item_i in these_items
		tell application "Finder"
			make new alias file to item_i at the target_folder
		end tell
	end repeat
end open

on name_helper()
	set the tag_name to text 1 thru -5 of (name of (info for (path to me)))
	set the container_path to (the path to me as Unicode text) & "Contents:"
	try
		return (the container_path & the tag_name) as alias
	on error
		tell application "Finder"
			return (make new folder at the container_path with properties {name:the tag_name})
		end tell
	end try
end name_helper