AppleScript + friends to generate proof image of PDF

Often when you create artwork for use as part of your making a living you don’t charge for it and just keep it to yourself. You also have to show it to the customer for approval, but sending them the original art means risking them taking it Someplace Else. You can of course charge up front or after the fact, but you still risk being out priced by the shop not doing the layout. So my best practice has been to create a lower than optimal resolution image with a watermark (aka stamp) placed over the artwork.

Originally done manually in the layout software, then via an Automator workflow peppered with AppleScript, which was fine but didn’t handle multiple pages nor cropping the bleed. Now that’s in the past with just AppleScript and help from Ghostscript and ImageMagick. The other requirement is an image (a PNG with transparency/alpha channel) to use as the watermark. Though it would be easy enough to have ImageMagick render text instead. I already had the watermark, so went with that.

This script is meant and coded for business cards, but could (and might) be reworked to handle any size PDF document. Not sure how to handle cropping bleed, though. Couple weiridities with this one. First is there is a logging function meant to help debug when run as a droplet or from the script menu. It writes the last thing it did before it died to a file on the Desktop. Second is my binaries (‘gs’ and ‘mogrify’) are from MacPorts, so your path may vary.

 

property BCPP_troubleshoot : false

on run
	tell application "Finder"
		set _files to {}
		try
			repeat with _f in (selection as list)
				set end of _files to (_f as alias)
			end repeat
			my make_it_flat(_files)
		on error _err
			display alert _err
		end try
	end tell
end run

on open drop_files
	tell application "Finder"
		my _log("drop received")
		set _files to {}
		try
			repeat with _f in drop_files
				set end of _files to _f
			end repeat
			my make_it_flat(_files)
		on error _err
			display alert _err
		end try
	end tell
end open

on make_it_flat(the_files)
	my _log("file list ready")
	set _res to "150" --(choose from list {"150", "300", "600", "900", "1200"})
	--	my _log("res set")
	repeat with _this in the_files
		my _log("start loop 1")
		--		try
		set finfo to info for _this
		my _log("info accessed")
		if name extension of finfo is in {"pdf", "ps"} then
			my _log("extension found")
			set source_path to POSIX path of _this as string
			my _log("source path found (" & source_path & ")")
			set f_name to name of finfo
			set _names to {}
			set n to 1
			set page_n to do shell script "mdls -name kMDItemNumberOfPages " & quoted form of POSIX path of _this
			set page_n to (text ((offset of "=" in page_n) + 1) thru -1 of page_n) as integer
			my _log("number of PDF pages found (" & page_n & ")")
			if n = page_n then
				set end of _names to (characters 1 thru -((offset of "." in (reverse of items of f_name as string)) + 1) of f_name as string) & " proof.jpg"
			else
				repeat until n > page_n
					my _log("start loop 2")
					set end of _names to (characters 1 thru -((offset of "." in (reverse of items of f_name as string)) + 1) of f_name as string) & " proof " & n & ".jpg"
					set n to n + 1
				end repeat
			end if
			log _names
			repeat with i from 1 to (count of _names)
				my _log("start loop 3")
				set target_path to (characters 1 thru -((offset of "/" in (reverse of items of source_path as string)) + 1) of source_path as string) & "/" & item i of _names
				my _log("target path found (" & target_path & ")")
				set the_script to "/opt/local/bin/gs -sDEVICE=jpeg -r" & _res & " -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dJPEGQ=98 -dSAFER -dNOCACHE  -dALLOWPSTRANSPARENCY -dFirstPage=" & i & " -dLastPage=" & i & " -sOutputFile=" & quoted form of target_path & space & quoted form of source_path
				log the_script
				do shell script the_script
				my _log("Ghostscript called")
				my cropit(target_path)
			end repeat
		end if
		--		on error theErr
		--			if theErr is not "User canceled." then display dialog theErr
		--			my _log("try block giving false results")
		--		end try
	end repeat
end make_it_flat

on cropit(img)
	tell application "Image Events"
		launch
		set i to open (img)
		my _log("img opened")
		copy dimensions of i to {W, H}
		my _log("dims copied (" & W & space & H & ")")
		set dims to {W, H}
		my _log("dims set")
		close i
	end tell
	if item 1 of dims is greater than 525 then
		set dims to {525, 300}
		my _log("dims reset horz")
	else if item 2 of dims is greater than 525 then
		set dims to {300, 525}
		my _log("dims reset vert")
	end if
	my markit(img, dims)
	tell application "Image Events"
		launch
		set _img to open (img)
		crop _img to dimensions dims
		my _log("image cropped")
		save _img
	end tell
end cropit

on markit(img, dims)
	if (item 1 of dims) > (item 2 of dims) then
		do shell script "/opt/local/bin/mogrify -gravity center -draw \"image Over 0,0 525,300 '/path/to/watermark.png'\" " & quoted form of img
		my _log("img marked horz")
	else
		do shell script "/opt/local/bin/mogrify -gravity center -draw \"image Over 0,0 300,525 '/path/to/watermark-vert.png'\" " & quoted form of img
		my _log("img marked vert")
	end if
end markit

on _log(input)
	if BCPP_troubleshoot is true then
		do shell script "echo `date`' " & input & "' >> ~/Desktop/ASlog-BCPP.txt"
	else
		return
	end if
end _log

Leave a Reply

Your email address will not be published. Required fields are marked *