• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Explorer ,
Sep 28, 2020 Sep 28, 2020

Copy link to clipboard

Copied

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 @2X, 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);

               }
           }

  }

 

 

TOPICS
How to , Import and export , Scripting

Views

1.4K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 29, 2020 Sep 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');
  
...

Votes

Translate

Translate
Community Expert ,
Sep 28, 2020 Sep 28, 2020

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 28, 2020 Sep 28, 2020

Copy link to clipboard

Copied

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.

lcooperdesign85_0-1601311954396.png

 

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

 

lcooperdesign85_1-1601312058167.png

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 28, 2020 Sep 28, 2020

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 29, 2020 Sep 29, 2020

Copy link to clipboard

Copied

Okay, it was a bit of a head-scratcher but this is now working perfectly, thank you!

 

The random white spaces were caused by a drop shadow that was applied to grouped objects. In fact, this process highlighted a bug caused by having the Aquafades plug-in installed. Every newly created group had a drop shadow automatically applied by default (nothing to do with default object settings). I couldn't shake it but once uninstalled the removal of drop shadow effects stuck and so the white space turned out to be a completely unrelated issue. Phew/sorry!

 

The only remaining improvement I'd like would be to have the images be exported to a folder on the desktop (that may or may not exist yet) rather than just scatter the files (albeit far fewer now) on the desktop.

Would you be able to help with that?

 

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 28, 2020 Sep 28, 2020

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 29, 2020 Sep 29, 2020

Copy link to clipboard

Copied

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()
}

 

 

 

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 29, 2020 Sep 29, 2020

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 29, 2020 Sep 29, 2020

Copy link to clipboard

Copied

I can't thank you enough @Charu Rajput, it works wonderfully!

 

I really appreciate your time helping me crack this, I'm about to become very popular amongst our team of designers!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 29, 2020 Sep 29, 2020

Copy link to clipboard

Copied

LATEST

You're welcome 🙂

And congrats to become a popular in your team.. 🙂 

Best regards

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Sep 29, 2020 Sep 29, 2020

Copy link to clipboard

Copied

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();
    }

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 29, 2020 Sep 29, 2020

Copy link to clipboard

Copied

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".

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Sep 29, 2020 Sep 29, 2020

Copy link to clipboard

Copied

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();
    }

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 29, 2020 Sep 29, 2020

Copy link to clipboard

Copied

Thanks @Jens Trost.

 

Afraid I can't get this to work either. I kept the double-quotes from your example and also tried single quotes but neither worked. Also, it appears this attempts to export everything selected as a single JPEG, whereas I need individual JPEGs from each object or group. @Charu Rajput solution works and is 95% what I need so far.

 

lcooperdesign85_0-1601379062661.png

Screenshot 2020-09-29 at 13.33.05.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 29, 2020 Sep 29, 2020

Copy link to clipboard

Copied

@lcooperdesign85 - I have added your code in my script above.

Best regards

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines