Skip to main content
Participating Frequently
May 2, 2022
Answered

Relink an image to a file that does not exist

  • May 2, 2022
  • 3 replies
  • 2135 views

Is there any way to relink a placed image to a file that does not exist yet? I have an INDD file with a bunch of other INDD files placed as links. I want to replace each of those INDD links with a PDF of the exact same name, but a .pdf extension. This script works when the PDF version of the file already exists:

 

var links = app.activeDocument.links,
    linksLength = links.length;

    for (var i = linksLength-1; i >= 0 ; i--) {
		var oldLink = links[i];
        if(oldLink.linkType == "InDesign Format Name") {
            var newLink = new File(oldLink.filePath.replace(".indd",".pdf"));
            oldLink.relink(newLink);
        }
    }

 

But in my workflow, the PDFs are not yet created so running this script fails because the newLink does not yet exist. I want them to show as missing for now.

 

Maybe, instead of a "relink", what I need is just some way to change the name of the link that InDesign is looking for without first confirming that it exists. 

 

Any ideas? Thanks!

This topic has been closed for replies.
Correct answer m1b

Actually you can do it, with reinitLink method of Link. See this script I wrote for another answer previously:

/*
    Reinit Links
    for Adobe Indesign 2022
    by m1b

    Use this when your file system has changed and current links are broken.
    Set `replaceThis` and `withThis` to re-align the file paths.

    This is different to Relinker because it doesn't require
    modified links to exist.
*/

function main() {

    relinker(

        // the document to relink
        app.activeDocument,

        // in each link path, replace this string or a regex
        /\.indd/,

        // with this string
        '.pdf'

    )


    function relinker(doc, replaceThis, withThis) {
        var links = doc.links,
            counter = 0;

        for (var i = 0; i < links.length; i++) {
            var linkURI = links[i].linkResourceURI,
                newLinkedURI = linkURI.replace(replaceThis, withThis);
            links[i].reinitLink(newLinkedURI);
            if (linkURI != newLinkedURI)
                counter++
        }

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

}


app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Reinit Links");

 

All it does is replace .indd with .pdf and "reinit" the links which, unlike "relink", doesn't require a valid file spec.

- Mark

3 replies

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
May 2, 2022

Actually you can do it, with reinitLink method of Link. See this script I wrote for another answer previously:

/*
    Reinit Links
    for Adobe Indesign 2022
    by m1b

    Use this when your file system has changed and current links are broken.
    Set `replaceThis` and `withThis` to re-align the file paths.

    This is different to Relinker because it doesn't require
    modified links to exist.
*/

function main() {

    relinker(

        // the document to relink
        app.activeDocument,

        // in each link path, replace this string or a regex
        /\.indd/,

        // with this string
        '.pdf'

    )


    function relinker(doc, replaceThis, withThis) {
        var links = doc.links,
            counter = 0;

        for (var i = 0; i < links.length; i++) {
            var linkURI = links[i].linkResourceURI,
                newLinkedURI = linkURI.replace(replaceThis, withThis);
            links[i].reinitLink(newLinkedURI);
            if (linkURI != newLinkedURI)
                counter++
        }

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

}


app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Reinit Links");

 

All it does is replace .indd with .pdf and "reinit" the links which, unlike "relink", doesn't require a valid file spec.

- Mark

brian_p_dts
Community Expert
Community Expert
May 2, 2022

TIL about reinitLink 

 

Thanks, Mark! 

brian_p_dts
Community Expert
Community Expert
May 2, 2022

Hmmm don't think this is possible. I might write a script that removes the old INDD graphic and adds the potential PDF path to the containing frame's label, then a separate script to place file by label. 

BobLevine
Community Expert
Community Expert
May 2, 2022

Now you have me thinking and I don't know if this is possible, but what if you exported an IDML file and edited it to change the file extensions of the links?

Community Expert
May 2, 2022

Good idea, Bob!

One could also do a quick test with an IDMS file that is exported from a frame containing an InDesign page.

Hm. Maybe it would make a difference for the experiment if the InDesign document that is placed contains more than one page? ( Just a note to myself … )

 

Regards,
Uwe Laubender

( ACP )

BobLevine
Community Expert
Community Expert
May 2, 2022
Unless I'm missing something, you don't need a script for this. You can use the "relink file extension" feature in the links panel.
Participating Frequently
May 2, 2022

That has the same limitation that it first looks to see if the file with the new extension exists. Also, I will need to do this for thousands of links in hundreds of files.

BobLevine
Community Expert
Community Expert
May 2, 2022

Yeah, miss that part. Sorry, but I think you're going to have to actually generate those files first.