PDF -> TIFF, without using Automator?

Hi,
I’m trying to figure out how I can convert a PDF file into a set of TIFF files using AppleScript, one TIFF per page.

Using Automator, I can just use the ‘Render PDF Pages as Images’ action to do the job, and at a push I can use AppleScript to control Automator to create a suitable workflow that does this.

But this seems like massive overkill. Surely I can do the equivalent of this action directly from within AppleScript?

What I can’t figure out (being fairly new to OS X and AppleScript) is which application the ‘Render PDF’ action is actually using, and whether it is directly scriptable.

Any help much appreciated!

Cheers - Steve

Model: PPC Mac Mini
AppleScript: 1.10.7
Browser: Firefox 1.5.0.8
Operating System: Mac OS X (10.4)

The general drill goes like this:


set thisImage to (choose file of type "PDF " with prompt "Choose a PDF file")
tell application "Image Events"
	set pdfImage to open thisImage
	save pdfImage in ((path to desktop as text) & "PDFConv.tif" as Unicode text) as TIFF
	close pdfImage
end tell

This will leave a tiff of the pdf you chose on the desktop, but the tiff isn’t pretty, and it’s only the first page. To get individual pages, you have to break them out as individual documents. Search for that, it’s been done before (though I haven’t done it).

It might help if we all knew what programs you had to work with other than standard OS apps. Preview can convert a PDF to a TIFF but it doesn’t appear to be scriptable.

Do you have PhotoShop? Or Acrobat Professional? If you can give the version number that would be ideal.

Model: G5 Tower (not Intel) - Script Editor ver 2.1.1
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

GraphicConverter does a nice job too, but only of the first page.

Hi Adam,
Thanks for the example. I had to remove the ‘of type “PDF”’, otherwise all my PDF files were greyed out in the finder. Otherwise, this works fine, and has given me a good push in the right direction. Thank you.

Matt-Boy,
If at all possible I want to avoid this being dependent on additional software that may or may not be installed.

Cheers - Steve

Hi Steve - pdf files are of type “PDFspace” – all types have four characters.

Hi Adam,

Thanks for pointing this out - I should have known that - but it didn’t occur to me.

Cheers - Steve

Drove myself nuts for days with my File Repair software forgetting that simple little detail. bleh

Alright. The only way I could come up with was using Preview which all Macs have, but which is 100% no scriptable. So, I did it all with System Events. I know it’s ugly but it does work. Two notes: You must have the save window expanded (click on the little arrow) so it can choose TIFF as the file format, and you have to manually enter the number of pages. I couldn’t figure out how to pull the number of pages in the PDF (since Preview has no scripting dictionary). Maybe someone else can suggest something for that so you didn’t have to type it in. If you have problems, try increasing the delay times before giving up.

set thisImage to (choose file of type "PDF " with prompt "Choose a PDF file")
set NumberInput to text returned of (display dialog "Enter Number of pages." default answer "")
try
	set NumPages to NumberInput as integer
on error
	display dialog "Expected number entry. Cancel and try again" buttons "Cancel" default button "Cancel"
end try
if NumPages is not greater than 0 then
	display dialog "Invalid number. Cancel and try again" buttons "Cancel" default button "Cancel"
end if

tell application "Preview"
	activate
	open thisImage
	delay 2
	tell application "System Events"
		repeat with x from 1 to NumPages
			keystroke "s" using {command down, shift down}
			delay 1
			keystroke "d" using {command down} --save to desktop
			delay 0.5
			key code 124 --right arrow
			delay 0.5
			key code (x + 17) --puts page number in filename
			delay 0.5
			repeat with whatever from 1 to 7
				key code 48 --tab
				delay 0.5
			end repeat
			repeat with whatever2 from 1 to 8
				key code 125 --down arrow til it gets to TIFF
				delay 0.5
			end repeat
			keystroke return
			delay 0.5
			keystroke return
			delay 4
			keystroke "w" using {command down} --close TIFF that atuo-opens
			delay 1
			if x = NumPages then exit repeat
			tell application "Preview" to open thisImage
			delay 2
			repeat with PageDown from 1 to x
				key code 121 --page down
			end repeat
			delay 0.5
		end repeat
	end tell
end tell

Model: G5 Tower (not Intel) - Script Editor ver 2.1.1
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

The Automator action apparently either uses some unknown system call to split the PDF pages or works from its own knowledge of the PDF format. If a script’s going to use an application to do the job, it may as well use an Automator workflow saved as an application. (This isn’t quite the same as using Automator itself.) The following assumes a workflow saved as an application under the name “PDF to TIFF.app” and consisting of:

  1. “Render PDF Pages as Images”: set to produce TIFFs.
  2. “Rename Finder Items”: Make Sequential, new name (anything). The other settings aren’t critical. The script renames the files anyway, but this seems to be the best way to let it know the order in which they were created.
  3. “New Folder”: set to receive the TIFFs into a new folder it creates on the desktop called “PDF to TIFF Conversions”.

The process isn’t particularly fast, but of course that’s the workflow’s fault, not the script’s. :wink:

set pdfFile to (choose file of type "PDF ")

-- Apply the workflow application to the chosen PDF file.
tell application "PDF to TIFF" to open pdfFile

-- Hang around until the workflow's done its stuff.
tell application "System Events"
	repeat while (application process "PDF to TIFF" exists)
		delay 0.2
	end repeat
end tell

tell application "Finder"
	-- Get a sorted list of the sequentially named TIFFs in the folder created by the workflow.
	set conversionsFolder to folder "PDF to TIFF Conversions" of desktop
	set tiffList to (sort (get files of conversionsFolder) by name)
	-- Rename the TIFFs with the original PDF name followed by two digits and ".tiff".
	-- The workflow's numbering system continues to increment over successive runs,
	-- so we need to use our own counter for the digits. Starting at it 100 rather than at 0
	-- ensures that the tens digit exists in the numbers as text. We'll drop the leading "1"s.
	set baseName to (name of pdfFile) & "."
	set counter to 100
	repeat with thisTiff in tiffList
		set counter to counter + 1
		set thisTiff's name to baseName & text 2 thru -1 of (counter as Unicode text) & ".tiff"
	end repeat
	-- Move the renamed TIFFs to the same folder as the PDF file and zap the temporary folder.
	move files of conversionsFolder to container of pdfFile
	delete conversionsFolder
end tell

beep 2 -- "Finished!"