Skip to main content
Participant
Nov 1, 2021 6:59 PM UTC
Answered

Export multiple cropped images on a spread with original linked names and specific dimensions

I am looking to export multiple cropped images on a spread with original linked names and specific dimensions. Do I need a a script for this? Has anyone done this? 

 

I'll attach a preview so you can see the spread with the containers. 

m1b
m1bCorrect answer
Community Expert
Nov 6, 2021 8:25 AM UTC

Hi @kevinfabrizi,

I've had a crack at writing a script to solve your challenge. Let me know if this works for you. I've only tested on basic documents, so it might need some tweaking to handle less basic cases.

- Mark

 

/*

    Export All Graphics As JPG
    by m1b
    here: https://community.adobe.com/t5/indesign-discussions/export-multiple-cropped-images-on-a-spread-with-original-linked-names-and-specific-dimensions/m-p/12490323

    Will attempt to export every graphic in a document to a folder, as JPG image at set resolution.
    Filename has pixel dimensions appended.

*/

function main() {

    var exportResolution = 300; // ppi

    exportAllLinkedImagesAsJPG(exportResolution);

    function exportAllLinkedImagesAsJPG(resolution) {
        resolution = resolution || 300;
        var graphics = app.activeDocument.allGraphics;
        for (var i = 0; i < graphics.length; i++) {
            var graphic = graphics[i],
                frame = graphic.parent,
                w = Math.round((frame.visibleBounds[3] - frame.visibleBounds[1]) * (resolution / 72)),
                h = Math.round((frame.visibleBounds[2] - frame.visibleBounds[0]) * (resolution / 72)),
                filename = getFileNameForItem(frame) + '_' + w + 'x' + h;
            exportItemAsJPG({
                item: frame,
                filename: filename,
                resolution: resolution,
                exportPath: '~/Desktop/exported-jpg-' + resolution + 'ppi/'
            });
        }
    }

    function exportItemAsJPG(p) {
        if (p.item == undefined) return;
        p.filename = p.filename || p.item.constructor.name + ' ' + p.item.index;
        p.resolution = p.resolution || 300;
        p.exportPath = p.exportPath || '~/Desktop/exported-jpg-' + p.resolution + 'ppi/';

        // create the folder if necessary
        if (!Folder(p.exportPath).exists) Folder(p.exportPath).create();

        // set jpeg preferences
        app.jpegExportPreferences.exportResolution = p.resolution; // ppi
        app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.high; // low medium high maximum

        // export
        var path = p.exportPath + p.filename + '.jpg';
        p.item.exportFile(
            ExportFormat.jpg,
            File(path),
            false
        );
    }

    function getFileNameForItem(item, keepExtension) {
        var name = item.constructor.name + ' ' + item.id;
        if (item.graphics != undefined) {
            if (item.graphics[0].itemLink != undefined) {
                name = item.graphics[0].itemLink.name.replace(/\.[^.]+$/, '');
            }
        }
        return name;
    }


} // end main

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Export graphics');

8 replies

Willi Adelberger
Community Expert
Community Expert
Nov 6, 2021 11:51 AM UTC
  1. Set up the object caption. Object > Object captions
  2. When you place images activate create static captions

Design and position can be done with Object and Paragraph Styles.

Translate
m1b
Community Expert
Community Expert
Nov 6, 2021 7:51 PM UTC

Nice! I've never used object captions. Great info!

 

Also I assumed OP wanted the linked image name and dimensions in the *filename*, but you may be right and it should be saved in the jpeg as a visible caption. I'll wait for @kevinfabrizi to clarify.

- Mark

Translate
m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
Nov 6, 2021 8:25 AM UTC

Hi @kevinfabrizi,

I've had a crack at writing a script to solve your challenge. Let me know if this works for you. I've only tested on basic documents, so it might need some tweaking to handle less basic cases.

- Mark

 

/*

    Export All Graphics As JPG
    by m1b
    here: https://community.adobe.com/t5/indesign-discussions/export-multiple-cropped-images-on-a-spread-with-original-linked-names-and-specific-dimensions/m-p/12490323

    Will attempt to export every graphic in a document to a folder, as JPG image at set resolution.
    Filename has pixel dimensions appended.

*/

function main() {

    var exportResolution = 300; // ppi

    exportAllLinkedImagesAsJPG(exportResolution);

    function exportAllLinkedImagesAsJPG(resolution) {
        resolution = resolution || 300;
        var graphics = app.activeDocument.allGraphics;
        for (var i = 0; i < graphics.length; i++) {
            var graphic = graphics[i],
                frame = graphic.parent,
                w = Math.round((frame.visibleBounds[3] - frame.visibleBounds[1]) * (resolution / 72)),
                h = Math.round((frame.visibleBounds[2] - frame.visibleBounds[0]) * (resolution / 72)),
                filename = getFileNameForItem(frame) + '_' + w + 'x' + h;
            exportItemAsJPG({
                item: frame,
                filename: filename,
                resolution: resolution,
                exportPath: '~/Desktop/exported-jpg-' + resolution + 'ppi/'
            });
        }
    }

    function exportItemAsJPG(p) {
        if (p.item == undefined) return;
        p.filename = p.filename || p.item.constructor.name + ' ' + p.item.index;
        p.resolution = p.resolution || 300;
        p.exportPath = p.exportPath || '~/Desktop/exported-jpg-' + p.resolution + 'ppi/';

        // create the folder if necessary
        if (!Folder(p.exportPath).exists) Folder(p.exportPath).create();

        // set jpeg preferences
        app.jpegExportPreferences.exportResolution = p.resolution; // ppi
        app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.high; // low medium high maximum

        // export
        var path = p.exportPath + p.filename + '.jpg';
        p.item.exportFile(
            ExportFormat.jpg,
            File(path),
            false
        );
    }

    function getFileNameForItem(item, keepExtension) {
        var name = item.constructor.name + ' ' + item.id;
        if (item.graphics != undefined) {
            if (item.graphics[0].itemLink != undefined) {
                name = item.graphics[0].itemLink.name.replace(/\.[^.]+$/, '');
            }
        }
        return name;
    }


} // end main

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Export graphics');
Translate
Inspiring
Jul 21, 2023 3:33 PM UTC

Very helpful @m1b!
The exportItemAsJPG() function has certainly saved me a lot of time when creating a similar export script.
Thank you very much!
Martin 

Translate
m1b
Community Expert
Community Expert
Jul 21, 2023 8:04 PM UTC

Good to hear, Martin! - Mark

Translate
Participant
Nov 1, 2021 6:59 PM UTC
Screen_Shot_2021-11-01_at_2-57-37_PM.png
Translate