Weird stuff with folder actions!

Still teaching myself Applescript and still having fun :smiley:
Made quite some progress the last days thanks to this wonderful forum !

I gave myself another assignment:

"Throw all sorts of files in a folder. The folder puts them into subfolders which are
named with the files’ extensions. If no such subfolder exists, create it. Files without
extensions go to a folder “no extension”. "

Example: all xxx.jpg go to a subfolder called “jpg”

Everything works fine (I’m so proud i got this going :smiley: ) but there are some questions…

Problem 1: why can I create a subfolder with a fixed string but not with a generated
string? Please see comments!

Problem 2: if I move the folder into another folder or rename the folder the script is activated! (A folder DS_Store is created and filled with the invisible file .DS_Store)

Edit:
Problem 3: I just tried it with 200 files! Boy, oh boy, how does this script choke! It seems to work but it takes forever! I have never seen so many beachballs of death! Restarting the Finder had no effect. I had to restart the whole Mac!


on adding folder items to this_folder after receiving added_items
	
	set basepath to this_folder as string

	--something weird happened here: I first tested my folder action with a fixed string
	--later on in the script I use basepath to create the subfolders. This works fine.
	--set basepath to "Macintosh HD:Users:vml:Desktop:opentest:" as string

	--In a later stage I  set basepath to the current folder whereever that is:
	--set basepath to this_folder as string
	--creating the subfolders fails!
	
	
	repeat with f in added_items
		set this_info to info for f
		if not folder of this_info then -- its not a folder
			set fstring to f as string
			
			set olddelimiters to AppleScript's text item delimiters --I'll move this out of the loop I guess
			
			set AppleScript's text item delimiters to {"."}
			set extstring to get last text item of fstring
			if extstring = fstring then -- there is no extension. This will go wrong with filenames that do contain a "." somewhere
				set extstring to "no extension"
			end if
			
			set AppleScript's text item delimiters to olddelimiters --I'll move this out of the loop I guess
			
			set fullpath to basepath & extstring
			
			
			try
				move f to alias fullpath
			on error
				try    -- this try is only because of the next problem, later I removed it.
					-- In the next line basepath fails, I have to use this_folder
					-- when basepath was a fixed string it worked fine!!
					-- when basepath was set by this_folder it fails!!!
					tell application "Finder" to set x to make new folder at this_folder (*why not basepath????*) with properties {name:extstring}
				on error
					display dialog basepath buttons {"Cancel"} default button 1
					-- the dialog shows the correct basepath!!!
				end try
				move f to alias fullpath
			end try
			
		end if
	end repeat
end adding folder items to

This doesn’t work when you use ‘basepath’ because basepath is a string.

You can’t create a folder at a string. For what AppleScript knows ‘basepath’ might be the text of the declaration of independence.

What you need to do is be more specific - coerce the string to a folder path:

 tell application "Finder" to set x to make new folder at [b]folder[/b] this_folder with properties {name:extstring} 

Thanks for your answer!

I realise that, I just thought it was strange that the following combination actually works, while basepath is a string!

set basepath to "Macintosh HD:Users:vml:Desktop:opentest:" as string
tell application "Finder" to set x to make new folder at  basepath with properties {name:extstring} 

Just one of those quirks I guess, I’ll just forget about it and (as you suggest) in the future coerce the string to a folder path!