In our last blog we introduced the beginnings of a script to automate exporting pages of an InDesign document as JPEG images without the accompanying text frames. This was done by moving text frames to a non-printing layer prior to exporting.

To complete this script there are two procedures that need to be taken care of:

  1. Set up the layer that will be used for the text frames
  2. Establish the properties for JPEG export preferences

For this you will need to get information from the user:

  1. The name of the layer to be created for the text frames
  2. Values for some of the JPEG export preference properties

For this we will build a custom dialog. While we are at it, we can get the name for the folder that will hold the JPEG images. It is often a good idea to build a dialog as a separate script so it can be tested without disturbing the main portion of the script. Below is an example of how this subroutine could be written.


(*custom dialog provides for user input to get values to be used in the script.*)
on userDialog(dlgName, cancelIt)
    tell application "Adobe InDesign CS6"
        set origLevel to user interaction level of script preferences
	set user interaction level of script preferences to interact with all
	set dlgRef to make dialog with properties {name:dlgName, canCancel:cancelIt}
	tell dlgRef
	    set dlgColumn to make dialog column
		tell dlgColumn
		    set dlgRow to make dialog row
		end tell
	tell dlgRow
	    set col1 to make dialog column
	    set col2 to make dialog column
	end tell
	tell col1
	    make static text with properties {static label:"Export folder name:"}
	    make static text with properties {static label:"Layer name:"}
	    make static text with properties {static label:"Choose JPEG export quaity"}
	    make static text with properties {static label:"Choose rendering style"}
	    make static text with properties {static label:"Choose resolution"}
	end tell
	tell col2
	    set nameField to make text editbox with properties {min width:200, edit value:""}
	    set layerField to make text editbox with properties {min width:200, edit value:""}
	    set qualityDrop to make dropdown with properties {min width:200, string list:qualityOptions, selected index:0}
	    set renderDrop to make dropdown with properties {min width:200, string list:renderOptions, selected index:0}
	    set resolutionDrop to make dropdown with properties {min width:200, string list:resOptions, selected index:0}		
	end tell
    end tell
    set userResponse to show dlgRef
    set wasCancelled to false
    if userResponse = true then
        set folderName to edit contents of nameField
        set layerName to edit contents of layerField
        set qualityIndex to (selected index of qualityDrop) + 1
        set renderIndex to (selected index of renderDrop) + 1
        set resolutionIndex to (selected index of resolutionDrop) + 1
    else --user cancelled
        set wasCancelled to true
    end if
    destroy dlgRef
    set user interaction level of script preferences to origLevel
    --if cancelled, throw error; otherwise return values
    if wasCancelled = true then
        throw("user cancelled")
    end if
    return {folderName, layerName, qualityIndex, renderIndex, resolutionIndex}
    end tell
end userDialog


//custom dialog provides for user input to get values needed for the script.
function userDialog(dlgName, cancelIt) {
    var origLevel = app.scriptPreferences.userInteractionLevel;
    app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;
    var dlgRef = app.dialogs.add({name:dlgName, canCancel:cancelIt});
    var dlgColumn = dlgRef.dialogColumns.add();
    var dlgRow = dlgColumn.dialogRows.add();
    var col1 = dlgRow.dialogColumns.add();
    var col2 = dlgRow.dialogColumns.add();
    col1.staticTexts.add({staticLabel:"Export folder name:"});
    var nameField = col2.textEditboxes.add({minWidth:200, editValue:""});
    col1.staticTexts.add({staticLabel:"Layer name:"});
    var layerField = col2.textEditboxes.add({minWidth:200, editValue:""});
    col1.staticTexts.add({staticLabel:"Choose JPEG export quality:"});
    var qualityDrop = col2.dropdowns.add({minWidth:200, stringList:qualityOptions, selectedIndex:0});
    col1.staticTexts.add({staticLabel:"Rendering style:"});
var renderDrop = col2.dropdowns.add({minWidth:200, stringList:renderOptions, selectedIndex:0});
       col1.staticTexts.add({staticLabel:"Choose resolution:"});
        var resolutionDrop = col2.dropdowns.add({minWidth: 180, stringList:resOptions, selectedIndex:0});
    var userResponse =;
    var wasCancelled = false;
    if (userResponse == true) {
        var folderName = nameField.editContents;
        var layerName = layerField.editContents;
        var qualityIndex = qualityDrop.selectedIndex;
	var renderIndex = renderDrop.selectedIndex;
	var resolutionIndex = resolutionDrop.selectedIndex;
    } else { //user cancelled
        wasCancelled = true
    app.scriptPreferences.userInteractionLevel = origLevel;
    //if cancelled, throw error; otherwise return values
    if (wasCancelled) {
        throw ("User cancelled");
    return [folderName, layerName, qualityIndex, renderIndex, resolutionIndex];

The second bit of information returned from the dialog (layerName) will be used to establish the layer in another subroutine called createLayer. This requires a reference to the document and the name for the layer.



(*Creates the layer if it does not exist; unlocks layer if it does exist*)
on createLayer(docRef, layerName)
    set layerRef to missing value
    tell application "Adobe InDesign CS6"
        tell docRef
            if not (exists layer layerName) then
	        set layerRef to make layer with properties {name:layerName, printable:false}
	        set layerRef to layer layerName
	        if locked of layerRef = true then
	           set locked of layerRef to false
	        end if
                set printable of layerRef to false
	    end if
        end tell
    end tell
    return layerRef
end createLayer


 /*Checks for a layer named with value of variable layerName. If found, it is unlocked. Otherwise, a new layer is created. The layer is set to be nonprintable*/
function createLayer(docRef, layerName) {
    var layerRef = undefined;
    if (docRef.layers.itemByName(layerName).isValid) {
        layerRef = docRef.layers.itemByName(layerName);
        if (layerRef.locked == true) {
            layerRef.locked = false;
        layerRef.printable = false;
    } else {
        layerRef = docRef.layers.add({name:layerName, printable:false});
    return layerRef

This leaves having to set the values for JPEG export preferences. Our subroutine only sets a few properties as the balance are assumed to be consistent for JPEG export. Notice that the values returned from the dialog are indexes that point to list (array) items corresponding to the string list (array) passed to the dialog. The index value chosen is then passed to the subroutine that pulls the actual value from these lists (arrays).


(*Sets JPEG export preferences*)
on setJPEGExportPrefs(qualityIndex, renderIndex, resolutionIndex)
   tell application "Adobe InDesign CS6"
       set qualityOpts to {low, medium, high, maximum}
       set renderOpts to {baseline encoding, progressive encoding}
       set resOpts to {72, 96, 150, 300}
       tell JPEG export preferences
           set simulate overprint to true
           set anti alias to false
           set use document bleeds to false
           set jpeg color space to RGB
           set embed color profile to false
           set JPEG Quality to item qualityIndex of qualityOpts
           set Page String to "All Pages"
           set JPEG export range to export range
           set JPEG Rendering style to item renderIndex of renderOpts
           set Exporting Spread to false
           set export resolution to item resolutionIndex of resOpts
       end tell
    end tell
end setJPEGExportPrefs


/*Sets JPEG export preferences*/
function setJPEGExportPrefs(qualityIndex, renderIndex, resIndex) {
    var qualityOpts = [JPEGOptionsQuality.LOW, JPEGOptionsQuality.MEDIUM, JPEGOptionsQuality.LOW, JPEGOptionsQuality.HIGH];
    var renderOpts=  [JPEGOptionsFormat.BASELINE_ENCODING, JPEGOptionsFormat.PROGRESSIVE_ENCODING];
    var resOpts  = [72, 150, 300];
    var jpegPrefs = app.jpegExportPreferences;
    with (jpegPrefs){
       simulateOverprint = true;
       antiAlias = false;
       useDocumentBleeds = false;
       jpegColorSpace = ColorSpace.RGB;
       embedColorProfile = false;
       jpegQuality = qualityOpts[qualityIndex];
       pageString = "All Pages";
       jpegExportRange = ExportRangeOrAllPages.EXPORT_ALL;
       jpegRenderingStyle = renderOpts[renderIndex];
       exportingSpread = false;
       exportResolution= resOpts[resIndex];
    }//closes with

Notice the use of with in the ExtendScript version.


The top portion of the script now needs to be changed to accommodate calls to the added routines.


--global variables
property qualityOptions : {"low", "medium", "high", "maximum"}
property renderOptions : {"baseline encoding", "progressive encoding"}
property resOptions : {"72 dpi", "96 dpi", "150 dpi", "300 dpi"}
    set userChoices to userDialog("JPEGExport Options", true)
    set docRef to getDocRef()
    set layerRef to createLayer(docRef, item 2 of userChoices)
    moveTextFrames(docRef, layerRef)
    setJPEGExportPrefs(item 3 of userChoices, item 4 of userChoices, item 5 of userChoices)
    set saveFolderPath to getSavePath(docRef, item 1 of userChoices)
    tell application "Adobe InDesign CS6"
        tell docRef
            export to (saveFolderPath & ":Page0.jpg") format JPG
	end tell
    end tell
on error errStr
    display alert errStr
end try


//global variables
var qualityOptions = ["low", "medium", "high", "maximum"];
var renderOptions = ["baseline encoding", "progressive encoding"];
var resOptions = ["72 dpi", "150 dpi", "300 dpi"];
try {
var userChoices = userDialog ("JPEG Export Options", true);
var docRef = getDocRef();
var layerRef = createLayer (docRef, userChoices[1]);
moveTextFrames (docRef, layerRef);
setJPEGExportPrefs (userChoices[2], userChoices[3], userChoices[4])
var saveFolderPath = getSavePath(docRef, userChoices[0]);
var saveFilePath = new File(saveFolderPath + "/Page0.jpg");
docRef.exportFile(ExportFormat.JPG, saveFilePath);
} catch (e) {
    alert (e);

This pretty much completes the script. At this point we should be able to test its functionality to make sure there is nothing that the user can hand us in the way of a document that will cause our script to fail. And, yes, there may be more than one situation that can cause the script, as written, to be completely worthless. Think about a document where images (some or all) have been anchored to the text. This will give you something to think about until our next blog.