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.

rename a PDF file using an internal info with Automator

Hi I'm searching a solution to rename a PDF file with a nr that I can find inside of the same PDF.


Can someone help me?


MacBook Pro (2020 and later)

Posted on Jul 8, 2021 6:54 AM

Reply
24 replies

Jul 9, 2021 6:52 AM in response to VikingOSX

Test the following AppleScript on your PDF with that "ddt_2345" string in it. Copy/paste the following into the Script Editor and run it:


use framework "Foundation"
use framework "PDFKit"
use AppleScript version "2.4" -- Yosemite or later
use scripting additions

property NSURL : a reference to current application's NSURL
property PDFDocument : a reference to current application's PDFDocument
property NSLiteralSearch : a reference to current application's NSLiteralSearch
property NSCaseInsensitiveSearch : a reference to current application's NSCaseInsensitiveSearch

set search_mask to (NSLiteralSearch as integer) + (NSCaseInsensitiveSearch as integer)
set search_str to "ddt_2345"

set pdf_file to POSIX path of (choose file of type {"com.adobe.pdf"} default location (path to desktop)) as text

set pdf_url to NSURL's fileURLWithPath:pdf_file
set pdf to PDFDocument's alloc()'s initWithURL:pdf_url
set selection_array to pdf's findString:search_str withOptions:search_mask
set the_text to (selection_array's objectAtIndex:0)'s |string|()
if not (the_text = "" or the_text = missing value) = true then
	display dialog "Captured text is: " & the_text as text with title "Capture Results"
else
	display dialog "Captured text is not found"
end if

return


Jul 9, 2021 7:36 AM in response to edoardo40

This will replace the original PDF name with the captured text string from the PDF, and then rename the original PDF with that string (e.g. foo.pdf becomes ddt_2345.pdf. Tested with macOS 11.4.


However, this does not account for finding that formatted document number when we don't know it in advance. I will need to write a different script that dynamically finds the PDF document number and uses that for document renaming. Will the document number always be on the first page of the PDF?


use framework "Foundation"
use framework "PDFKit"
use AppleScript version "2.4" -- Yosemite or later
use scripting additions

property NSString : a reference to current application's NSString
property NSURL : a reference to current application's NSURL
property PDFDocument : a reference to current application's PDFDocument
property NSLiteralSearch : a reference to current application's NSLiteralSearch
property NSCaseInsensitiveSearch : a reference to current application's NSCaseInsensitiveSearch

# how to bitwise OR integer items like n | m
set search_mask to (NSLiteralSearch as integer) + (NSCaseInsensitiveSearch as integer)
set search_str to "ddt_2345"

set pdf_file to POSIX path of (choose file of type {"com.adobe.pdf"} default location (path to desktop)) as text

set pdf_url to NSURL's fileURLWithPath:pdf_file
set pdf to PDFDocument's alloc()'s initWithURL:pdf_url
set selection_array to pdf's findString:search_str withOptions:search_mask
set the_text to (selection_array's objectAtIndex:0)'s |string|()
if not (the_text = "" or the_text = missing value) = true then
	display dialog "Captured text is: " & the_text as text with title "Capture Results"
	tell application "Finder" to set the name of ((POSIX file pdf_file) as alias) to my change_pdf_name(the_text, ".pdf")
else
	display dialog "Captured text is not found"
end if
return

on change_pdf_name(newtxt, ext)
	return ((NSString's stringWithString:newtxt)'s stringByAppendingString:ext) as text
end change_pdf_name


Jul 9, 2021 8:19 AM in response to VikingOSX

And this AppleScript scans the text of the first PDF page for a pattern that matches the format of the document number you have shown (some characters, an underscore, and some numbers, eg ddt_2345). It returns that document number string and then renames your selected PDF accordingly.


use framework "Foundation"
use framework "PDFKit"
use AppleScript version "2.4" -- Yosemite or later
use scripting additions

property NSString : a reference to current application's NSString
property NSURL : a reference to current application's NSURL
property PDFDocument : a reference to current application's PDFDocument
property NSRegularExpression : a reference to current application's NSRegularExpression
property NSRegularExpressionCaseInsensitive : a reference to current application's NSRegularExpressionCaseInsensitive
property NSMatchingAnchored : a reference to current application's NSMatchingAnchored

set pdf_file to POSIX path of (choose file of type {"com.adobe.pdf"} default location (path to desktop)) as text

set pdf_url to NSURL's fileURLWithPath:pdf_file
set pdf to PDFDocument's alloc()'s initWithURL:pdf_url
# assumption: document number is on first page of the PDF
set page_text to (pdf's pageAtIndex:0)'s |string|()
set docnum_result to my find_document_number(page_text)
if not docnum_result = "No match" then
	tell application "Finder"
		set the name of ((POSIX file pdf_file) as alias) to my change_pdf_name(docnum_result, ".pdf")
	end tell
else
	display dialog "Cannot find document number in PDF."
end if
return

on find_document_number(atxt)
	set tStr to NSString's alloc()'s initWithString:atxt
	set trange to current application's NSMakeRange(0, tStr's |length|())
	# look for document number in format with multiple characters, underline, multiple numbers (e.g. ddt_2345)
	set pattern to "(\\w+_\\d+)"
	set regex to NSRegularExpression's regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive |error|:0
	
	set matches to (regex's firstMatchInString:tStr options:NSMatchingAnchored range:trange)
	
	if not (matches = "" or matches = missing value) is true then
		set matchrange to matches's rangeAtIndex:1
		return (tStr's substringWithRange:matchrange) as text
	else
		return "No match"
	end if
end find_document_number

on change_pdf_name(newtxt, ext)
	return ((NSString's stringWithString:newtxt)'s stringByAppendingString:ext) as text
end change_pdf_name


Tested on macOS 11.4.

Jul 9, 2021 11:25 AM in response to edoardo40

Well, the second one was tested here with the same PDF containing the string ddt_2345, found the first match for that string, and renamed the PDF. The regular expression pattern is assuming the characters to the left of the '_' are alphanumeric, and that the righthand side is only numbers. Is that a wrong assumption for the document numbers you expect? Should both sides of the '_' expect alphanumeric characters?


Do the number of characters that make up the document number vary on either side of the underscore, or is it a fixed character count on either side of it? Knowing that can help exclude other text that may appear with an underscore in it.


The script is only looking on the first page of the PDF for the document number. Wrong assumption?

Jul 11, 2021 5:01 AM in response to edoardo40

As I mentioned, I am only looking on the first page of the PDF for that document number. Can the document number appear on other pages, but not the first page? The pattern I am using should match any document number format you have shown — if it is on the first page of the PDF.


The key to understanding a folder action is that items can be dropped on the folder triggering the action, but no write operation in the folder can occur after the drop or it will trigger the folder action again. You will need to move the dropped PDF to another folder outside of the drop folder renamed with the document number that was found in the PDF.


I can put together an Automator Folder Action using the second example of my posted code above. I need some time to test the folder action with my code and I will post it here later today.



Jul 11, 2021 9:37 AM in response to edoardo40

I have a working and tested Automator Folder action that renames one or multiple dropped PDFs to their included document number and then moves them from the drop folder into a designated external folder. This is because any subsequent write activity within the drop folder causes the folder action to run again.


In your /Applications folder, double-click the Automator application to launch it. On the dialog that appears, select New Document and then select Folder Action before clicking Choose.


Now, you are faced with a large blank central panel. At the top, choose the folder that you want to be your drop folder that the Automator Folder Action is assigned too. It should be on your Desktop for ease of drag and drop of PDF to it.


On the left side of the panel are Automator libraries and you want to click on Utilities, and then drag and drop the Run AppleScript action onto the large central window.


Select the entire default contents of that Run AppleScript action and remove them. Now you can copy/paste my working code below into the empty Automator Run AppleScript action.


Code:


use framework "Foundation"
use framework "PDFKit"
use AppleScript version "2.4" -- Yosemite or later
use scripting additions

property NSString : a reference to current application's NSString
property NSURL : a reference to current application's NSURL
property PDFDocument : a reference to current application's PDFDocument
property NSRegularExpression : a reference to current application's NSRegularExpression
property NSRegularExpressionCaseInsensitive : a reference to current application's NSRegularExpressionCaseInsensitive
property NSMatchingAnchored : a reference to current application's NSMatchingAnchored
property OUTFOLDER : ((path to desktop as text) & "Metadata:") as alias
property ext : ".pdf"

on run {input, parameters}
	# can handle multiple dropped PDFs with document number
	tell application "Finder"
		repeat with apdf in input
			if name extension of (apdf as alias) is "pdf" then
				set pdf_file to POSIX path of (apdf as alias) as text
				set dropfolderPath to my drop_folder_path(pdf_file) as POSIX file as text
				set pdf_url to (NSURL's fileURLWithPath:pdf_file)
				set pdf to (PDFDocument's alloc()'s initWithURL:pdf_url)
				# assumption: document number is on first page of the PDF
				set page_text to (pdf's pageAtIndex:0)'s |string|()
				set docnum_result to my find_document_number(page_text)
				
				if docnum_result contains "_" then
					set newname to docnum_result & ext
					set name of (apdf as alias) to newname
					move (dropfolderPath & newname) to OUTFOLDER with replacing
					
				else
					display dialog "Cannot find document number in PDF."
				end if
			else
				log "continue" # wasn't a PDF so skip it
			end if
		end repeat
	end tell
	return input
end run

on find_document_number(atxt)
	set tStr to NSString's alloc()'s initWithString:atxt
	set trange to current application's NSMakeRange(0, tStr's |length|())
	# look for document number in format with multiple characters, underline, multiple numbers (e.g. ddt_2345)
	# allow for the possibility of a none, or multiple leading space characters if present
	set pattern to "^\\s*?(^\\w+_\\w+)" # ddt_2345
	set regex to NSRegularExpression's regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive |error|:0
	
	set matches to (regex's firstMatchInString:tStr options:NSMatchingAnchored range:trange)
	
	if not (matches = "" or matches = missing value) is true then
		set matchrange to matches's rangeAtIndex:1
		return (tStr's substringWithRange:matchrange) as text
	else
		return "No match"
	end if
end find_document_number

on drop_folder_path(afile)
	# return the drop folder path associated with the newly dropped PDF file
	return do shell script "/bin/zsh -s <<'EOF' - " & afile & "
	# return the path of the dropfolder
	printf '%s' \"${1:a:h}\"
EOF"
end drop_folder_path


I chose a Metadata folder on my Desktop to receive the renamed and moved PDF files. You can change the name to suit your need. The script also checks that the dropped files have a PDF extension on them or they are skipped.


Within that Run AppleScript action, click the hammer icon at the top of it to compile the AppleScript code in the window. Then save the Folder Action. I chose a name: Rename PDF with Document Number. Quit Automator.


You now have that designated drop folder ready to receive PDF. I dropped four of them bearing different internal document numbers and they were all renamed and moved to the external folder as expected.


Tested: macOS 11.4

Jul 12, 2021 5:51 AM in response to edoardo40

On macOS 11.4, this Automator Folder Action worked and moved the dropped (and renamed) files out of the drop folder into the Desktop Metadata folder. I just copied and pasted the most recent (2021-07-11) code above into a blank Automator Run AppleScript action, and saved that folder action with a new name, but with the same drop folder. I then dropped my four PDF test files onto the drop folder and all were subsequently renamed and moved to the Desktop Metadata folder.


The search pattern that I am applying on the first page of the PDF allows for none, one, or more white-space characters to precede the Document Number at the beginning of the text line. Any other characters before the document number and the same line will fail the capture and document rename.


I suggest you start over with a fresh Automator Folder Action and follow my steps carefully.

Jul 12, 2021 9:34 AM in response to edoardo40

A proper folder action has no "Ottiemi elementi del Finder specificati" action in it. Just the Run AppleScript action with the code that I provided. You specify the drop folder and save the Folder Action. These are intended for you to drag and drop files onto the drop folder, from outside of the drop folder, and that behavior triggers the folder action code.


Remove the first action, save the Folder Action, and then drag/drop that same PDF with a document number from another folder location onto the designated Folder Action folder.

Jul 13, 2021 6:19 AM in response to edoardo40

When I include a Get Specified Finder Item and assign it a candidate PDF, then re-enable the Drop Folder, and click the workflow Run button, the same code that I posted on 2021-07-11 works as expected with the test PDF. I cannot duplicate your failure scenario with my test PDFs.


Put the PDF that is failing the Folder Action on a downloadable web location so I can test it to determine why is it is failing. Otherwise, I cannot solve the problem, or help further with code that works for me.

rename a PDF file using an internal info with Automator

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