date difference in file renaming

I have some some PDFs that have an isssue or date code for a magazine as their suffix. So “Johnson-Real_LF0607.pdf” is a PDF for a client named Johnson running their real estate ad in Lifestyle magazine in the June 2007 issue. We will reuse this PDF for several months, but will often have to remember what issue it came from. So by the time it runs in August, we should really have renamed the PDF to be “Johnson-Real_LF0807.pdf”. In doing that, we don’t remember where it came from, so we would typically want to do something like “Johnso-R_0607_LF0807.pdf” to show the august date but still retain some of the client name and some of the June info. But we have to do this by hand and with 10 people, they’d all combine the info from both names differently as they cut out part of the client or product part of the code. As a side note, all the PDFs are stored on our server in folders like LF0607 and LF0807.

Spoke with a friend and he suggested we try something that could start adding 1,2,3, etc. to the original month. Thought about that and came up with something like this:

Name the original PDF “Johnson-Real_00_LF0607” to show it was the original. When it comes time to reuse the PDF for August, find the appropriate PDF and drop into the LF0807 folder which would want to rename the end of the PDF to 0807 and in so doing would see the difference between aug and june as 2 and would rename the PDF to “Johnson-Real_02_LF0807.PDF.” So the new PDF is appropriately named for august and shows that it is 2 months from when the original was made, or June. Would be easy to look at when that middle number was a 1,2 or 3, but harder if it was a 9 or something. But perhaps you could create a companion script that when you dropped the new PDF on it could subract the 9 from the new issue month and prompt the user with something as simple as the phrase “February 2007.”


Thanks in advance.

Hi curtis,

what’s about taking the creation date of the original PDF like “Johnson-Real_0607_LF0607”
it can easily retrieved from the file info.

Here is an example to do this.
The folder action handler is commented out but can be used instead of the “hard coded” parameters.
Further error handling is not included

set d to path to desktop as Unicode text
set theFolder to alias (d & "LF0807:")
set theFiles to {d & "Johnson-Real_0000_LF0607.PDF" as alias}

-- on adding folder items to theFolder after receiving theFiles
tell name of (info for theFolder) to set {curFolderMn, curFolderYr} to {text 3 thru 4, text 5 thru 6}
repeat with theFile in theFiles
	-- get some file info (the splitted file name and creation date) 
	tell (info for theFile) to set {{fName, a, orig, b, fDate}, cDate} to {words of (name as text), creation date}
	-- calculate creation date string (mmyy)
	tell cDate to set origDate to (text -2 thru -1 of ("0" & (its month as integer) as string)) & text 3 thru 4 of (its year as string)
	-- get month, year of file name string
	tell fDate to set {fMn, fYr} to {text 3 thru 4, text 5 thru 6}
	-- change only, if necessary (in case of a folder action this avoids infinite loop of renaming)
	if curFolderMn as integer is not equal to fMn as integer then
			-- create new file name string
			set newName to fName & a & origDate & b & "LF" & curFolderMn & curFolderYr & ".pdf"
			tell application "Finder" to set name of theFile to newName
		end try
	end if
end repeat
-- end adding folder items to

btw, interesting not(ic)e about different behavior of the text range keyword word:

words of ("Johnson-Real_0000_LF0607.PDF" as Unicode text) --> {"Johnson", "Real_0000_LF0607", "PDF"}
words of ("Johnson-Real_0000_LF0607.PDF" as string) --> {"Johnson-Real", "_", "0000", "_", "LF0607", "PDF"}