Skip to main content
Known Participant
September 28, 2020
Answered

Script to export selected objects (including grouped) as individual JPEGs at 144dpi

  • September 28, 2020
  • 3 replies
  • 3224 views

Hi all,

 

I've found variations of this but nothing that hits the spot quite as I need. We use InDesign to design and mockup web page and email designs and subsequently pass on the image assets to our developers to build a HTML page with. Some times we cheat image backgrounds extensions with subtle stretches and re-group, so I need the group to export exactly how it appears in InDesign but as a single JPEG.

 

I'd like a shorcut triggered script that exports all selected objects, including grouped ojects, as individual JPEGs at max quality, at a predefined resolution (in this case @15739213, so 144ppi) and be able to define the save location (though straight to Desktop would be good enough).

 

The below script kind of works but it only exports the groups and breaks the groups down in to individual images.

 

I'd love to learn how to edit this to achieve what I need. Thanks in advance to any that may be able to help.

 

 

test();
function test(){

    var myDoc = app.activeDocument;
    var myGroups = myDoc.groups;

    //for each group...
    for (var i = 0;i < myGroups.length; i++){
        // for each rectangle in the group...
        for(var r = 0; r< myGroups[i].rectangles.length; r++){

             var myRect = myGroups[i].rectangles[r];
               app.jpegExportPreferences.exportResolution = 144;
               app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.MAXIMUM;

               //give it a unique name
               var myFile = new File('~/desktop/newJPG' + myRect.id + '.jpg');

               myRect.exportFile(ExportFormat.JPG, myFile);

               }
           }

  }

 

 

Correct answer Charu Rajput

Hi,

You can incoorporate your above small code as below

test();
function test() {
    var myDoc = app.activeDocument;
    var _pageItems = myDoc.selection;
    var f = new Folder("~/Desktop/Export");
    if (!f.exists) {
        f.create()
    }
    for (var i = 0; i < _pageItems.length; i++) {
        app.jpegExportPreferences.exportResolution = 144;
        app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.MAXIMUM;
        var myFile = new File(f + "/" + _pageItems[i].id + '.jpg');
        _pageItems[i].exportFile(ExportFormat.JPG, myFile);
    }
}

 

I hope this works for you.

3 replies

Jens Trost
Inspiring
September 29, 2020

Did you try this?
https://indesignsecrets.com/new-indesign-script-to-export-jpegs-at-a-precise-size.php

Alternative: I use this snippet (not sure where I got it):

    function exportSelectionAsJpeg(saveFile) {
        var sel = app.selection;

        if (sel == 0) { alert("Please select at least one object"); return; }
        else if (sel.length > 1) {
            app.copy();
            app.pasteInPlace();
            var obj = app.activeDocument.groups.add(app.selection);
            var grp = true;
        } else var obj = app.selection[0];

        app.jpegExportPreferences.exportResolution = 144;
        app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.MAXIMUM;
        app.jpegExportPreferences.jpegRenderingStyle = JPEGOptionsFormat.BASELINE_ENCODING;
        app.jpegExportPreferences.jpegColorSpace = JpegColorSpaceEnum.RGB;
        app.jpegExportPreferences.embedColorProfile = true;
        app.jpegExportPreferences.antiAlias = true;
        app.jpegExportPreferences.simulateOverprint = true;
        obj.exportFile(ExportFormat.JPG, saveFile, false);
        if (grp) obj.remove();
    }

 

Known Participant
September 29, 2020

Hi Jens,

 

I did try that script and it's brilliant, but unfortunately it's a non-starter as it requires one to specify dimensions. I already have my images at the correct dimensions, I just need a double-size 144 version of them output.

 

Thank you for the alternative snippet. I'm embarassed to say I have know idea where to paste it though. If I pasted only that snippet in to a .jsx file then nothing happens when I run it. If I replace everything under "test();" then I get an error: "test is not a function".

 

 

Jens Trost
Inspiring
September 29, 2020

Hi,

sorry I forgot the execution (first line), put this into a .jsx file, you just need to edit the path within the quotes:

exportSelectionAsJpeg("/Your/Path/Where/You/Wanna/Save.jpg");

function exportSelectionAsJpeg(saveFile) {
        var sel = app.selection;

        if (sel == 0) { alert("Please select at least one object"); return; }
        else if (sel.length > 1) {
            app.copy();
            app.pasteInPlace();
            var obj = app.activeDocument.groups.add(app.selection);
            var grp = true;
        } else var obj = app.selection[0];

        app.jpegExportPreferences.exportResolution = 144;
        app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.MAXIMUM;
        app.jpegExportPreferences.jpegRenderingStyle = JPEGOptionsFormat.BASELINE_ENCODING;
        app.jpegExportPreferences.jpegColorSpace = JpegColorSpaceEnum.RGB;
        app.jpegExportPreferences.embedColorProfile = true;
        app.jpegExportPreferences.antiAlias = true;
        app.jpegExportPreferences.simulateOverprint = true;
        obj.exportFile(ExportFormat.JPG, saveFile, false);
        if (grp) obj.remove();
    }

 

Inspiring
September 28, 2020

It might work to move the selected objects to a separate layer and hide the other layers before export. But that can get complicated if the stacking order of the selected objects is important. And it usually is. Any chance of building this idea into the workflow? If the designer sets up one layer for each jpeg-to-be, you'd have better control over the stacking order. 

Othewise, I think you're talking about a fairly complicated script that would temporarily move the selected items to an "export" layer, then moving them back while preserving the relative stacking arrangement both times. 

Bob

Known Participant
September 29, 2020

Thanks for your input Bob.

 

That all sounds far more complicated than I think it needs to be. Charu's simple script above is almost perfect, in fact it is perfect, I'd just like to be prompted for the save location OR save to a folder called "Export" on my Mac's desktop and create that "Export" folder if it's not already there.

 

I think this is what I need but I'm not sure where/how I need to add it to what @Charu Rajput provided:

 

var f = new Folder("~/Desktop/Export");
if ( ! f.exists ) {
	f.create()
}

 

 

 

 

 

 

Charu Rajput
Charu RajputCorrect answer
Community Expert
September 29, 2020

Hi,

You can incoorporate your above small code as below

test();
function test() {
    var myDoc = app.activeDocument;
    var _pageItems = myDoc.selection;
    var f = new Folder("~/Desktop/Export");
    if (!f.exists) {
        f.create()
    }
    for (var i = 0; i < _pageItems.length; i++) {
        app.jpegExportPreferences.exportResolution = 144;
        app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.MAXIMUM;
        var myFile = new File(f + "/" + _pageItems[i].id + '.jpg');
        _pageItems[i].exportFile(ExportFormat.JPG, myFile);
    }
}

 

I hope this works for you.

Best regards
Charu Rajput
Community Expert
September 28, 2020

Hi,

Try following snippet

 

test();
function test() {
    var myDoc = app.activeDocument;
    var _pageItems = myDoc.pageItems;
    for (var i = 0; i < _pageItems.length; i++) {
        app.jpegExportPreferences.exportResolution = 144;
        app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.MAXIMUM;
        var myFile = new File('~/desktop/newJPG' + _pageItems[i].id + '.jpg');
        _pageItems[i].exportFile(ExportFormat.JPG, myFile);
    }
}

 

The above snippet will export all items present in the document.

 

Also, you can learn about scripting from the following

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#about.html

Best regards
Known Participant
September 28, 2020

Thanks for such a quick response Charu.

 

You weren't kidding about "all items" were you! 😅 I'm definitely going to need to be able to limit it to selected items only.

 

Also, it's picking up random white space from outside of the groups.

 

 

 

Charu Rajput
Community Expert
September 29, 2020

To get selectedItems, you can use following line

var _pageItems = myDoc.selection;

instead of 

var _pageItems = myDoc.pageItems;

So, complete script for selected items will be 

test();
function test() {
    var myDoc = app.activeDocument;
    var _pageItems = myDoc.selection;
    for (var i = 0; i < _pageItems.length; i++) {
        app.jpegExportPreferences.exportResolution = 144;
        app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.MAXIMUM;
        var myFile = new File('~/desktop/newJPG' + _pageItems[i].id + '.jpg');
        _pageItems[i].exportFile(ExportFormat.JPG, myFile);
    }
}

For random white spaces - I could not see any random white spaces. Could you please share your sample document to look more for this issue. I have tested with simple scenario, and the result from this script is same as the result of "Export".

 

Best regards