Sequential file naming

I have an AppleScript that is connected to Mail Rule. At my office, I receive emails from a reference laboratory every day with patient test results; the results are in PDF form. The script extracts the subject of the email which includes patient name, laboratory number, and the term Lab Results. For simplicity, I just take all the terms and that becomes the new name of the attachment that is subsequently saved to a specific folder. The folders are created day by day as necessary. The issue is that every once in a while, I get an updated report on the same day as the original report, so the previous file is over-written, as it has the same name (the laboratory number is assigned based on the day the sample arrives and does not change as updates are posted.) I tried this handler:

----------------------------------------------------------------------
to MakeNextFilename(root_text, dp)
	tell application "Finder" to set curr_filenames to the name of every file in folder dp whose name contains root_text
	if (curr_filenames's length) > 0 then set root_text to (root_text & ((curr_filenames's length) as text))
	return root_text
end MakeNextFilename
----------------------------------------------------------------------

where root_text is the string with all the data (name, number, Lab Results) and dp is the destination folder where the file is to be saved. This handler does not work at all; no files are saved with this, regardless of the existence of a previous file, or even in an empty folder.

Ideally, I am looking for something that will examine the current contents of the folder, and determine the existence of a file with the same name pattern and if it is found, create a new string with a 1 or a 2 added, depending on how many files are already present.

I hope this is a clear explanation.

Hi Craig,

once I wrote a script to save mail attachments just for this purpose.
Hope it helps


using terms from application "Mail"
	on perform mail action with messages The_Messages
		set dt to path to desktop
		set Save_folder to (dt as text) & "Incoming PDF:"
		repeat with This_Message in The_Messages
			repeat with oneAttachment in mail attachments of This_Message
				set Nm to name of oneAttachment
				if Nm ends with ".PDF" then
					set fName to my checkFileName(Save_folder, Nm)
					save oneAttachment in fName
				end if
			end repeat
		end repeat
	end perform mail action with messages
end using terms from

on checkFileName(fDir, fName)
	try
		set f to (fDir & fName) as alias
		set {name:Nm, name extension:Ex} to info for f
		if Ex is missing value then set Ex to ""
		if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
		set idx to 0
		repeat
			set idx to idx + 1
			set checkName to (fDir & Nm & "_" & (idx as string) & "." & Ex)
			try
				checkName as alias
			on error
				return checkName
			end try
		end repeat
	on error
		return (fDir & fName)
	end try
end checkFileName

Wordy, but logical. Thank you, Stefan. I put in your handler and have changed the rule to use that script. I will most likely have a report next week; very little lab data coming in today.