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

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

Explorer ,
Dec 21, 2023 Dec 21, 2023

Copy link to clipboard

Copied

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?

 

 

TOPICS
Scripting

Views

371

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 , Dec 21, 2023 Dec 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 pa
...

Votes

Translate

Translate
Community Expert ,
Dec 21, 2023 Dec 21, 2023

Copy link to clipboard

Copied

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

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 ,
Dec 21, 2023 Dec 21, 2023

Copy link to clipboard

Copied

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. 

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 ,
Dec 21, 2023 Dec 21, 2023

Copy link to clipboard

Copied

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/

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 ,
Dec 21, 2023 Dec 21, 2023

Copy link to clipboard

Copied

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.

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 ,
Dec 21, 2023 Dec 21, 2023

Copy link to clipboard

Copied

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. 

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 ,
Dec 21, 2023 Dec 21, 2023

Copy link to clipboard

Copied

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

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 ,
Dec 21, 2023 Dec 21, 2023

Copy link to clipboard

Copied

I have updated the script to handle selecting a File.

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 ,
Dec 21, 2023 Dec 21, 2023

Copy link to clipboard

Copied

the new folder with the new linked images are in a folder called "no callout images" but i can rename that to the part number if need.

 

I ran the updated script and changed the new folder to the part number and it is giving this error. Sorry my knowledge is limited with scripts, i understand how the work kinda but no clue how to find problems and modify them. I really appreicate your help

 

Screenshot 2023-12-21 at 4.08.22 PM.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
Explorer ,
Dec 21, 2023 Dec 21, 2023

Copy link to clipboard

Copied

Also not sure if this will help but this is how my the files will be set up, except times 100 haha. Its the same callouts on all the imaged just the images need to change due to the imprint on the product changes Screenshot 2023-12-21 at 4.11.48 PM.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 ,
Dec 21, 2023 Dec 21, 2023

Copy link to clipboard

Copied

Ah, I see. I've updated the script to handle that. Note the warning that the way it derives the old part number is simply by getting the first number that starts two link file names. So this may fail if you have other linked files, also starting with a number.

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 ,
Jan 02, 2024 Jan 02, 2024

Copy link to clipboard

Copied

sorry was out for the holidays! this worked perfectly! thank you so much!

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 ,
Jan 02, 2024 Jan 02, 2024

Copy link to clipboard

Copied

Glad to hear!

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 ,
Mar 29, 2024 Mar 29, 2024

Copy link to clipboard

Copied

Heyyyy! So the script worked perfectly for a while to do what I needed it to but today when I'm trying to run it I get a pop-up that says "could not get the old part number from the document". Any ideas on how to fix this? 

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 ,
Mar 29, 2024 Mar 29, 2024

Copy link to clipboard

Copied

Hi @andrews96921672, your initial requirements stated that the part number is a five digit number at the start of the linked files names followed by a space. Is that still the case? Your screen shot does not show any links like that. 
- Mark

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 ,
Mar 29, 2024 Mar 29, 2024

Copy link to clipboard

Copied

LATEST

Good catch! They normally always are 5 digits, these being the first I encountered that arnt and didn't even make that connection. Thanks so much for your quick help!

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 ,
Mar 29, 2024 Mar 29, 2024

Copy link to clipboard

Copied

@andrews96921672 

 

Try this:

var findWhat = "old_part";
var changeTo = "new_part";
var links = doc.links,
for (var i = links.length-1; i>=0; i--) {
    var linkURI = links[i].linkResourceURI,
    newLinkedURI = linkURI.replace(findWhat, changeTo);
    links[i].reinitLink(newLinkedURI);
    links[i].update();
};

 

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