Sair
  • Comunidade global
    • Idioma:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
Bloqueada
0

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

Explorador ,
Sep 28, 2020 Sep 28, 2020

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

               }
           }

  }

 

 

TÓPICOS
Como fazer , Importar e exportar , Scripts
3.0K
Traduzir
Denunciar
Diretrizes da comunidade
Seja respeitoso, dê crédito à fonte original do conteúdo e verifique se há cópias antes da publicação. Saiba mais
community guidelines

correct answers 1 resposta correta

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');
  
...
Traduzir
Community Expert ,
Sep 28, 2020 Sep 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
Traduzir
Denunciar
Diretrizes da comunidade
Seja respeitoso, dê crédito à fonte original do conteúdo e verifique se há cópias antes da publicação. Saiba mais
community guidelines
Explorador ,
Sep 28, 2020 Sep 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.

lcooperdesign85_0-1601311954396.png

 

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

 

lcooperdesign85_1-1601312058167.png

 

 

Traduzir
Denunciar
Diretrizes da comunidade
Seja respeitoso, dê crédito à fonte original do conteúdo e verifique se há cópias antes da publicação. Saiba mais
community guidelines
Community Expert ,
Sep 28, 2020 Sep 28, 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
Traduzir
Denunciar
Diretrizes da comunidade
Seja respeitoso, dê crédito à fonte original do conteúdo e verifique se há cópias antes da publicação. Saiba mais
community guidelines
Explorador ,
Sep 29, 2020 Sep 29, 2020

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?

 

 

 

 

Traduzir
Denunciar
Diretrizes da comunidade
Seja respeitoso, dê crédito à fonte original do conteúdo e verifique se há cópias antes da publicação. Saiba mais
community guidelines
Envolvido ,
Sep 28, 2020 Sep 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

Traduzir
Denunciar
Diretrizes da comunidade
Seja respeitoso, dê crédito à fonte original do conteúdo e verifique se há cópias antes da publicação. Saiba mais
community guidelines
Explorador ,
Sep 29, 2020 Sep 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()
}

 

 

 

 

 

 

Traduzir
Denunciar
Diretrizes da comunidade
Seja respeitoso, dê crédito à fonte original do conteúdo e verifique se há cópias antes da publicação. Saiba mais
community guidelines
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');
        _pageItems[i].exportFile(ExportFormat.JPG, myFile);
    }
}

 

I hope this works for you.

Best regards
Traduzir
Denunciar
Diretrizes da comunidade
Seja respeitoso, dê crédito à fonte original do conteúdo e verifique se há cópias antes da publicação. Saiba mais
community guidelines
Explorador ,
Sep 29, 2020 Sep 29, 2020

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!

Traduzir
Denunciar
Diretrizes da comunidade
Seja respeitoso, dê crédito à fonte original do conteúdo e verifique se há cópias antes da publicação. Saiba mais
community guidelines
Community Expert ,
Sep 29, 2020 Sep 29, 2020

You're welcome 🙂

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

Best regards
Traduzir
Denunciar
Diretrizes da comunidade
Seja respeitoso, dê crédito à fonte original do conteúdo e verifique se há cópias antes da publicação. Saiba mais
community guidelines
Iniciante na comunidade ,
Sep 16, 2025 Sep 16, 2025

Hi, I'm a little late to the party, but this works perfectly for what I need too, but is there a way I can get it to work to export transparent pngs too?

I've edited the script but with limited the coding knowledge no matter what I do I can't get it to run the same as the jpg one?
Sidequest: is there a way for it to save down the exported asset width in the finename such as xxxx_500.png?

Any answers would be greatly appreciated?!

Traduzir
Denunciar
Diretrizes da comunidade
Seja respeitoso, dê crédito à fonte original do conteúdo e verifique se há cópias antes da publicação. Saiba mais
community guidelines
Envolvido ,
Sep 17, 2025 Sep 17, 2025

@Jubilant_Flair8342 
Try this code:

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.pngExportPreferences.exportResolution = 144;
        app.pngExportPreferences.pngQuality = PNGQualityEnum.MAXIMUM;
        var myFile = new File(f + "/" + _pageItems[i].id + '.png');
        _pageItems[i].exportFile(ExportFormat.PNG_FORMAT, myFile);
    }
}
Thanks,
Prabu
Design smarter, faster, and bolder with InDesign scripting.
Traduzir
Denunciar
Diretrizes da comunidade
Seja respeitoso, dê crédito à fonte original do conteúdo e verifique se há cópias antes da publicação. Saiba mais
community guidelines
Iniciante na comunidade ,
Sep 17, 2025 Sep 17, 2025
MAIS RECENTE

Thank you!

Traduzir
Denunciar
Diretrizes da comunidade
Seja respeitoso, dê crédito à fonte original do conteúdo e verifique se há cópias antes da publicação. Saiba mais
community guidelines
Entusiasta ,
Sep 29, 2020 Sep 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();
    }

 

Traduzir
Denunciar
Diretrizes da comunidade
Seja respeitoso, dê crédito à fonte original do conteúdo e verifique se há cópias antes da publicação. Saiba mais
community guidelines
Explorador ,
Sep 29, 2020 Sep 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".

 

 

Traduzir
Denunciar
Diretrizes da comunidade
Seja respeitoso, dê crédito à fonte original do conteúdo e verifique se há cópias antes da publicação. Saiba mais
community guidelines
Entusiasta ,
Sep 29, 2020 Sep 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();
    }

 

Traduzir
Denunciar
Diretrizes da comunidade
Seja respeitoso, dê crédito à fonte original do conteúdo e verifique se há cópias antes da publicação. Saiba mais
community guidelines
Explorador ,
Sep 29, 2020 Sep 29, 2020

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

Traduzir
Denunciar
Diretrizes da comunidade
Seja respeitoso, dê crédito à fonte original do conteúdo e verifique se há cópias antes da publicação. Saiba mais
community guidelines
Community Expert ,
Sep 29, 2020 Sep 29, 2020

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

Best regards
Traduzir
Denunciar
Diretrizes da comunidade
Seja respeitoso, dê crédito à fonte original do conteúdo e verifique se há cópias antes da publicação. Saiba mais
community guidelines