# Using PDFpen AppleScript to Copy Comments from PDFs

Last week I posted a script for extracting the contents of note annotations from PDFs. An alternative approach uses the comprehensive scripting library of PDFpen.[1] One advantage of this method is that the notes, which inherit from the imprint class, are contained within separate pages, allowing page numbers to be accessed. The AppleScript is as follows:

tell application "PDFpen"
set finalResult to ""
set pageNumber to 0
repeat with aPage in pages of document 1
set pageResult to ""
set pageNumber to pageNumber + 1
repeat with theImprint in imprints of aPage
if the class of theImprint is equal to note then
set theResult to rich text of theImprint
set pageResult to pageResult & "* " & theResult & "

"
end if
end repeat -- imprints
if pageResult is not equal to "" then
set finalResult to finalResult & "page " & (pageNumber as string) & ":

" & pageResult
end if
end repeat -- pages
end tell
set the clipboard to the finalResult


This script can be accessed from inside PDFpen by saving it to /Users/name/Library/Application Support/PDFpen/Scripts, or combined with a Open Finder Items action in Automator to build a Service. Running as a Service will crash the AppleScript above if the PDF has not finished opening. Adding the following code makes the main Applescript wait before trying to process the PDF:

repeat
if document 1 of application "PDFpen" exists then exit repeat
delay 0.5
end repeat


1. I was a little surprised to learn that Preview offers zero AppleScript support out of the box.  ↩