Folder Action to make new folder with selected item

First of all I’m a beginner in AppleScript. Second, my first language is french. So i’ll try my best to be clear.

I created a simple folder action to get all the items i have selected, move them in a new folder named with the first item selected (without extension) inside the folder action. For example if i select item 1, 2 and 3 and drag them on the folder action, it create a folder named “item 1” inside my folder action with item 1, 2 and 3 inside. Works great the first time but the second time i try it take couple of seconds to run and i wonder why. Any help would be very appreciated!!!

Here is my script:

on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		set myFirstItem to name of first item of these_items as text
		set myOldDelims to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "."
		set myFolderName to first text item of myFirstItem
		set AppleScript's text item delimiters to myOldDelims
		set myDestFolder to (make new folder at this_folder with properties {name:myFolderName})
		move these_items to myDestFolder with replace
	end tell
end adding folder items to

Thanks in advance for the help.

Folder actions are notoriously unreliable. That may be the problem right there. It may be just a side effect of being a folder action.

Some other things:

  1. The Finder can perform tasks slowly too if it is busy doing something else… which it often is… so I wouldn’t perform any tasks with the Finder if it’s not needed. Note that system events can get the name of a file so I would use that instead.
  2. “name” is already text, so you don’t need to coerce it to text.

As such you might try your code like this…

on adding folder items to this_folder after receiving these_items
	tell application "System Events"
		set myFirstItem to name of first item of these_items
	end tell
	
	set myOldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "."
	set myFolderName to first text item of myFirstItem
	set AppleScript's text item delimiters to myOldDelims
	
	tell application "Finder"
		set myDestFolder to (make new folder at this_folder with properties {name:myFolderName})
		move these_items to myDestFolder with replace
	end tell
end adding folder items to

Hey guys. The folder action will probably be faster and more reliable if you use a folder other than the watched folder as the destination, even if it’s just nested one level inside that folder. Additionally, “with replace” should read “with replacing”.

I tried to use a folder other than the watched folder as the destination and i still have the same problem.

First time i put something in, it’s fast, but if i try to put more items in the folder or just delete what i just done and try again, it take 5 to 10 seconds or it’s not working at all.
My folder is not busy doing something else and all my apps are closed.

Is it my script who’s not well written or is it a side effect of being a folder action, should i try system events instead and where can i find good info on how to do system events?

Here’s the script that i tried:

on adding folder items to this_folder after receiving these_items
	tell application "System Events"
		set myFirstItem to name of first item of these_items
	end tell
	
	set myOutputFolder to ((path to desktop as text) & "Output")
	set myOldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "."
	set myFolderName to first text item of myFirstItem
	set AppleScript's text item delimiters to myOldDelims
	
	tell application "Finder"
			set myDestFolder to (make new folder at myOutputFolder with properties {name:myFolderName})
			move these_items to myDestFolder with replacing
	end tell
end adding folder items to

Thanks!

Model: Mac pro 2 x 2.66 GHz Dual-Core Intel Xenon
AppleScript: 2.0.1
Operating System: Mac OS X (10.5)

You could just make it a droplet instead of a folder action. Save this as an application and drop files on it. Remember to remove the folder action if you do this. This script allows you to choose the folder where the files are moved to, but you could hard code that path if you want.

on open these_items
	tell application "System Events"
		set myFirstItem to name of first item of these_items
	end tell
	
	set myOldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "."
	set myFolderName to first text item of myFirstItem
	set AppleScript's text item delimiters to myOldDelims
	
	set myOutputFolder to choose folder with prompt "Choose a folder. The dropped files will be moved into a folder named \"" & myFolderName & "\""
	try
		tell application "Finder"
			set myDestFolder to (make new folder at myOutputFolder with properties {name:myFolderName})
			move these_items to myDestFolder with replacing
		end tell
	on error theError number errorNumber
		tell me
			activate
			display dialog "There was an error:" & return & theError & return & return & "Error Number: " & errorNumber as text buttons {"OK"} default button 1 with icon stop
		end tell
	end try
end open

on run
	display dialog "This is a droplet, so drop some files on me!" buttons {"OK"} default button 1 with icon note
end run

This is EXACTLY what i was looking for!!! I never thought of doing it that way.

Thanks regulus6633. Well done!!!