Your prescription for increased productivity and profitability
The Macintosh operating system provides a wealth of scripting options for automating Adobe InDesign using AppleScript. This is in addition to that provided by InDesign itself.
With AppleScript you can take advantage of the scripted capability of other programs such as Photoshop that have opened their functionality to scripting.
One application you may not be aware of that comes bundled with the Macintosh operating system is Image Events. This tidy little faceless background utility allows you to manipulate an image without having to open it in any other application.
For instance, a user recently posted an inquiry to the InDesign scripting forum wanting to know if there were some way that he could use a script to crop an image as part of its being exported from InDesign. The answer to the question is yes and no. The problem is that the user wanted to crop the image from one side only. Sorry Image Events can’t do that, but it can crop horizontally and vertically (either dimension or both). To crop on one side only, you could have a script crop the image’s container temporarily, export, and reset the container after export. But first, let’s see how we could use Image Events to add to InDesign’s support for exporting an image.
InDesign’s scripting export capability includes exporting a selected image as PNG or JPEG. For a once-in-a-while need, simply select the image and choose Export (Command E). From the export window you then select the file type. Then from the Export PNG/JPG export window you select the options needed.
This is all well and good, but if this is something you do time and time again using the same options, you could automate this with a handy script available from InDesign’s script menu (or using a keyboard shortcut).
The following script tests to make sure that an image or a page item with an image is selected, sets the export options, and exports the image to the desktop with a pre-determined name.
--Assumes image is to be exported to the desktop set userPath to path to desktop as string set fileName to "Test.png" set errMsg to "Requires image to be selected" try tell application "Adobe InDesign CC 2015" set selList to selection set selItem to item 1 of selList if class of selItem is in {rectangle, oval, polygon} then set selImage to image 1 of selItem else if class of selItem is image then set selImage to selItem end if set export resolution of PNG export preferences to 72.0 tell PNG export preferences set PNG color space to RGB set PNG Quality to high set transparent background to true set use document bleeds to true end tell export selImage format PNG format to (userPath & fileName) end tell on error activate display alert errMsg return end try
Notice in the above how a try/on error trap catches errors that will be generated if a page item or image is not selected.
Also take note of the settings for PNG export preferences that are being used. You will want to change these to your preferred settings.
You may want to enhance this script by giving the user a dialog box into which to enter the name for the exported file. Or, you could build the name of the file based on the name of the document and the image’s parent page. Then check to make sure a file of the same name does not exist at that location. If so, add a timestamp to the file’s name to insure its uniqueness.
What you need to know is that when you export a single selected image using either the manual method or with a script, you get the entire image not as cropped by its container. This may be exactly what you want. But what if you want the image exported cropped? Read on.
For those options that are not possible from within InDesign’s export capability there is Image Events. With Image Events you can perform other image operations including rotate and crop.
To crop an image as part of the export process, add the following code to the script above. This code uses Image Events to crop an image’s width uniformly (both left an right side). Notice that Image Events needs to open the file before processing. And, once a file is opened, your script needs to make sure the file is closed.
--Add code for cropping image to the script above set fileRef to (userPath & fileName) set cropWid to 200 set cropHgt to 0 tell application "Image Events" set this_image to open file fileRef try set fileInfo to dimensions of this_image set fileWid to item 1 of fileInfo set fileHgt to item 2 of fileInfo set newWid to fileWid - cropWid set newHgt to fileHgt - cropHgt crop this_image to dimensions {newWid, newHgt} save this_image in file (userPath & fileName) close this_image on error close this_image end try end tell
To make this script more versatile, you will want to add a custom dialog so the user can enter the amount of crop for both the width and the height–or for no crop at all.
Image as Exported
Image after cropping
Interesting to note is that if there is more than one image container selected on an InDesign page, the images are exported as one image positioned as on the page. You can use this to your advantage. What you need to know is if InDesign detects that there are multiple images selected for export, it exports the image as cropped by its container.
Here is where a script can really come in handy should you want multiple selected images exported as individual files or you want one or more images exported as cropped by their containers. The script for this is a little more involved because it needs to parse through the list of selected page items and determine if they contain an image. Here the code that sets the PNG export preferences has been moved to its own subroutine (handler).
set userPath to path to desktop from user domain as string set fileName to "myTest" set imageList to {} tell application "Adobe InDesign CC 2015" set selList to selection repeat with i from 1 to length of selList set thisItem to item i of selList if class of thisItem is in {rectangle, oval, polygon} then if images of thisItem is not {} then set end of imageList to thisItem end if end if end repeat if imageList is not {} then my setExportPrefs() repeat with i from 1 to length of imageList export item i of imageList format PNG format to userPath & fileName & i & ".png" end repeat end if end tell imageList on setExportPrefs() tell application "Adobe InDesign CC 2015" set export resolution of PNG export preferences to 72.0 tell PNG export preferences set PNG color space to RGB set PNG Quality to high set transparent background to true set use document bleeds to true end tell end tell end setExportPrefs
Using the Export Multiple Selected script above, all images inside selected containers are exported as cropped. If you have only one image that needs to be exported as cropped by its container, simply create an empty container and select both. Then export using the Export Multiple Selected script.
Of course this is only the beginning when it comes to working with multiple images. If you want to add the ability to crop or not crop images individually, you have your work cut out for you. So how could you have your user determine how to crop (or not crop) each image?
This should give you some ideas as to how to crop an image as part of its export from InDesign. Further, take a look at the dictionary for Image Events and see what other magic this handy addition can perform for you. You can find some sample scripts for using Image Events as part of the collection provided by Apple. To access these scripts, make sure you have the AppleScript menu installed on the Mac OS X menu bar.
If the Script Menu is not installed: Choose Script Editor from the Utilities folder in Applications folder. (Choose AppleScript Editor in older OS X versions). Once installed, launch Script Editor and choose Preferences from its application menu (ScriptEditor). In the preferences’ General tab, check Show Script menu in menu bar and Show Computer Scripts.
This installs scripts and places the script icon as a button on the menu bar. Clicking on this icon opens a dropdown menu to reveal a number of directories, one of which is Script Editor Scripts. Expand Script Editor Scripts to locate Image Manipulation Scripts. This is where you will find sample scripts for Image Events.
The discussion above just touches some of the issues that can be addressed as part of an image export script. In next week’s blog we will expand on these scripts even further.
Scripts presented in these blogs are provided to give users ideas and code for creating scripts of their own. No representation is made as to the completeness or accuracy of the scripts. Use at your own discretion.