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 my _log(“example”) text lines until the last one before it dies to a file on the Desktop. Second is my binaries (‘gs’ and ‘mogrify’) are from MacPorts, so your path may vary. Also, has not been tested on files with pages of differing orientations.

Edit: Now includes the command to have mdimport update mdns (Spotlight) for  files that return “(null)” values so mdls can do its thing. Was using a routine named “spotit” applied to the container of item 1, but if your folder is chock full-o-files the mdimport could take longer than a reasonable delay. Plus it wasn’t necessary for older files. Now tests each source file and runs the import on only that file if necessary.

 

-- strawhousepig.net

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 spotit(container of file (item 1 of _files as text)) --See routine below for why this is commented out.
			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 spotit(container of file (item 1 of _files as text)) --See routine below for why this is commented out.
			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 & ")")
			if (do shell script "mdls -name kMDItemFSName " & quoted form of source_path) = "kMDItemFSName = (null)" then
				do shell script "mdimport -i " & quoted form of source_path
			end if
			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 messing up results") --Yep, every "my _log()" still gets called.
		--		end try
	end repeat
end make_it_flat

(* This was meant to update mdns (Spotlight) info on new files by importing the parent folder.
If that folder has a lot of items that could bog down before that data is needed resulting in "(null)" errors.
Moved into the main repeat and applied to each file, testing for "kMDItemFSName = (null)" first.
on spotit(_folder)
	set _folder to _folder as text
	do shell script "mdimport -i " & quoted form of POSIX path of _folder
	delay 3
end spotit
*)

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 '/Users/Shared/Artwork & Masters/Creative Printers/watermarks/PROOF watermark magenta.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 '/Users/Shared/Artwork & Masters/Creative Printers/watermarks/PROOF watermark magenta 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 *