How to Split PDFs Into Individual Pages?

Any ideas on how to split a PDF into its individual pages?

Specifically, I'd like to take a PDF file of a powerpoint presentation and break it into one PDF per slide. (I'm currently using PDFPen Pro, but can use Preview as well.)
Alternatively, any thoughts on how to export all slides to individual PDFs from Powerpoint directly?

Thank you!
Dave
Pennsylvania

In the current version of PDFPen Pro (v 9.0.1), the Scripts menu (the icon to the right of the Help menu) includes an AppleScript called Split PDF. That will do what you’d like. I believe v8 of PDFPen Pro includes this capability as well.

Found it! Thanks, NaOH!

See this post on the Applescript Users List by Shane Stanley.

The script:

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

set inPath to POSIX path of (choose file with prompt "Choose a PDF file:")
set folderPath to POSIX path of (choose folder with prompt "Choose a folder to save the separated pages to.")
its splitPagesInPath:inPath savingInFolder:folderPath

on splitPagesInPath:inPath savingInFolder:folderPath
   --  make URL of the PDF
   set inNSURL to current application's class "NSURL"'s fileURLWithPath:inPath
   -- get name of PDF
   set docName to inNSURL's lastPathComponent()
   -- make URL of output folder
   set outFolderNSURL to current application's class "NSURL"'s fileURLWithPath:folderPath
   -- make PDF document from the file
   set theDoc to current application's PDFDocument's alloc()'s initWithURL:inNSURL
   -- store media bounds of page 1; unnecessary in most cases
   set theBounds to (theDoc's pageAtIndex:0)'s boundsForBox:(current application's kPDFDisplayBoxMediaBox)
   -- count the pages
   set theCount to theDoc's pageCount() as integer
   repeat with i from 1 to theCount
      -- build new document's name
      set newDocName to (its addString:("-" & i) beforeExtensionIn:docName)
      -- make URL for new PDF
      set outNSURL to (outFolderNSURL's URLByAppendingPathComponent:newDocName)
      -- get page of old PDF
      set thePDFPage to (theDoc's pageAtIndex:(i - 1)) -- zero-based indexes
      -- set media bounds if you stored it above
      (thePDFPage's setBounds:theBounds forBox:(current application's kPDFDisplayBoxMediaBox))
      -- make new PDF document
      set theNewPDFDocument to current application's PDFDocument's alloc()'s init()
      -- insert the page
      (theNewPDFDocument's insertPage:thePDFPage atIndex:0)
      -- save the new PDF
      (theNewPDFDocument's writeToURL:outNSURL)
   end repeat
end splitPagesInPath:savingInFolder:

on addString:extraString beforeExtensionIn:aPath
   set aString to current application's NSString's stringWithString:aPath
   set newString to current application's NSString's stringWithFormat_("%@%@.%@", aString's stringByDeletingPathExtension(), extraString, aString's pathExtension())
   return newString as text
end addString:beforeExtensionIn:
1 Like

I have made a macro to do this.
It uses the command line tool Coherent PDF. Just remember you need a license if you use it commercially.

I installed it using Homebrew using these 2 commands.

brew tap oncletom/brew
brew install cpdf

Split pdf Macro (v10.1.1)

Split pdf.kmmacros (5.6 KB)

2 Likes

Hey there,

any chance to start the script with a concrete folder instead of choosing the file and the folder to save the documents? I want to trigger the Macro by getting a new document in the folder. The folder is called:

/Users/carolinejusten/Desktop/Caroline

If there is a new document, I want this to get split by the macro and save the new single documents in an new folder called:

Users/carolinejusten/Desktop/Splits

Any idea?

Thank you guys!

Better still would be to use the currently selected file or folder in Finder.

Hi Again,

I want the AppleScript to run when my Keyboard Maestro macro detects that there is one or more new files in the /Users/carolinejusten/Desktop/Caroline folder. These should then be split by the AppleScript and placed in a different folder. So might be "just" set different paths in the first part of the AppleScript:

set inPath to POSIX path of (choose file with prompt "Choose a PDF file:")
set folderPath to POSIX path of (choose folder with prompt "Choose a folder to save the separated pages to.")
it's splitPagesInPath:inPath savingInFolder:folderPath

But everything I've tried is not working. So my question if anybody could help me to get this done.

Thank you!

Hey Caroline,

Here's how to modify the script to work with a specific file and folder:

--------------------------------------------------------
# Auth: Christopher Stone { Heavy Lifting by Shane Stanley }
# dCre: 2023/03/03 12:21
# dMod: 2023/03/03 12:21 
# Appl: AppleScriptObjC
# Task: Split a Given PDF File into Pages in a Given Directory.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Split, @PDF, @File, @Into, @Pages
--------------------------------------------------------
use scripting additions
use framework "Foundation"
use framework "Quartz" -- required for PDF stuff
--------------------------------------------------------

set targetFilePath to "~/test_directory/pdf_test_files/AppleScript Language Guide – 2013.pdf"
set saveFolderPath to "~/test_directory/Keyboard_Maestro_Test_Folder/SPLIT_PDF/"

set targetFilePath to ((current application's NSString's stringWithString:targetFilePath)'s stringByExpandingTildeInPath) as text
set saveFolderPath to ((current application's NSString's stringWithString:saveFolderPath)'s stringByExpandingTildeInPath) as text

its splitPagesInPath:targetFilePath savingInFolder:saveFolderPath

--------------------------------------------------------
on splitPagesInPath:inPath savingInFolder:folderPath
   --  make URL of the PDF
   set inNSURL to current application's class "NSURL"'s fileURLWithPath:inPath
   -- get name of PDF
   set docName to inNSURL's lastPathComponent()
   -- make URL of output folder
   set outFolderNSURL to current application's class "NSURL"'s fileURLWithPath:folderPath
   -- make PDF document from the file
   set theDoc to current application's PDFDocument's alloc()'s initWithURL:inNSURL
   -- store media bounds of page 1; unnecessary in most cases
   set theBounds to (theDoc's pageAtIndex:0)'s boundsForBox:(current application's kPDFDisplayBoxMediaBox)
   -- count the pages
   set theCount to theDoc's pageCount() as integer
   repeat with i from 1 to theCount
      -- build new document's name
      set newDocName to (its addString:("-" & i) beforeExtensionIn:docName)
      -- make URL for new PDF
      set outNSURL to (outFolderNSURL's URLByAppendingPathComponent:newDocName)
      -- get page of old PDF
      set thePDFPage to (theDoc's pageAtIndex:(i - 1)) -- zero-based indexes
      -- set media bounds if you stored it above
      (thePDFPage's setBounds:theBounds forBox:(current application's kPDFDisplayBoxMediaBox))
      -- make new PDF document
      set theNewPDFDocument to current application's PDFDocument's alloc()'s init()
      -- insert the page
      (theNewPDFDocument's insertPage:thePDFPage atIndex:0)
      -- save the new PDF
      (theNewPDFDocument's writeToURL:outNSURL)
   end repeat
end splitPagesInPath:savingInFolder:
--------------------------------------------------------
on addString:extraString beforeExtensionIn:aPath
   set aString to current application's NSString's stringWithString:aPath
   set newString to current application's NSString's stringWithFormat_("%@%@.%@", aString's stringByDeletingPathExtension(), extraString, aString's pathExtension())
   return newString as text
end addString:beforeExtensionIn:
--------------------------------------------------------

Here's how to get variable data in and out of Keyboard Maestro from AppleScript actions:

set kmInstance to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
   set asVarName to getvariable "local_copiedText" instance kmInstance
   setvariable "local_KM_VarName" instance kmInstance to dataStr
end tell

I'm not going to mess with the folder trigger, because I don't feel well today and have other things to do.

-Chris

1 Like

Here is my attempt to create a macro using the folder trigger.
Like my other macro, it requires the cpdf command-line utility, which can be installed with Homebrew.

Split pdf [Folder trigger] Macro (v10.2)

Split pdf [Folder trigger].kmmacros (6.6 KB)

4 Likes

Thank you guys - you helped a lot!

2 Likes