{"id":976,"date":"2024-10-24T11:55:11","date_gmt":"2024-10-24T18:55:11","guid":{"rendered":"https:\/\/strawhousepig.net\/wordpress\/?p=976"},"modified":"2024-11-11T09:08:06","modified_gmt":"2024-11-11T16:08:06","slug":"applescript-friends-to-generate-proof-image-of-pdf","status":"publish","type":"post","link":"https:\/\/strawhousepig.net\/wordpress\/2024\/10\/24\/applescript-friends-to-generate-proof-image-of-pdf\/","title":{"rendered":"AppleScript + friends to generate proof image of PDF"},"content":{"rendered":"<p>Often when you create artwork for use as part of your making a living you don&#8217;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 <a href=\"https:\/\/www.neighborhoodarchive.com\/music\/songs\/to_go_someplace_else.html\">Someplace Else<\/a>. 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.<!--more--><\/p>\n<p>Originally done manually in the layout software, then via an Automator workflow peppered with AppleScript, which was fine but didn&#8217;t handle multiple pages nor cropping the bleed. Now that&#8217;s in the past with just AppleScript and help from <a href=\"https:\/\/www.ghostscript.com\/\">Ghostscript<\/a> and <a href=\"https:\/\/imagemagick.org\/index.php\">ImageMagick<\/a>. 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.<\/p>\n<p>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(&#8220;example&#8221;) text lines until the last one before it dies to a file on the Desktop. Second is my binaries (&#8216;gs&#8217; and &#8216;mogrify&#8217;) are from <a href=\"https:\/\/www.macports.org\/\">MacPorts<\/a>, so your path may vary. Also, has not been tested on files with pages of differing orientations.<\/p>\n<p>Edit: Now includes the command to have <strong>mdimport<\/strong> update <strong>mdns<\/strong> (Spotlight) for\u00a0 files that return &#8220;(null)&#8221; values so <strong>mdls<\/strong> can do its thing. Was using a routine named &#8220;spotit&#8221; applied to the container of item 1, but if your folder is chock full-o-files the <strong>mdimport<\/strong> could take longer than a reasonable delay. Plus it wasn&#8217;t necessary for older files. Now tests each source file and runs the import on only that file if necessary.<\/p>\n<p>\u00a0<\/p>\n\n\n<pre class=\"wp-block-code scroll\"><code>-- strawhousepig.net\n\nproperty BCPP_troubleshoot : false\n\non run\n\ttell application \"Finder\"\n\t\tset _files to {}\n\t\ttry\n\t\t\trepeat with _f in (selection as list)\n\t\t\t\tset end of _files to (_f as alias)\n\t\t\tend repeat\n\t\t\t--my spotit(container of file (item 1 of _files as text)) --See routine below for why this is commented out.\n\t\t\tmy make_it_flat(_files)\n\t\ton error _err\n\t\t\tdisplay alert _err\n\t\tend try\n\tend tell\nend run\n\non open drop_files\n\ttell application \"Finder\"\n\t\tmy _log(\"drop received\")\n\t\tset _files to {}\n\t\ttry\n\t\t\trepeat with _f in drop_files\n\t\t\t\tset end of _files to _f\n\t\t\tend repeat\n\t\t\t--my spotit(container of file (item 1 of _files as text)) --See routine below for why this is commented out.\n\t\t\tmy make_it_flat(_files)\n\t\ton error _err\n\t\t\tdisplay alert _err\n\t\tend try\n\tend tell\nend open\n\non make_it_flat(the_files)\n\tmy _log(\"file list ready\")\n\tset _res to \"150\" --(choose from list {\"150\", \"300\", \"600\", \"900\", \"1200\"})\n\t--\tmy _log(\"res set\")\n\trepeat with _this in the_files\n\t\tmy _log(\"start loop 1\")\n\t\t--\t\ttry\n\t\tset finfo to info for _this\n\t\tmy _log(\"info accessed\")\n\t\tif name extension of finfo is in {\"pdf\", \"ps\"} then\n\t\t\tmy _log(\"extension found\")\n\t\t\tset source_path to POSIX path of _this as string\n\t\t\tmy _log(\"source path found (\" &amp; source_path &amp; \")\")\n\t\t\tif (do shell script \"mdls -name kMDItemFSName \" &amp; quoted form of source_path) = \"kMDItemFSName = (null)\" then\n\t\t\t\tdo shell script \"mdimport -i \" &amp; quoted form of source_path\n\t\t\tend if\n\t\t\tset f_name to name of finfo\n\t\t\tset _names to {}\n\t\t\tset n to 1\n\t\t\tset page_n to do shell script \"mdls -name kMDItemNumberOfPages \" &amp; quoted form of POSIX path of _this\n\t\t\tset page_n to (text ((offset of \"=\" in page_n) + 1) thru -1 of page_n) as integer\n\t\t\tmy _log(\"number of PDF pages found (\" &amp; page_n &amp; \")\")\n\t\t\tif n = page_n then\n\t\t\t\tset end of _names to (characters 1 thru -((offset of \".\" in (reverse of items of f_name as string)) + 1) of f_name as string) &amp; \" proof.jpg\"\n\t\t\telse\n\t\t\t\trepeat until n > page_n\n\t\t\t\t\tmy _log(\"start loop 2\")\n\t\t\t\t\tset end of _names to (characters 1 thru -((offset of \".\" in (reverse of items of f_name as string)) + 1) of f_name as string) &amp; \" proof \" &amp; n &amp; \".jpg\"\n\t\t\t\t\tset n to n + 1\n\t\t\t\tend repeat\n\t\t\tend if\n\t\t\tlog _names\n\t\t\trepeat with i from 1 to (count of _names)\n\t\t\t\tmy _log(\"start loop 3\")\n\t\t\t\tset target_path to (characters 1 thru -((offset of \"\/\" in (reverse of items of source_path as string)) + 1) of source_path as string) &amp; \"\/\" &amp; item i of _names\n\t\t\t\tmy _log(\"target path found (\" &amp; target_path &amp; \")\")\n\t\t\t\tset the_script to \"\/opt\/local\/bin\/gs -sDEVICE=jpeg -r\" &amp; _res &amp; \" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dJPEGQ=98 -dSAFER -dNOCACHE  -dALLOWPSTRANSPARENCY -dFirstPage=\" &amp; i &amp; \" -dLastPage=\" &amp; i &amp; \" -sOutputFile=\" &amp; quoted form of target_path &amp; space &amp; quoted form of source_path\n\t\t\t\tlog the_script\n\t\t\t\tdo shell script the_script\n\t\t\t\tmy _log(\"Ghostscript called\")\n\t\t\t\tmy cropit(target_path)\n\t\t\tend repeat\n\t\tend if\n\t\t--\t\ton error theErr\n\t\t--\t\t\tif theErr is not \"User canceled.\" then display dialog theErr\n\t\t--\t\t\tmy _log(\"try block messing up results\") --Yep, every \"my _log()\" still gets called.\n\t\t--\t\tend try\n\tend repeat\nend make_it_flat\n\n(* This was meant to update mdns (Spotlight) info on new files by importing the parent folder.\nIf that folder has a lot of items that could bog down before that data is needed resulting in \"(null)\" errors.\nMoved into the main repeat and applied to each file, testing for \"kMDItemFSName = (null)\" first.\non spotit(_folder)\n\tset _folder to _folder as text\n\tdo shell script \"mdimport -i \" &amp; quoted form of POSIX path of _folder\n\tdelay 3\nend spotit\n*)\n\non cropit(img)\n\ttell application \"Image Events\"\n\t\tlaunch\n\t\tset i to open (img)\n\t\tmy _log(\"img opened\")\n\t\tcopy dimensions of i to {W, H}\n\t\tmy _log(\"dims copied (\" &amp; W &amp; space &amp; H &amp; \")\")\n\t\tset dims to {W, H}\n\t\tmy _log(\"dims set\")\n\t\tclose i\n\tend tell\n\tif item 1 of dims is greater than 525 then\n\t\tset dims to {525, 300}\n\t\tmy _log(\"dims reset horz\")\n\telse if item 2 of dims is greater than 525 then\n\t\tset dims to {300, 525}\n\t\tmy _log(\"dims reset vert\")\n\tend if\n\tmy markit(img, dims)\n\ttell application \"Image Events\"\n\t\tlaunch\n\t\tset _img to open (img)\n\t\tcrop _img to dimensions dims\n\t\tmy _log(\"image cropped\")\n\t\tsave _img\n\tend tell\nend cropit\n\non markit(img, dims)\n\tif (item 1 of dims) > (item 2 of dims) then\n\t\tdo shell script \"\/opt\/local\/bin\/mogrify -gravity center -draw \\\"image Over 0,0 525,300 '\/Users\/Shared\/Artwork &amp; Masters\/Creative Printers\/watermarks\/PROOF watermark magenta.png'\\\" \" &amp; quoted form of img\n\t\tmy _log(\"img marked horz\")\n\telse\n\t\tdo shell script \"\/opt\/local\/bin\/mogrify -gravity center -draw \\\"image Over 0,0 300,525 '\/Users\/Shared\/Artwork &amp; Masters\/Creative Printers\/watermarks\/PROOF watermark magenta vert.png'\\\" \" &amp; quoted form of img\n\t\tmy _log(\"img marked vert\")\n\tend if\nend markit\n\non _log(input)\n\tif BCPP_troubleshoot is true then\n\t\tdo shell script \"echo `date`' \" &amp; input &amp; \"' >> ~\/Desktop\/ASlog-BCPP.txt\"\n\telse\n\t\treturn\n\tend if\nend _log<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Often when you create artwork for use as part of your making a living you don&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[10,24,33,34],"class_list":["post-976","post","type-post","status-publish","format-standard","hentry","category-code","tag-applescript-2","tag-macos","tag-script","tag-shell-script"],"_links":{"self":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts\/976","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/comments?post=976"}],"version-history":[{"count":10,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts\/976\/revisions"}],"predecessor-version":[{"id":1007,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts\/976\/revisions\/1007"}],"wp:attachment":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/media?parent=976"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/categories?post=976"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/tags?post=976"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}