Skip to main content
Inspiring
December 8, 2021
Answered

InDesign: Script for exporting layers to jpg

  • December 8, 2021
  • 2 replies
  • 2310 views

Hello

 

I'm looking for a Script that is capable to export my layers in a .indd file to jpgs in a Folder which I can choose. Also the Script need to name the exportet jpg's as the layer is named in the inDesign file.

 

Anyone an Idea how to makte this happen?

 

Thanks in advance!

This topic has been closed for replies.
Correct answer OPTEN

Yes you can export a selection. Selection is an array, so if the selection is more than one object you would have to handle how to group the pageitems. It could get complicated if the page items are on different layers. By default a page item’s name returns as nothing unless it has been named in the Layers panel, but the name could also be scripted. Something like this, which exports to the desktop:

 

 

 

app.jpegExportPreferences.properties = {
    antiAlias: true,
    embedColorProfile: true,
    exportResolution: 72,
    jpegColorSpace: JpegColorSpaceEnum.RGB,
    jpegQuality: JPEGOptionsQuality.maximum,
    simulateOverprint: false,
  }

var sel = app.documents[0].selection[0];
var lay = sel.itemLayer;
var pin = sel.name;
//sel.name returns as "" by default
if (sel.name == "") {
	pin = "mySelection"
} 

//the jpg name for export—the selection’s Layer - the selection’s name + .jpg
var fn = "/" + lay.name + "-" + pin +".jpg"
$.writeln(sel.name)

sel.exportFile(ExportFormat.jpg, File(Folder.desktop + fn));

 

 

 


@rob day 

You're the man! With a few changes and your Script, it works!

 

For everyone wondering how to do it, this is my take, bases on @rob day 's soulution:

 

app.jpegExportPreferences.properties = {
	antiAlias: true,
	embedColorProfile: true,
	exportResolution: 300,
	jpegColorSpace: JpegColorSpaceEnum.RGB,
	jpegQuality: JPEGOptionsQuality.maximum,
	simulateOverprint: false,
}

	var myFolder = Folder.selectDialog ("Select location to export the Jpeg files.");
	if (myFolder != null) {
		var mySelection = app.selection;
		if (mySelection.length > 0) {
			for (var i = 0; i < mySelection.length; i ++) {
				var exportedSelected = mySelection[i];
				var selectedLayer = exportedSelected.itemLayer;				
				var layerName = "/" + selectedLayer.name + ".jpg";
				exportedSelected.exportFile(ExportFormat.jpg, File(myFolder + layerName));		
			}		
		} else {
			alert("Please select at least one Layer!");
		}		
	}

2 replies

Legend
December 8, 2021

Hello @OPTEN,

 

The below code is a bit more simplified.... I included the page # in the output name just to account for multi page documents, you can modify the code as needed.

 

var doc = app.activeDocument;

var myFolder = Folder.selectDialog ("Select location to export the Jpeg files.");
if (myFolder != null){ 
}else{
exit();
}

for(var p = 0; p < doc.pages.length; p++) {
var myPageName = doc.pages[p].name;

function myGetVisibleLayers(){
 var myVisibleLayers = new Array;
  for (var i = 0; i < doc.layers.length; i++){
   if (doc.layers[i].visible == true) {   
    myVisibleLayers.push(doc.layers[i].index);
      }
   }
      return myVisibleLayers;
  }

  var myVisibleLayers = myGetVisibleLayers();
      
  doc.layers.everyItem().visible = false;

  for (var i = 0; i < doc.layers.length; i++) {

  var my_layer = doc.layers[i].name;

  doc.layers[i].visible = true;

// Set JPEG export preferences 
app.jpegExportPreferences.properties = {
    antiAlias: true,
    embedColorProfile: true,
    exportResolution: 150,
    // exportingSpread: true, // Uncomment if spreads
    jpegColorSpace: JpegColorSpaceEnum.rgb,
    jpegExportRange: ExportRangeOrAllPages.exportRange,
    jpegQuality: JPEGOptionsQuality.maximum,
    jpegRenderingStyle: JPEGOptionsFormat.PROGRESSIVE_ENCODING,
    useDocumentBleeds: false,
    simulateOverprint: false,
    pageString: myPageName,
  }

 doc.exportFile(ExportFormat.jpg,File(myFolder + "/" + my_layer + "_pg" + myPageName + ".jpg"),false);

  doc.layers[i].visible = false;
}
  doc.layers.everyItem().visible = false;

  for (var i = 0; i < myVisibleLayers.length; i++){  
    var myLayers = myVisibleLayers[i];
    doc.layers[myLayers].visible =  true; 
    }
}
 alert("Done Exporting Layers to Jpeg's!");

 

 

Regards,

Mike

OPTENAuthor
Inspiring
December 8, 2021

Hello @Mike Bro 

 

So I figured, your Code would be the things I needed. Do you know how to scale the exportet jpg's, so their heigth and width are correct? 

Like I mentioned in the Reply above, the jpg's are exported in the wrong size. Haven't found out, how to export with the right height and size.

 

Thanks for youre help in advance, your my lifesafer!

OPTEN

rob day
Community Expert
Community Expert
December 9, 2021

Good Morning @rob day 

 

I see the Problem here. Can't I group the children of the Layer and then exportFile() those?

Is your Trick to do it, with the pageItem[i].geometricBounds?

 

A other question is, if it would make sence to export the selection of the Doc. In my understanding, that would work. Would you know a way to get the name of something selected?

Sorry for wasteing your time,

OPT10


Also, if you set the export resolution to 72ppi, the exported pixel dimensions will match the InDesign dimensions if your ruler units are set to Pixels. If the document isn’t set up as Pixels you can set a target pixel by dividing the target pixel width by the width in inches.

 

EDIT: And you can temporarily group all of the items in a page’s layer and set the group as the selection–something like this:

 

 

 

 

//the export pixel width
var pxwidth = 1200
//the active page
var ap = app.activeWindow.activePage;
//the layer to target
var lyr = app.documents[0].layers.itemByName("MyLayer");
//group the layer‘s page items and select the group
var sel = app.selection = tempGroup(ap,lyr);
//sets the export pixel width
var res = getRes(sel, pxwidth)
//the jpg name for export—the page name - the layer’s name + .jpg
var fn = "/"+"Pg." + ap.name +"_"+ lyr.name +".jpg"


//export settings
app.jpegExportPreferences.properties = {
    antiAlias: true,
    embedColorProfile: true,
    exportResolution: res,
    jpegColorSpace: JpegColorSpaceEnum.RGB,
    jpegQuality: JPEGOptionsQuality.maximum,
    simulateOverprint: false,
}

sel.exportFile(ExportFormat.jpg, File(Folder.desktop + fn));

//ungroup the selection
sel.ungroup();


/**
* Get pixel dimension export resolution 
*  the page item to export 
*  the target pixel width 
*  the resolution to export 
* 
*/
function getRes(pi, pd){
    app.scriptPreferences.measurementUnit = MeasurementUnits.INCHES
    var b = pi.geometricBounds;
    var w = b[3]-b[1];
    return pd/w
}


/**
* Group a layer’s page items 
*  the page to target 
*  the layer to group 
*  the group 
* 
*/
function tempGroup(p,l){
    var lyrItems = l.pageItems;
    var gp=[];
    for (var i = 0; i < lyrItems.length; i++){
        if (lyrItems[i].parentPage == p) {
            gp.push(lyrItems[i])
        } 
    }; 
    return app.documents[0].groups.add(gp, l)
}


 

 

 

 

Legend
December 8, 2021

hello @OPTEN 

 

The PageExporterUtility script will do what you're looking for...

https://github.com/paulhennell/InDesign-Page-Export-Utility/blob/master/PageExporterUtility5.0.1.jsx

Regards,

Mike

 

OPTENAuthor
Inspiring
December 8, 2021

Hi Mike, 

 

Thanks for your fast reply. I've already tried to use that Script to change it how I needed it to work but couldn't finish it. Im Working on a new one.

I'll try your suggestion under this entry right now!

 

Hero!