Skip to main content
andrews96921672
Known Participant
December 21, 2023
Answered

Looking for help with batch relinking script for images with the same suffix

  • December 21, 2023
  • 3 replies
  • 1356 views

I want to be able to batch relink all images in an indesign file based on the  - 1.jpg, - 2.jpg, etc suffix but ignore the part number.

 

example would be the images that are linked in the indesign file are currently: 

in folder named by part number 23238
23238 - 1.jpg

23238 - 2.jpg

23238 - 3.jpg

etc

 

I want to be able to duplicate the InDesign file, rename it to a new part number, open it, and run a script that will allow me to select the folder of the new part number images to relink and it batch relink them based on the suffix so it places the same images of the new folder in the same slot.

 

The new folder images would be named 23239:
23239 - 1.jpg

23239 - 2.jpg

23239 - 3.jpg

etc

 

So the first 5 numbers would be different but the suffix is the same. Is there a way to batch-relink these instead of having to relink one at a time?

 

 

This topic has been closed for replies.
Correct answer m1b

Hi @andrews96921672, in this case I have a function that was ready to go. I just added a bit for selecting your new part number folder. Let me know if this is what you had in mind. Feel free to adjust the script if I haven't understood your requirements right.

- Mark

 

 

/**
 * Re-links part number files to a new folder
 * that is named for the part number.
 * Usage: open the document, run script, select
 * new part folder, eg. "12345", and script will
 * NOTE: the method for deriving the old part number
 * is simplistic and might need to be refined.
 * relink "my/link/path/25247/subfolder/subfolder/25247 - 1.jpg"
 *     to "my/link/path/12345/subfolder/subfolder/12345 - 1.jpg"
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/looking-for-help-with-batch-relinking-script-for-images-with-the-same-suffix/m-p/14312976
 */

function main() {

    var doc = app.activeDocument,
        oldPartNumber = getOldPartNumber(doc);

    if (!oldPartNumber)
        return alert('Could not get old part number from document.');

    var newPartNumber = getFirstParentNumberFolder();

    if (!newPartNumber)
        return alert('Could not get new part number from that folder.');

    // perform the find change
    findChangeLinkPath(
        // the document to search in
        doc,
        // find the existing part number
        new RegExp('\\b' + oldPartNumber + '\\b', 'g'),
        // change to the new part number
        newPartNumber,
        // update the links
        true,
        // show results alert
        true,
    );


    /**
     * Returns the new part number
     * by asking user for folder and
     * searching (node to root) for
     * first folder which is a number.
     * @returns {Number}
     */
    function getFirstParentNumberFolder() {

        var folder = Folder.selectDialog("Select part number folder:");

        if (null === folder)
            return;

        // look for folder whose name is a number
        while (
            undefined != folder.parent
            && String(Number(folder.name)) != folder.name
        )
            folder = folder.parent;

        if (undefined == folder.parent)
            return

        // expects folder name to be the part number
        var partNumber = Number(folder.name);

        if (isNaN(partNumber))
            return;

        return partNumber;

    };

    /**
     * Returns a five-digit part number.
     * Important: it works by returning
     * the first five-digit number found
     * at start of _two_ link filenames.
     * @param {Document} doc
     * @returns {Number}
     */
    function getOldPartNumber(doc) {

        var oldLinkPaths = doc.links.everyItem().linkResourceURI,
            oldPartNumber;

        for (var i = 0; i < oldLinkPaths.length; i++) {

            var num = decodeURI(File(oldLinkPaths[i]).name).match(/^(\d{5})\b/);

            if (
                !num
                || num.length < 1
                || isNaN(num = Number(num[0]))
            )
                continue;

            if (num == oldPartNumber)
                break;

            oldPartNumber = num;

        }

        return num;

    };

};

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Change Part Number");


/**
 * Performs find/change on link paths of document.
 * @author m1b
 * @version 2022-02-07
 * @param {Document} doc - an Indesign Document.
 * @param {RegExp|String} findWhat - RegExp or string to find in link path.
 * @param {RegExp|String} changeTo - replace with this.
 * @param {Boolean} [updateLinks] - whether to update the links (default: true).
 * @param {Boolean} [showResults] - whether to display a count of the changes (default: false).
 */
function findChangeLinkPath(doc, findWhat, changeTo, updateLinks, showResults) {

    updateLinks = false !== updateLinks;
    showResults = true === showResults;

    var links = doc.links,
        counter = 0;

    for (var i = 0; i < links.length; i++) {

        var linkURI = links[i].linkResourceURI,
            newLinkedURI = linkURI.replace(findWhat, changeTo);

        if (linkURI == newLinkedURI)
            continue;

        links[i].reinitLink(newLinkedURI);
        counter++

        if (
            updateLinks
            && LinkStatus.LINK_OUT_OF_DATE === links[i].status
        )
            links[i].update();

    }

    if (showResults)
        alert('Reinit ' + counter + ' out of ' + links.length + ' links.');

};

 

Edit 2023-12-22: After better understanding OP's file structure, adjusted script to derive the old part number from the document, then change to the new part number in all link paths.

3 replies

m1b
m1bCorrect answer
Community Expert
December 21, 2023

Hi @andrews96921672, in this case I have a function that was ready to go. I just added a bit for selecting your new part number folder. Let me know if this is what you had in mind. Feel free to adjust the script if I haven't understood your requirements right.

- Mark

 

 

/**
 * Re-links part number files to a new folder
 * that is named for the part number.
 * Usage: open the document, run script, select
 * new part folder, eg. "12345", and script will
 * NOTE: the method for deriving the old part number
 * is simplistic and might need to be refined.
 * relink "my/link/path/25247/subfolder/subfolder/25247 - 1.jpg"
 *     to "my/link/path/12345/subfolder/subfolder/12345 - 1.jpg"
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/looking-for-help-with-batch-relinking-script-for-images-with-the-same-suffix/m-p/14312976
 */

function main() {

    var doc = app.activeDocument,
        oldPartNumber = getOldPartNumber(doc);

    if (!oldPartNumber)
        return alert('Could not get old part number from document.');

    var newPartNumber = getFirstParentNumberFolder();

    if (!newPartNumber)
        return alert('Could not get new part number from that folder.');

    // perform the find change
    findChangeLinkPath(
        // the document to search in
        doc,
        // find the existing part number
        new RegExp('\\b' + oldPartNumber + '\\b', 'g'),
        // change to the new part number
        newPartNumber,
        // update the links
        true,
        // show results alert
        true,
    );


    /**
     * Returns the new part number
     * by asking user for folder and
     * searching (node to root) for
     * first folder which is a number.
     * @returns {Number}
     */
    function getFirstParentNumberFolder() {

        var folder = Folder.selectDialog("Select part number folder:");

        if (null === folder)
            return;

        // look for folder whose name is a number
        while (
            undefined != folder.parent
            && String(Number(folder.name)) != folder.name
        )
            folder = folder.parent;

        if (undefined == folder.parent)
            return

        // expects folder name to be the part number
        var partNumber = Number(folder.name);

        if (isNaN(partNumber))
            return;

        return partNumber;

    };

    /**
     * Returns a five-digit part number.
     * Important: it works by returning
     * the first five-digit number found
     * at start of _two_ link filenames.
     * @param {Document} doc
     * @returns {Number}
     */
    function getOldPartNumber(doc) {

        var oldLinkPaths = doc.links.everyItem().linkResourceURI,
            oldPartNumber;

        for (var i = 0; i < oldLinkPaths.length; i++) {

            var num = decodeURI(File(oldLinkPaths[i]).name).match(/^(\d{5})\b/);

            if (
                !num
                || num.length < 1
                || isNaN(num = Number(num[0]))
            )
                continue;

            if (num == oldPartNumber)
                break;

            oldPartNumber = num;

        }

        return num;

    };

};

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Change Part Number");


/**
 * Performs find/change on link paths of document.
 * @author m1b
 * @version 2022-02-07
 * @param {Document} doc - an Indesign Document.
 * @param {RegExp|String} findWhat - RegExp or string to find in link path.
 * @param {RegExp|String} changeTo - replace with this.
 * @param {Boolean} [updateLinks] - whether to update the links (default: true).
 * @param {Boolean} [showResults] - whether to display a count of the changes (default: false).
 */
function findChangeLinkPath(doc, findWhat, changeTo, updateLinks, showResults) {

    updateLinks = false !== updateLinks;
    showResults = true === showResults;

    var links = doc.links,
        counter = 0;

    for (var i = 0; i < links.length; i++) {

        var linkURI = links[i].linkResourceURI,
            newLinkedURI = linkURI.replace(findWhat, changeTo);

        if (linkURI == newLinkedURI)
            continue;

        links[i].reinitLink(newLinkedURI);
        counter++

        if (
            updateLinks
            && LinkStatus.LINK_OUT_OF_DATE === links[i].status
        )
            links[i].update();

    }

    if (showResults)
        alert('Reinit ' + counter + ' out of ' + links.length + ' links.');

};

 

Edit 2023-12-22: After better understanding OP's file structure, adjusted script to derive the old part number from the document, then change to the new part number in all link paths.

andrews96921672
Known Participant
December 21, 2023

I created a new scipit and copy pasted this  and ran it and it prompts me to select a folder which I pick the new folder that the new images are in and press ok and nothing happens from there. 

m1b
Community Expert
December 21, 2023

Is the new folder you chose called the part number (with nothing else)? eg. "12345"? Also did you choose a folder? It currently only works if you choose a folder (not a file).

Community Expert
December 21, 2023

This search:

indesign relink images script

produces many links. You may have to tweak the file-naming specifics, but that should be easy enough.

 

And maybe you could have a look at Kasian Servetsky's site: http://kasyan.ho.ua/

Community Expert
December 21, 2023

There are numerous scripts around that deal with (batch-)relinking of files. Google around and you're bound to find several.

andrews96921672
Known Participant
December 21, 2023

haha been doing that for about 3 days and still haven't found what I'm looking for which is why im asking here.

 

If you could help point me to one you think might be SPECIFIC to the SPECFIFC question I asked, then please let me know! Thanks.