converting script to folder action

this nay be quite easy, only I have not had much practice with folder actions…

I have a working script that sorts pdf files into subfolders based on matching file names to folder names

Can anyone help me turn this into a folder action

Also on anothe note… this will move files to their specified folders only if their names match, but what if the file names don’t match any of the folder names-how can i move them into an “unknown” folder



set approvedItems to choose folder without invisibles
set filesToSort to choose folder without invisibles

tell application "Finder"
	set droppedFiles to every file in folder filesToSort
	set clientFolderList to every folder in folder (approvedItems)
	repeat with aFileToSort in droppedFiles
		set fileName to name of aFileToSort as string
		repeat with clientFolder in clientFolderList
			set clientFolderName to name of clientFolder as string
			set x to characters 1 thru 3 of clientFolderName as string
			set y to characters 1 thru 3 of fileName as string
			if x = y then
				move file 1 of folder filesToSort to folder clientFolderName of folder approvedItems
			end if
		end repeat
	end repeat
end tell




this should help you out a bit.

on adding folder items to FilesToSort after receiving droppedFiles
	repeat with aFileToSort in droppedFiles
		tell application "Finder"

		end tell
	end repeat

--move every item of folder filesToSort to folder unknown

and as for unknown files you can simply add after that repeat loop a move command which will move the leftovers to the unknown folder,

Turning this into a Folder Action script has evaded me - I can’t make it reliable. However, the script below will sort the files for you. It presumes that there is a folder named “Receiver” on the desktop, along with a folder named “Sender”, and one called “Unsorted”. In my Receiver folder there are three folders to which the sorted files sort, and those that don’t sort go to Unsorted. You can rename any of those by fixing their paths and names to suit you

set SortedFldr to (path to desktop as text) & "Reciever:"
set filesToSort to (path to desktop as text) & "Sender:"
set NotSorted to (path to desktop as text) & "Unsorted:" as alias
tell application "Finder"
	-- do things that only need doing once
	set unSorted to every file of folder filesToSort
	set fileNames to name of every file of folder filesToSort
	set fCnt to count of fileNames
	set clientFolderList to every folder in folder SortedFldr
	set clientFolderNames to name of every folder of folder SortedFldr
	set fldrCnt to count of clientFolderNames
	set folderkeys to {}
	-- set up the sort parameters
	repeat with j from 1 to fldrCnt
		set end of folderkeys to (characters 1 thru 3 of item j of clientFolderNames) as string
	end repeat
	set filekeys to {}
	repeat with k from 1 to fCnt
		set end of filekeys to (characters 1 thru 3 of item k of fileNames) as string
	end repeat
	-- now the shuffle
	repeat with f from 1 to fCnt
		repeat with fldr from 1 to fldrCnt
			if item f of filekeys = item fldr of folderkeys then move item f of unSorted to item fldr of clientFolderList
		end repeat
	end repeat
	-- clean up what didn't sort
	set didnotsort to every file of folder filesToSort
	move items of didnotsort to folder NotSorted
end tell

Thanks Adam

as usual… you’re solutions are always great
I actually got my script to do the same thing, yet slower than yours ( I timed yor script in script debugger-1.48 sec to sort 12 files… very impressive)

I am not liking the idea of setting this up a a folder action-because in the long run the script will look at the file names dropped and sort them to the designated folders, but if the script meets a file name that doesn’t correspond to any of the folders designated, but the file name does in fact fit a certain naming convention, then it can create a new folder in “receiver” for that job

And i did a little research and feel that because the script will create a new folder within “receiver” which would have the folder action attached, then it will most likely see it as a dropped item and try to run the new folder through the specs of the the action, and ultimately send it to the “unsorted” folder

am I wrong?, or can it work if you designate the difference between dropped items being folders or files, which might work

On another hand, is it possible to have a script simply run continuously to just watch a designated folder to see if any new items have been added-so we can simply affect the folder in question without attaching the folder action to it