Help with Folder Action script to move files by name to other folders

I’m new to Apple Script and need to create a script with OS X 10.3 running on an Xserve. I’ve looked at the examples on the system as well as posts on the MacScripter BBS, I can got those to run fine. When I try to bend the examples to what I want, the scripts don’t run at all. I figure I’m probably trying to do too much or meld examples together that won’t work together and it’s best just to start from scratch.

This is what I’m trying to accomplish:
Using Folder Actions, when an item is added to this folder, if the first two characters in the file are AB then run this Apple Script or if the first two characters are CD then run this other Apple Script. For each case, take the name of the file, e.g. ABYZ031704C1.txt, and set characters 3 through 10, e.g. YZ031704, as the name of the folder to be used in processing the jobs called in the aforementioned Apple Scripts. Then move the file to another folder named processed.

I’ll greatly appreciate any help and thanks in advance.

I’m not sure where you want to create the new folder with the name that’s been extracted from the file name. In the folder that has the folder action attached? Should the files then be moved to the new folder? Here’s some code that might get you started. If you provide more details, we can likely make it happen.

on adding folder items to this_folder after receiving added_items
	repeat with item_ in added_items
		tell application "Finder"
			if kind of item_ is not "folder" then
				set name_ to name of item_
				if name_ begins with "AB" then
					set fol_name to (text 4 thru 10 of name_)
					-- do something - your code here
				else
					if name_ begins with "CD" then
						set fol_name to (text 4 thru 10 of name_)
						-- do something else - your code here
					end if
				end if
			end if
		end tell
	end repeat
end adding folder items to

– Rob

Thanks, Rob. Let me see if I can explain this clearly. The name that’s extracted from the file is a folder name where files that need to be processed are located. What happens is a job runs on another system that creates the directory and files to be processed then FTP’s a text file to the Xserve. The Folder Action will trigger job processing by Quark 6 on the Xserve.

There will be one of two types of jobs to trigger, the type of job is indicated by the first two characters of the text file, the directory where the files are located are the next few characters (4 → 10) of the text file. The location will need to be carried over so the Quark script will open a connection to an SMB share then begin processing files in the directory name extracted from the text file.

When processing is finished, the original text file that triggered script should be moved to an existing directory that will trigger another Folder Action to have Quark print the files created by the first job. The text file should be moved again to another folder that will collect the text files for processed jobs. On the other job type it would just move the file to the final folder as that job is only one phase

Ideally there should be some sort of error checking so that if a process does not complete, it will create a message (text file?) indicating what the error is so it can be checked the following morning as the processing will run over night.

There’s also going to be different numbers of sub-folders and amounts of files in them each time the process runs so that Quark will need to keep going until everything has been processed.

Thanks for the clarification. The code that I offered will check the name of each file and store the extracted folder name in fol_name. It also provides the fork that you need to handle the two scenarios. I don’t have access to Quark or a SMB share so I can’t really offer much help there. If Quark can signal that it has completed a process, it shouldn’t be difficult to generate an error log to track failures.

I’m sorry that I can’t offer more help but almost everything about your script is out of my reach. :?

Good luck!

– Rob

Every bit helps. If any one could post any code for any part or could recommend any reference book, etc., that too would be greatly appreciated.