InDesign to PDF Automation

A couple of years ago, we needed an easy way for our editors to create PDF files from InDesign documents. This script allows users to drag over InDesign documents for PDF creation. The script arranges pages in string order so each page needs to be named 01XXXX or 02XXXXX. If you name a page 1XXXX, it will be arranged with page 10XXXX, and so forth.

This script uses Acrobat to create the PDFs. Earlier attempts using the onboard PDF creation capabilities of Leopard & Snow Leopard proved incapable of rendering transparencies correctly.

This script asks if the user wants spreads or single pages or a specific print preset.

It was my first AppleScript so I have little doubt that major improvements could be made.

Thanks to Chris Paveglio at, the author of PDF Bee, for providing the Acrobat part of the script that does the heavy lifting.

-- ****************************************************************************
-- Version 2.3
-- Drew Grgich
-- McMurry
-- * Acrobat related Applescript contributed by Chris Paveglio *
-- * Author of PDF Bee *
-- * *
-- 2.3 - Removed property type_list; we now work with all indd files; remove interactive pdf features
-- 2.2 - cleaned up some variable names, moved global definitions to a better grouping
-- 1.1 - added ability to choose file location, spreads/no spreads, combined/single
-- ****************************************************************************

-- We want this script to only work with INDD files or folders of INDD files for now
-- We're going to check file extensions
property extension_list : {"indd"}

-- Our globals for this fine script
global PDFPath

-- Let's define variables to determine if we are going to use spreads, single pages, or a specific print preset
global spreadsChoice
global combinedChoice
global presetChoice

-- Let's define combinedPDFFileName as the name of the combined PDF at the end.
-- We want to determine this early in the process and refer to it late in the process
global combinedPDFFileName
global singlesFolder
global numberOfPDFs

on run
	display dialog "I don't do too much unless you drag InDesign files on top of me."
end run

on open filesToConvert
	-- Let's set our print preset variables
	-- Because I'm not using AppleScript Studio to make a GUI,
	-- we'll have to set these variables with questions
	-- Spreads
	set spreadsChoice to (display dialog "Do you want spreads?" buttons {"You Betcha", "Nope"} default button 2)
	-- Combined file or singles?
	set combinedChoice to (display dialog "Combined PDF or Single PDF Files?" buttons {"Combined", "Singles"} default button 1)
	-- Chosen Print Preset
	set presetChoice to (display dialog "Which Print Preset do you want?" buttons {"Smallest File Size", "High Quality Print"} default button 1)
	-- Let's name our combined PDF file if the user wants a combined PDF
	-- or pick/create a folder for single files if these are requested
	if button returned of combinedChoice = "Combined" then
		set comboORSingles to "combo"
		set combinedPDFFileAlias to choose file name with prompt "Where would you like to save the combined PDF?" default location (path to desktop folder) default name "Combined.pdf"
		set combinedPDFFileName to combinedPDFFileAlias as string
	else if button returned of combinedChoice = "Singles" then
		set comboORSingles to "singles"
		set singlesFolderAlias to choose folder with prompt "Where would you like to save your individual PDFs?" default location (path to desktop folder)
		set singlesFolder to singlesFolderAlias as string
	end if
	-- make a folder for us to store PDFs in for compilation with Chris' script
	-- this will be a custom subroutine
	-- now let's loop through whatever was dropped here and process it all individually
	-- note that InDesign files or folders containing InDesign files can be dragged here
	-- we'll process everything individually and ignore any non-InDesign files
	-- you can even drop nested folders here
	-- everything is combined at the end into one big file ordered by filename
	repeat with i from 1 to the count of filesToConvert
		set this_item to item i of filesToConvert
		set the item_info to info for this_item
		-- we'll need a few custom subroutines to do the magic
		-- starting with this one here
		do_processing(this_item, item_info)
	end repeat
	-- We need to determine if no InDesign files get converted to PDFs so we can catch an error before it happens
	tell application "Finder"
		set numberOfPDFs to count of (files in folder PDFPath)
	end tell
	if numberOfPDFs = 0 then
		display dialog "No PDFs to process - there appears to have been a problem with one of your INDD files." with icon 1 buttons {"Ponder This Quandry"}
	end if
	-- Finally, do we want to make combined files or copy some of our PDFs to a designated folder?
	if comboORSingles = "combo" then
	else if comboORSingles = "singles" then
	end if
end open

-- a subroutine to make a Temp folder with the defined name
-- This folder is placed in the User:Library:Caches:TemporaryItems folder
-- If created here initially, the folder will of course be empty
--If the folder already exists, all files inside of it are moved to the Trash
--Either way, the folder will be empty and PDFification can begin
on makeTempFolder(theNewFolderName)
	set tempPath to (path to temporary items folder from user domain)
	tell application "Finder"
		if not (exists folder theNewFolderName of tempPath) then
			make new folder at tempPath with properties {name:theNewFolderName}
		end if
		display dialog "This script is now emptying its PDF Storage Folder. You may hear a 'Move to Trash' sound." buttons {"Groovy"} default button 1 giving up after 3
		delete items of folder theNewFolderName of tempPath
	end tell
	-- Here's where we define where we're going to store our files
	set PDFPath to (tempPath as string) & theNewFolderName & ":"
end makeTempFolder

-- a subroutine to handle determining if our dropped items are folders or files
on do_processing(this_item, item_info)
	if folder of the item_info is true then
		-- Here's now where we verify that we are working with InDesign files only
	else if (the name extension of the item_info is in the extension_list) then
	end if
end do_processing

-- a subroutine to process folders
on process_folder(this_folder)
	set these_items to list folder this_folder without invisibles
	repeat with i from 1 to the count of these_items
		set this_item to alias ((this_folder as text) & (item i of these_items))
		set the item_info to info for this_item
		do_processing(this_item, item_info)
	end repeat
end process_folder

-- a subroutine to process individual items
on process_item(this_item)
	-- First, we need to know the file name of each item
	-- The PDF will be named to match the file name of the InDesign file
	-- I'm using Finder for this for now as I don't know how to do this in InDesign and I'm sleepy as I write this
	tell application "Finder"
		-- make 'myName' the name of the item we're working with now
		set myName to name of this_item
		-- make clipPoint the numeric position of the dot in the file name before the extension
		set clipPoint to offset of "indd" in myName
		-- count up to the dot and make everything before it the now trimmed file name
		-- example4325.indd nows becomes the trimmedName string "example4325"
			set trimmedName to (characters 1 thru (clipPoint - 1) of myName) as string
		on error
			display dialog "I am not able to read the filename correctly" with icon 0
		end try
	end tell
	-- And now the PDFication begins . . .
	tell application "Adobe InDesign"
		-- let's ignore modal warnings
		set «class UIAc» of «class pScr» to «constant elnteNvr»
		set myDocument to open this_item
		-- PDF Preferences creation time!
		-- These will be based on the choices made at the top of the script
		-- We'll start by dumping a premade pref list to our own list of preferences
		-- If the user wants spreads, we'll add that to the list of preferences
		-- We'll then export the PDFs using this set of preferences
		-- I've tried to pull these out into a subroutine that I call at the beginning
		-- to create a preset. The trouble is deleting the preset once I make it.
		-- I don't want to leave junk around!
		-- I'm thus looping this creating process through for each file . . .
		-- . . . not the most efficient!
		-- pick preset quality and use it to define our PDF export prefs
		if button returned of presetChoice = "Smallest File Size" then
			set theProps to «class qpro» of «class PFst» "[Smallest File Size]"
		else if button returned of presetChoice = "High Quality Print" then
			set theProps to «class qpro» of «class PFst» "[High Quality Print]"
		end if
		set «class qpro» of «class DFpf» to theProps
		-- pick spreads or no spreads
		if button returned of spreadsChoice = "You Betcha" then
			set «class rdsF» of «class DFpf» to true
		else if button returned of spreadsChoice = "Nope" then
			set «class rdsF» of «class DFpf» to false
		end if
		-- Let's do the whole range (thanks, Erica!)
		set «class pcty» of «class DFpf» to «constant prngprna»
		-- We now need to make sure this isn't exported
		-- as an interactive PDF
		set «class IAEo» of «class DFpf» to «constant IEOsDNIc»
		«event K2  expt» myDocument without «class imot» given «class exft»:«constant eXftt_PD», «class kfil»:(PDFPath & trimmedName & "pdf")
		«event CoReclos» myDocument
	end tell
end process_item

-- let's combine the PDFs now
-- this piece courtesy of Chris Paveglio
-- author of PDF Bee
on combine_PDFS(theFolder)
	-- We know where we're storing the PDFs
	-- This can be changed for further customization
	set theFolder to PDFPath
	set fileNames to list folder theFolder without invisibles -- all files 
	set firstFile to (theFolder as string) & (item 1 of fileNames) -- complete path
	tell application "Adobe Acrobat"
		open firstFile with «class nvis»
		repeat with m from 2 to count of items of fileNames
			set theFile to (theFolder as string) & item m of fileNames -- complete path 
				set countPages to count each «class cpag» of document (item 1 of fileNames)
				open theFile with «class nvis»
				--display dialog theFile
				set countPages2 to count each «class cpag» of document (item m of fileNames)
				«event CAROinpg» document (item 1 of fileNames) given «class srdc»:document (item m of fileNames) ¬
					, «class stpg»:1, «class nmpg»:countPages2, «class inaf»:countPages
				close document (item m of fileNames) saving no
			end try
		end repeat
		save document (item 1 of fileNames) in file combinedPDFFileName with «class svln»
		display dialog "Combination Complete!" with icon 1 buttons {"Super!"} default button 1 giving up after 3
	end tell
end combine_PDFS

on copy_files(theFolder)
	repeat with i from 1 to numberOfPDFs
		tell application "Finder"
			set fileToCopy to the item i in folder PDFPath
			duplicate fileToCopy to singlesFolder with replacing
		end tell
	end repeat
	display dialog "Your PDF files await your perusal" with icon 1 buttons {"I can't be more excited!"} default button 1 giving up after 3
end copy_files