Skip to main content
Inspiring
December 8, 2021
Answered

InDesign: Script for exporting layers to jpg

  • December 8, 2021
  • 2 replies
  • 2301 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

OPTENAuthor
Inspiring
December 9, 2021

The JPEG export exports the entire page or a selection. An InDesign Layer doesn’t have a dimension—it can contain page items that do, which would be pageItem[i].geometricBounds in JS.

 

@Mike Bro ’s script is exporting the entire page with the targeted layer visible, so the export would be the page as the canvas, with the visible Layer’s pageitems positioned on the canvas.

 

If you are trying to export a page item as a JPEG it could be done but I think it would be more complex


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

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!