Here is the revision that handles your document number in the form OOO_nnnnn and does the rename without the "Cannot find Document number" dialog. I am using a drop folder named DropMe and a recipient folder named Renamed — for testing purposes. You may want to change that Renamed folder property to your preferred recipient folder. This code is expecting the document number aligned left on the first line, but probably would find it in that location anywhere in the document's first page.
Tested on macOS Monterey 12.6.1.
The Run AppleScript content:
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) & "Renamed:") 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+?([A-Z0-9_]+)\\s+"
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