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.