Automating Adobe InDesign CC 2015

Export and Crop Image

Wednesday, December 14, 2016

Image Processing for Macintosh Users

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.

Introducing Image Events

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.

Export 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. 

PNG Export Dialog InDesign's PNG Export Dialog

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.

Export Selected to PNG

--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"
  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
  display alert errMsg
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.

Cropping the Image

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
    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 from InDesign Image as Exported

Image after cropping Image after cropping

Multiple Images

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).

Export Multiple Selected

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

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 

To Crop or Not

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?

  1. You could have the user wait until after export and then select the files to be cropped individually using a script while providing cropping instructions for the chosen image using a custom dialog.
  2. You could flag items for cropping inside the InDesign document, possibly using the image container's label property. (If the label of the page item selected has a given value, then perform the crop as indicated.)
  3. For a batch process that does not rely on items selected, indicate images that should be exported and cropped by placing on a layer named appropriately. Your script can then determine how to process the images using layer placement determination. And, yes, when exporting as part of a repeat loop, InDesign crops the images to their containers.

More for Image Events

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.

Onward and Upward

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.

Trackback Link
Post has no trackbacks.

Recent Posts


directory wild cards GPS BrdgeTalk Embed Document integer editboxes transform text concatenation Editbox Sound Export to PDF Find Change user interaction level automating InDesign Document from Template XML structure CSS arrays coordinates repeat loops text item delimiters Preferences Document stylesheets Templates image metadata PDF security Custom Dialog text editbox with timeout trimText Script Preferences AppleScript Templates selection placeable Find by Color parent script templates Placing images Checkbox Control Nested Styles Image Placement Adding Captions reframe Find by Font handlers Bullets System Events Read text file as list do script text placement new document resolve import text Combobox massage text Apple apps paragraph styles Character Styles Numbering Hyperlinks tutorial Document Presets save template scripting Excel XML tags doScript Publish Online AppleScript dialogs measurements Find Text Text Cleanup Custom CSS Style Sheets if/then HTML Export metacharacters measurement editboxes InDesign dialogs Fixed Layout ePub Repeat With page items ePub for Kindle Metadata enable redraw Export HTML Change Text Mail object styles Tables try/on error transformation matrix script folder XML resize Video Border Panel watermark External Object Scripts functions active spread stylesheets styles dropdowns automation PDF Media Fixed Format ePub automated workflow pasteboard Choose file ePub for iPad InDesign CC Autoflow file paths table styles find change options Enabling Groups Index of blogs and scripts for year 2016 transform again Library Folder Caption Variable CALS tables Create Document fit Styling Text Find by Attribute parent page AppleScript Adobe InDesign CC PDF export preferences Choose from list say GPS metadata import XML path folder Radiobutton Group Interactive Publications records Create Text Frame InDesign template import HTML Table cells cut and paste images Glossary Automating animations lists Custom Buttons entire path