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