Finding most recent proof file

I am trying to add functionality to my already existing script.

tell application "Adobe InDesign CS6"
	activate
	
	set myText to name of document 1 as string
	set myJobNumber to (characters 1 thru 6 of myText) as string
	set myDesktopFolder to path to desktop as string
	set myFile to myDesktopFolder & "OUT:" & myJobNumber & "proof.pdf" as string
	set properties of PDF export preferences to {export layers:true, generate thumbnails:true, pdf magnification:fit page, pdf page layout:single page}
	export document 1 format PDF type to myFile without showing options
	close document 1 saving yes
	
end tell

All this does is save my current opened file to a PDF named by the first 6 characters of the file name. What I would like to do next, is for the script to see if there is a XXXXXXproof.pdf file in the folder first. If there is one already, then the file name saved should be XXXXXXproof1.pdf. The trick here is that there might be 4 proof files, so I need to do something like:


tell application "Finder"
	
	--find all the proof files
	set myList to name of every file in myFolder whose name contains "PROOF"
	
	--check to see if the list is empty
	--if it is empty, then the are no proofs yet
	if last item of myList is "" then
		set myFileName to "123456proof.pdf"
	else
		-- the list wasn't empty and there is a proof in there
		-- if the proof name ends in an F, then the next proof is proof1
		-- i don't know the correct syntax here:
		set myVersion to "the fifth character from the end of the last item of myList"
		if myVersion = "f" then
			set myFileName to "123456proof1.pdf"
		else
			--otherwise, there are multiple proofs in the folder, and the next
			--proof number should be incremented by 1
			set myNextVersion to myVersion + 1
			set myFileName to "123456proof" & myNextVersion & ".pdf"
		end if
		
	end if
	
end tell




which will give me a list of ALL the proofs that have been created. Then, if myList is not empty, I can look at the last item and see what the 5th character from the end is. If it is “f”, then my file name gets a 1, if it is a number (like 1, 2, 3, 4, etc) then I can increment that number by 1 and that’s my new proof name. Does this make sense? Is there an easier way to do this?

the names of files in my proofing folder might be something like this:

123456art.pdf
123456bc.pdf
123456proof.pdf
123456proof1.pdf
123456proof2.pdf
123456sheet.pdf

Can someone guide me along the road please.
thanks
David

Hi David,

I have added a routine to your script that will increment the proof number if a pdf already exists.

tell application "Adobe InDesign CS6"
	activate
	
	set myText to name of document 1 as string
	set myJobNumber to (characters 1 thru 6 of myText) as string
	set myDesktopFolder to path to desktop as string
	set myFile to my checkExists(myDesktopFolder, myJobNumber)
	set properties of PDF export preferences to {export layers:true, generate thumbnails:true, pdf magnification:fit page, pdf page layout:single page}
	export document 1 format PDF type to myFile without showing options
	close document 1 saving yes
	
end tell

on checkExists(myDesktopFolder, myJobNumber)
	set fileExists to true
	set theIncrement to ""
	set myFile to myDesktopFolder & "OUT:" & myJobNumber & "proof.pdf" as string
	
	repeat until fileExists is false
		set fileStatus to do shell script "if [ -f " & quoted form of POSIX path of myFile & " ]; then echo true;else echo false; fi;" --> check a file exists
		if fileStatus is "false" then
			set fileExists to false
		else
			set theIncrement to theIncrement + 1
			set myFile to myDesktopFolder & "OUT:" & myJobNumber & "proof" & theIncrement & ".pdf" as string
		end if
	end repeat
	return myFile
end checkExists

Hope this helps.

Thanks,
Nik