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
Question marked as Top-ranking reply

Posted on Jul 9, 2021 8:19 AM

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.

24 replies

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 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.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

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.