Finder append names to PDFs

I’m trying to create a script that will allow me to choose from a list of 6 different names (Aproof, Bproof, Cproof, etcetera), and depending on what was chosen append the chosen name to a group of PDFs.

For example, if the PDF names are: XX_01.pdf, XX_02.pdf, XX_03.pdf & the chosen name from the list is Aproof then append that to the PDFs XX-01-Aproof.pdf, XX-02-Aproof.pdf, XX_03-Aproof.pdf. I know Automator or Adobe Bridge can easily do that, but I would like to learn how to do it in Applescript. This is what I have so far, but I need your expertise to finalize it.

Thanks in advance.

set source_folder to choose folder with prompt "Select folder with Pdfs"
tell application "Finder" to set theFiles to files of source_folder whose name extension is "pdf"

if (count of theFiles) is 0 then
   display dialog "No pdfs found" buttons "Cancel" default button "Cancel" with icon 0 giving up after 6
   return
end if

set the_choice to {"Aproof", "Bproof", "Cproof", "Dproof", "Eproof", "Fproof"}
set the_choiceFav to choose from list the_choice with title "" with prompt "Select a proof name to append to the pdfs"

if the_choiceFav is false then
   error number -128 (* user cancelled *)
   
else if item 1 of the_choiceFav is "Aproof" then
   --append or add _Aproof to the PDF
   --example original pdf name is 12345.pdf will be 12345_Aproof.pdf
   
else if item 1 of the_choiceFav is "Bproof" then
   --append or add _Bproof to the PDF
else if item 1 of the_choiceFav is "Cproof" then
   --append or add _Cproof to the PDF
else if item 1 of the_choiceFav is "Dproof" then
   --append or add _Dproof to the PDF
else if item 1 of the_choiceFav is "Eproof" then
   --append or add _Eproof to the PDF
else if item 1 of the_choiceFav is "Fproof" then
   --append or add _Fproof to the PDF
end if

display alert "All done! Proof names added "

macaria. Welcome to the forum. My suggestion:

set source_folder to (choose folder with prompt "Select folder with Pdfs")
tell application "Finder" to set theFiles to (files of source_folder whose name extension is "pdf")
if (count theFiles) is 0 then
	display dialog "No pdfs found" buttons "Cancel" default button "Cancel" with icon 0 giving up after 6
	error number -128
end if

set the_choice to {"Aproof", "Bproof", "Cproof", "Dproof", "Eproof", "Fproof"}
set the_choiceFav to choose from list the_choice with title "" with prompt "Select a proof name to append to the pdfs" default items (item 1 of the_choice)
if the_choiceFav is false then error number -128 -- user cancelled

tell application "Finder"
	repeat with aFile in theFiles
		set aFileName to name of aFile
		set newFileName to (text 1 thru -5 of aFileName) & "-" & the_choiceFav & ".pdf"
		set name of aFile to newFileName
	end repeat
end tell

display alert "All done! Proof names added"

1 Like

Thank you @peavine for your quick response and help. I see the results and differences in the Applescript application, but I do not see the PDFs being physically renamed.

I misunderstood what you wanted to do. I’ve revised my script in my earlier post. Please test it on duplicate files just to make sure it works as desired.

@peavine That worked. THANK YOU!

1 Like

@peavine
My bad. I didn’t realize that if the script is run again, it will continue appending names to the PDFs from the choice list. Would it be possible to stop that from happening?

macaria. Give the following a try. It skips all files whose file name without the extension already ends with “proof”.

set source_folder to (choose folder with prompt "Select folder with Pdfs")
tell application "Finder" to set theFiles to (files of source_folder whose name extension is "pdf")
if (count theFiles) is 0 then
	display dialog "No pdfs found" buttons "Cancel" default button "Cancel" with icon 0 giving up after 6
	error number -128
end if

set the_choice to {"Aproof", "Bproof", "Cproof", "Dproof", "Eproof", "Fproof"}
set the_choiceFav to choose from list the_choice with title "" with prompt "Select a proof name to append to the pdfs" default items (item 1 of the_choice)
if the_choiceFav is false then error number -128 -- user cancelled

tell application "Finder"
	repeat with aFile in theFiles
		set aFileName to name of aFile
		set baseFileName to text 1 thru -5 of aFileName
		if baseFileName does not end with "proof" then
			set newFileName to baseFileName & "-" & the_choiceFav & ".pdf"
			set name of aFile to newFileName
		end if
	end repeat
end tell

display alert "All done! Proof names added"
1 Like

Thank you @peavine that worked too.

1 Like