I have a problem that I found a partial solution to.

Daily, I daily search for a list of words within a pdf document. Then, I need to save each matching page in a separate file, renamed to "same name as original file - word found - page #"

I found the script below that does almost all of those things but asks for an input of the searched word.

What I need is to change this so that the list of words is contained within the script in the code.

use scripting additions
use framework "Foundation"
use framework "Quartz" -- required for PDF stuff

--property theKey : ""

#===== Handlers

-- Supposed to create a new PDF file for every page from the passed PDF file which contains the key string.

on splitPDF:thePath forKey:theKey
   set inNSURL to current application's |NSURL|'s fileURLWithPath:thePath
   set thePDFDocument to current application's PDFDocument's alloc()'s initWithURL:inNSURL
   # CAUTION. theList contain indexes of pages numbered starting from 1, but ASObjC number them starting from 0
   set theCount to thePDFDocument's pageCount() as integer
   repeat with i from 1 to theCount
       set thePDFPage to (thePDFDocument's pageAtIndex:(i - 1)) # ?????
       set itsText to (thePDFPage's |string|()) as text
       if itsText contains theKey then
           set newPath to (its addString:("-" & theKey & " -page " & text -2 thru -1 of ((100 + i) as text)) beforeExtensionIn:thePath)
           set outNSURL to (current application's |NSURL|'s fileURLWithPath:newPath)
           set newPDFDoc to current application's PDFDocument's alloc()'s init()
           (newPDFDoc's insertPage:thePDFPage atIndex:0)
           (newPDFDoc's writeToURL:outNSURL)
       end if
   end repeat
end splitPDF:forKey:

-- inserts a string in a path before the extension
on addString:extraString beforeExtensionIn:aPath
   set pathNSString to current application's NSString's stringWithString:aPath
   set newNSString to current application's NSString's stringWithFormat_("%@%@.%@", pathNSString's stringByDeletingPathExtension(), extraString, pathNSString's pathExtension())
   return newNSString as text
end addString:beforeExtensionIn:

#===== Caller

set theKey to text returned of (display dialog "Enter the key to search for:" default answer "Manang Saling")

set thePath to POSIX path of (choose file with prompt "Choose a PDF file." of type {"PDF"})
its splitPDF:thePath forKey:theKey

1 Answer 1


Replace your Caller code with this:

set keyList to {"multiple", "nnum"}
set thePath to POSIX path of (choose file with prompt "Choose a PDF file." of type {"PDF"})
repeat with theKey in keyList
    (its splitPDF:thePath forKey:theKey)
end repeat

Essentially, you need to make a list of search terms ('keyList') and then loop through that with the handler.

NB I know nothing about ASObjC code but I find that the file name does not have the search term added (either before or after my suggested edit). Perhaps someone knowledgeable on the subject can provide some insight.

Also, I get a syntax error with the backticks in your last line so I removed them. Dunno what effect that may have had.

  • 1
    It works! Thanks! Yes, the ticks were a mistake. About the file name I was able to do the following change: set newPath to (its addString:("-" & theKey & " -page " & text -2 thru -1 of ((100 + i) as text)) beforeExtensionIn:thePath) Feb 2 at 2:54
  • 1
    @PaoloMartinez I'm glad to hear it. If it answered your question, please consider accepting. Regards.
    – Mockman
    Feb 2 at 3:44
  • one problem solved starts 10 new ones... lol... everything works perfectly except the pdf produced by the script are not searchable. I don't know if the problem is with the original file (witch is fully searchable), or because its in portuguese or something else. Any ideas? Feb 2 at 4:36
  • @PaoloMartinez Sorry but no idea. I'm not familiar with the code that handles that part. For testing PDFs, I saved a man page to text and then printed that to a PDF. Searchability is maintained in the newly-exported PDFs. You might try that — if such a file works for you then it might be an issue with your PDFs. Also, try posting where you found the original script (which I think is on macscripter) and see if anyone has any suggestions. Several regulars are knowledgeable on ASObjC.
    – Mockman
    Feb 2 at 17:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .