Skip to main content
Participant
November 1, 2021
Answered

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

  • November 1, 2021
  • 3 replies
  • 1566 views

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. 

Correct answer m1b

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

3 replies

Willi Adelberger
Community Expert
Community Expert
November 6, 2021
  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.

m1b
Community Expert
Community Expert
November 6, 2021

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

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
November 6, 2021

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');
Inspiring
July 21, 2023

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 

m1b
Community Expert
Community Expert
July 21, 2023

Good to hear, Martin! - Mark

Participant
November 1, 2021
Screen_Shot_2021-11-01_at_2-57-37_PM.png