When you export a document using the export to JPEG option, each page is exported in its entirety as a jpeg. This includes text as well as graphics and images. This may not be what you want especially if exporting at a lower resolution. Text, as you well know, does not display well at lower resolutions. Accordingly, you may want to export text and images separately. For this you have two options:

  1. Export as HTML
  2. Use a script to move the text to a non-printing layer before exporting using export to JPEG.

In this blog we will discuss the latter option: moving text frames to a different layer and exporting pages as JPEG.

If all text frames in the subject document are not embedded in other page items, and are not part of a group, a script can simply get a list of all of the document’s text frames and assign them to a non-printing layer. But you may not have control for how the document was created.

First, if some text frames are part of a group, you will need to give the user options for how the groups will be handled. To keep our sample script simple, we ungroup all groups and move the text frames.

Second, there might be the possibility of text frames being embedded inside other page items. To solve this problem, use the all page items property for a document rather than text frames. All page items returns a list of all page items, nested or otherwise.

Your script can then parse through the list, and test for the class of the item. If its class is text frame, the item is then “moved” to another layer. The subroutine to handle this functionality could be written as follows. Again, for simplicity, the routine simply ungroups all groups before getting the list of page items.

AppleScript

(*Assigns item layer for text frames to layer referenced by layerRef variable*)
on moveTextFrames (docRef, layerRef)
tell application "Adobe InDesign CS6"
		tell docRef
			ungroup groups
			set itemRef to (all page items)
			repeat with eachItem in itemRef
				if class of eachItem = text frame then
					set item layer of eachItem to layerRef
				end if
			end repeat
		end tell
	end tell
end moveTextFrames

ExtendScript

/*Assigns item layer for text frames to layer referenced by layerRef variable*/
function moveTextFrames (docRef, layerRef) {
   var eachItem;
   var groupArr = docRef.groups;
   for (var i = 0; i < groupArr.length; i++) {
       eachItem = groupArr[i];
       eachItem.ungroup();
    }  
   var itemArr = docRef.allPageItems;
   for (var i = 0; i < itemArr.length; i++) {
       eachItem = itemArr[i];
       if (eachItem.constructor.name == "TextFrame") {
           eachItem.itemLayer = layerRef;
       }
   }
}

Before the script performs the export, the JPEG preferences for the application will need to be set.

With Adobe InDesign CS6 we have the following properties that can be set for JPEG export preferences. We leave this portion of the script up to you.

 

   simulate overprint: true or false
   anti alias: true or false
   use document bleeds: true or false
   jpeg color space: RGB, CMYK, or Gray
   embed color profile: false or true
   JPEG Quality: maximum, high, medium, low
   Page String: "All Pages", or specified as a page number, or array of page numbers (valid when JPEG export range is not all)
   JPEG export range: export range, export all
   JPEG Rendering style: baseline encoding, progressive encoding
   Exporting Spread: true or false
      if true, exports each spread as a single JPEG file. 
      If false, facing pages are exported as separate files with sequential numbers added to each file name.
   export resolution: 72 (range from 1 to 2400)

The export to JPEG method requires only the path to the file and the export format (JPG). To get the file path, we will use another subroutine getSavePath.

AppleScript

set folderName to "TestFolder"
set saveFolderPath to getSavePath(docRef, folderName)
(*now do the export*)
tell application "Adobe InDesign CS6"
     tell document 1
         export to (saveFolderPath & ":Page0.jpg") format JPG
     end tell
end tell
(*Establishes folder for saving exported files*)
on getSavePath(docRef, folderName)
set folderPath to missing value
     tell application "Adobe InDesign CS6"
          set isSaved to saved of docRef
	  if isSaved then
		set folderPath to (file path of docRef) as string
	  end if
     end tell
     if folderPath = missing value then
	  set folderPath to path to desktop from user domain
     end if
     tell application "Finder"
	  if not (exists folder (folderPath & folderName)) then
		make folder at folder folderPath with properties {name:folderName}
	  end if
	  set saveFolder to (folderPath & folderName)
     end tell
     return saveFolder
end getSavePath

ExtendScript

var docRef = app.documents.item(0);
var layerRef = docRef.layers.item(0);
var folderName = "TestFolder";
var saveFolderPath = getSavePath (docRef, folderName);
var saveFilePath = new File(saveFolderPath + "/Page0.jpg");
//export the pages of the document as separate jpeg files
docRef.exportFile(ExportFormat.JPG, saveFilePath);
/*Establishes folder path for exported files*/
function getSavePath (docRef, folderName) {
    var folderPath;
    var isSaved = docRef.saved;
    if (isSaved) {
        folderPath = docRef.filePath.toString();
     } else {
         folderPath = Folder.Desktop;
      }
    var saveFolder = folderPath + "/" + folderName;
      if (Folder(saveFolder).exists == false){
          Folder(saveFolder).create();
      }
    return Folder(saveFolder);
 }

With this information in mind you should be able to create your own custom script for exporting pages as JPEGs.

Here are the steps:

  1. Make sure there is a document open. Place its reference in a variable.
  2. Check to see if there is a layer having a name specified in the script. If the layer does not exist, create the layer. Either way, make the layer non-printable. Place a reference to the layer in a variable.
  3. Move all text frames in the document to the new layer.
  4. Set JPEG export preferences using predetermined settings.
  5. If document has been saved, use its parent folder as the export folder’s parent, otherwise create a folder on the user’s desktop for the export.
  6. Export the document as JPEG

See if you can develop this script on your own. But keep tuned, as we will take this script to the next level.