macOS

PDF Kraken - PDF Splitter

This is a little script I've wrote to help someone to split PDF files into individual PDF's or images.

After the app is build and executed it will ask for a PDF file to split and offer three choices: export as individual PDFs, as JPGs or as PNGs and then it will save the document's pages into individual files to the Desktop.

This script relies on GhostScript to manipulate PDF files. Since GhostScript is not installed on OS X by default, you have to install it first. The easiest way to do that is using brew.

Install Brew

mkdir homebrew && curl -L https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C homebrew

Install GhostScript

brew install ghostscript

Build the App

Open Script Editor and paste the script below in it

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

try
    set pdfFile to choose file with prompt "Select pdf:"
on error errmsg
    quit
end try

set filePath to POSIX path of pdfFile
tell application "System Events"
    set userName to name of current user
end tell

set destFolder to the POSIX path of "/Users/" & userName & "/Desktop/"

tell application "System Events" to tell disk item (pdfFile as text) to set {theName, theExtension} to {name, name extension}
if theExtension is not "" then set theName to text 1 thru -((count theExtension) + 2) of theName

if theExtension is not "pdf" then
   display dialog "© ¤ ¤ ¤ ¤ ¤ Feed me PDFs..." buttons {"Quit"}
   quit
else
   set nPages to do shell script "/usr/local/bin/gs -q -dNODISPLAY -c \"(" & filePath & ") (r) file runpdfbegin pdfpagecount = quit\";"

   if nPages is equal to "1" then
       display dialog "© ¤ ¤ ¤ ¤ ¤ Feed me lots of pages..." buttons {"Quit"}
       quit
   end if

   set mainDialog to (choose from list {"🔸  Export to PDF", "🔹  Export to PNG", "🔹  Export to JPG"} with prompt "  Splitting " & theName & "." & theExtension & "
 " & nPages & " new" & " files will be created" with title "PDF Kraken" OK button name "Split" default items {"🔹  Export to PNG"})

   if mainDialog is false then
       quit
   else

       repeat with mItem in {"🔸  Export to PDF"}
           if mItem is in mainDialog then
               do shell script "/usr/local/bin/gs -sDEVICE=pdfwrite -dNOPAUSE -dQUIET -dBATCH -sOutputFile='" & destFolder & theName & "-%d.pdf' " & "\"" & filePath & "\";"
           end if
       end repeat

       repeat with mItem in {"🔹  Export to PNG"}
           if mItem is in mainDialog then
               do shell script "/usr/local/bin/gs -sDEVICE=png16m -dNOPAUSE -dQUIET -dBATCH -sOutputFile='" & destFolder & theName & "-%d.png' " & "\"" & filePath & "\";"
           end if
       end repeat

       repeat with mItem in {"🔹  Export to JPG"}
           if mItem is in mainDialog then
               do shell script "/usr/local/bin/gs -sDEVICE=jpeg -dNOPAUSE -dQUIET -dBATCH -sOutputFile='" & destFolder & theName & "-%d.jpg' " & "\"" & filePath & "\";"
           end if
       end repeat

       quit

       end if
   end if

Save the Script as Application

Now press CMD + S to save it. Make sure File Format has Application type selected and check the Stay open after run handler box.

That's it! Now this little app can help you split your PDF documents into images or single page PDFs with ease.

Change the App's Icon

  1. Open some image in Preview Press CMD + A to select all the image and CMD + C to copy it.

  2. Select the recently created Eject.app application and press CMD + i to open the information window. Highlight the little manuscript icon by clicking in it and press CMD + V to paste the image. That's it, the change is applied immediately.

Here is the icon I've used:

Kraken Icon