You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

finder add names

I’m trying to create a script that will allow me to choose from a list of 6 different names (Aproof, Bproof, Cproof etc.) 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


and the chosen name from the list is Aproof then append that to


XX-01-Aproof.pdf,

XX-02-Aproof.pdf

XX_03-Aproof.pdf.


I know Automator or Adobe Bridge can easily do this, but I would like to do it in Applescript. This is what I have so far, but I need your expertise to finalize it. Thanks in advance.


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 "


Posted on May 10, 2023 3:24 PM

Reply
Question marked as Top-ranking reply

Posted on May 13, 2023 4:01 PM

Your example script had smart quotes in it and AppleScript won't compile until you make these straight quotes.


Try this code that I tested on macOS Ventura 13.3.1 (a).


use scripting additions

property PDF : ".pdf"

tell application "Finder"
	set theFiles to (every file of folder (choose folder) whose name extension is in {"pdf", "PDF"}) as alias list
	
	set the_choice to {"Aproof", "Bproof", "Cproof", "Dproof", "Eproof", "Fproof"}
	set the_choiceFav to (choose from list the_choice with prompt "Select a proof name to apend to the PDFs")
	
	if the_choiceFav is false then return
	
	set the_index to (my getpositionOfItemInList(the_choiceFav, the_choice)) as integer
	
	repeat with apdf in theFiles
		set xpdf to name of apdf
		set appendStr to (item the_index of the_choice)
		set name of apdf to (my appendString:appendStr toPDF:xpdf withExtension:PDF)
	end repeat
end tell
return

on getpositionOfItemInList(theItem, theList)
	-- return the index of the item in the list
	-- eliminate the processing intense if/else cascade
	repeat with a from 1 to count of theList
		if item a of theList contains theItem then return a
	end repeat
	return 0
end getpositionOfItemInList

to appendString:astr toPDF:apdf withExtension:ext
	set basename to text 1 thru ((offset of ".pdf" in apdf as text) - 1) of (apdf as text)
	return (basename & "_" & astr & ext) as text
end appendString:toPDF:withExtension:


For a PDF with a name XX_string.pdf, it is renamed XX_string_Aproof.pdf if the_choiceFav was "Aproof".



Similar questions

10 replies
Question marked as Top-ranking reply

May 13, 2023 4:01 PM in response to macaria-valamia

Your example script had smart quotes in it and AppleScript won't compile until you make these straight quotes.


Try this code that I tested on macOS Ventura 13.3.1 (a).


use scripting additions

property PDF : ".pdf"

tell application "Finder"
	set theFiles to (every file of folder (choose folder) whose name extension is in {"pdf", "PDF"}) as alias list
	
	set the_choice to {"Aproof", "Bproof", "Cproof", "Dproof", "Eproof", "Fproof"}
	set the_choiceFav to (choose from list the_choice with prompt "Select a proof name to apend to the PDFs")
	
	if the_choiceFav is false then return
	
	set the_index to (my getpositionOfItemInList(the_choiceFav, the_choice)) as integer
	
	repeat with apdf in theFiles
		set xpdf to name of apdf
		set appendStr to (item the_index of the_choice)
		set name of apdf to (my appendString:appendStr toPDF:xpdf withExtension:PDF)
	end repeat
end tell
return

on getpositionOfItemInList(theItem, theList)
	-- return the index of the item in the list
	-- eliminate the processing intense if/else cascade
	repeat with a from 1 to count of theList
		if item a of theList contains theItem then return a
	end repeat
	return 0
end getpositionOfItemInList

to appendString:astr toPDF:apdf withExtension:ext
	set basename to text 1 thru ((offset of ".pdf" in apdf as text) - 1) of (apdf as text)
	return (basename & "_" & astr & ext) as text
end appendString:toPDF:withExtension:


For a PDF with a name XX_string.pdf, it is renamed XX_string_Aproof.pdf if the_choiceFav was "Aproof".



May 18, 2023 10:09 AM in response to VikingOSX

@VikingOSX

Apologies. I got sick & couldn't respond as quickly as you did.


Your first script is doing exactly what I asked. If I need to run it again on the same PDFs because the wrong name was selected from the list then the script will continue to append the chosen name from the list. I just need to be more careful to select the correct name.


I would like to make a modification. Would it possible to select multiple pdfs within a folder instead of an entire folder with pdfs?

May 13, 2023 4:45 PM in response to VikingOSX

The following code revision will prevent you from multiple appendages such as XX_string_Aproof_Eproof.pdf because it counts the underscores where more than one causes it to skip the current file.


use scripting additions

property PDF : ".pdf"

tell application "Finder"
	set theFiles to (every file of folder (choose folder) whose name extension is in {"pdf", "PDF"}) as alias list
	
	set the_choice to {"Aproof", "Bproof", "Cproof", "Dproof", "Eproof", "Fproof"}
	set the_choiceFav to (choose from list the_choice with prompt "Select a proof name to apend to the PDFs")
	
	if the_choiceFav is false then return
	
	set the_index to (my getpositionOfItemInList(the_choiceFav, the_choice)) as integer
	
	repeat with apdf in theFiles
		set xpdf to name of apdf
		if my charcount(xpdf, "_") = 1 then
			set appendStr to (item the_index of the_choice)
			set name of apdf to (my appendString:appendStr toPDF:xpdf withExtension:PDF)
		else
			log "already has appended name string"
		end if
	end repeat
end tell
return

on getpositionOfItemInList(theItem, theList)
	-- return the index of the item in the list
	-- eliminate the processing intense if/else cascade
	repeat with a from 1 to count of theList
		if item a of theList contains theItem then return a
	end repeat
	return 0
end getpositionOfItemInList

to appendString:astr toPDF:apdf withExtension:ext
	set basename to text 1 thru ((offset of ".pdf" in apdf as text) - 1) of (apdf as text)
	return (basename & "_" & astr & ext) as text
end appendString:toPDF:withExtension:

on charcount(afile, achar)
	set cnt to (do shell script "tr -cd " & achar & " <<<" & afile & " | wc -c")
	return (cnt) as integer
end charcount


May 15, 2023 12:17 PM in response to VikingOSX

Hi VikingOSX,

That's a good catch. In the first script you posted, the names are appended. I didn't think about how the script would keep appending if I ran it again. I tested with your revised code, which prevents multiple appendages but it is not appending any names. Additionally, it would also be possible to choose a single PDF from a folder rather than all the PDFs in the folder?


May 15, 2023 12:43 PM in response to macaria-valamia

I have 50 PDF (e.g. XX_001.pdf … XX_050.pdf) in a folder named PDF on my Desktop. When I run the second revised script that I copy/pasted from the previous post into Script Editor and run it choosing that PDF folder, and Aproof from the list, I have XX_001_Aproof.pdf … XX_050_Aproof.pdf in the PDF folder. Running it a second time does not alter the names of the first run.


Based on the previous paragraph, I don't know what you mean about the revised code not appending any names.


With further smaller code revision, I can prompt you to choose a folder, then open a Finder window on that folder contents, where it loops until you click on a single PDF document. It gets that filename and then closes the open Finder window, before continuing the familiar append processing on that selected PDF document. Is that what you want?

May 18, 2023 11:14 AM in response to macaria-valamia

The following script will allow you to select the folder containing the PDFs and then within that folder select one or more PDFs for processing. This script will not apply multiple appends to previously processed PDF files. Tested on macOS Ventura 13.3.1 (a).


use scripting additions

property PDF : ".pdf"

set theFolder to (choose folder default location (path to desktop))
set theFiles to (choose file of type {"com.adobe.pdf"} default location theFolder with multiple selections allowed)
set the_choice to {"Aproof", "Bproof", "Cproof", "Dproof", "Eproof", "Fproof"}
set the_choiceFav to (choose from list the_choice with prompt "Select a proof name to apend to the PDFs")

if the_choiceFav is false then return

set the_index to (my getpositionOfItemInList(the_choiceFav, the_choice)) as integer
tell application "Finder"
	repeat with apdf in theFiles
		set xpdf to name of apdf
		if my charcount(xpdf, "_") = 1 then
			set appendStr to (item the_index of the_choice)
			set name of apdf to (my appendString:appendStr toPDF:xpdf withExtension:PDF)
		else
			log "already has appended name string"
		end if
	end repeat
end tell
return

on getpositionOfItemInList(theItem, theList)
	-- return the index of the item in the list
	-- eliminate the processing intense if/else cascade
	repeat with a from 1 to count of theList
		if item a of theList contains theItem then return a
	end repeat
	return 0
end getpositionOfItemInList

to appendString:astr toPDF:apdf withExtension:ext
	set basename to text 1 thru ((offset of ".pdf" in apdf as text) - 1) of (apdf as text)
	return (basename & "_" & astr & ext) as text
end appendString:toPDF:withExtension:

on charcount(afile, achar)
	set cnt to (do shell script "tr -cd " & achar & " <<<" & afile & " | wc -c")
	return (cnt) as integer
end charcount

finder add names

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.