"Re-Link to new Path" Script

Community Beginner ,
Feb 25, 2022 Feb 25, 2022

Copy link to clipboard

Copied

Hi!

 

we found to move our data from one server to another.

The ending of the path will stay the same. But the servername will change and there is nothing we can do about it.

 

Ive looked up the internet and found a script for InDesign. With this script it is possible to Re-Link links to a new path. It works a treat and is great! If anyone is interested, i got it from here:

http://kasyan.ho.ua/indesign/link/restore_paths_of_links.html

 

We would want to use the same script for Illustrator, if there is one..

If anybody knows a script like this or could help me build one, i'd very much appreciate that..

 

Thank you!

TOPICS
Scripting

Views

112

Likes

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
Adobe Community Professional ,
Feb 25, 2022 Feb 25, 2022

Copy link to clipboard

Copied

Hi @Hannes5CE3, here's a script I wrote that should do what you want. The limitation is that it won't relink unless the file exists in the new path. I honestly couldn't work out a way around this limitation. Would love to know if anyone has solved it.

To use the script, first set the vars "findWhat" and "changeTo" so that when doing a find/change on the path, the result will be a valid path to the correct files. Then just run the script and it will relink all placedItems in the active document. Have a look through the script and you'll see I've added a couple of options that can be passed to the function, eg. if you're using the function in a batch (all open documents, for example) you might want to turn the alert off by setting showResult to false.

- Mark

 

/*
    Relinker.js
    for Adobe Illustrator

    by m1b
    here: https://community.adobe.com/t5/illustrator-discussions/quot-re-link-to-new-path-quot-script/m-p/12776878

    with help from advice found here:
    https://community.adobe.com/t5/illustrator/get-broken-file-path-of-rasteritem/td-p/9794237

    Limitations:
    • only relinks if the new path points to a valid file
    • document must be saved before relinking to ensure
      xmp manifest is up-to-date (script handles this)

    Notes:
    • the find/change is performed on the complete path,
      including filename and file extension
    • findWhat can be String or RegExp
    • onlyChangeBrokenLinks means don't do the relinking
      if the link isn't broken
    • showResults means show an alert with result stats

*/
var findWhat = 'Old Folder',
    changeTo = "New Folder";

findChangeInLinkedFilePaths(app.activeDocument, findWhat, changeTo, false, false, true);

function findChangeInLinkedFilePaths(doc, findWhat, changeTo, onlyChangeBrokenLinks, automaticallySaveDocumentBeforeRelinking, showResults) {
    doc = doc || app.activeDocument;
    if (showResults !== false) showResults = true;

    // must save document before relinking to ensure the xmp manifest is up-to-date
    if (!doc.saved) {
        if (automaticallySaveDocumentBeforeRelinking || confirm('Document "' + doc.name + '" must be saved before relinking. Continue?', false, 'Save Document')) {
            doc.save();
        } else {
            return
        }
    }

    // get all the placedItems
    var items = doc.placedItems,
        relinked = [],
        notFound = [],
        notChanged = [];

    // collect the paths of all the placedItems
    if (ExternalObject.AdobeXMPScript == undefined) ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
    var xmp = new XMPMeta(doc.XMPString),
        manifestItemCount = xmp.countArrayItems(XMPConst.NS_XMP_MM, 'Manifest');

    // sanity check
    if (manifestItemCount != items.length)
        throw ('Aborting: Manifest item count does not match placed item count.');

    for (var i = 0; i < manifestItemCount; i++) {
        // get the path from the document's manifest
        var xpath = 'xmpMM:Manifest[' + (i + 1) + ']/stMfs:reference/stRef:filePath',
            path = xmp.getProperty(XMPConst.NS_XMP_MM, xpath).value,
            newPath = path.replace(findWhat, changeTo),
            filename = newPath.split('/').pop(),
            item = items[i],
            success = false;

        if (path == newPath) {
            notChanged.push(filename);
            continue;
        }

        try {
            var triggerErrorIfBrokenLink = item.file;
            // current link is okay
            if (!onlyChangeBrokenLinks) {
                success = relinkPlacedItem(item, newPath);
            }
        } catch (error) {
            // current link is broken
            success = relinkPlacedItem(item, newPath);
        }

        if (success) {
            relinked.push(filename);
        } else {
            notFound.push(filename);
        }
    }

    // report
    app.redraw();
    if (showResults) {
        var report = ['Relinker results'];
        if (relinked.length > 0) {
            report.push('Relinked:\n' + relinked.join('\n'));
        } else {
            report.push('No files relinked.');
        }
        if (notFound.length > 0)
            report.push('Link didn\'t exist:\n' + notFound.join('\n'));
        if (notChanged.length > 0)
            report.push('Ignored:\n' + notChanged.join('\n'));
        alert(report.join('\n\n'));
    }

    function relinkPlacedItem(item, path) {
        if (File(path).exists) {
            item.file = new File(path);
            return true;
        }
    }
}

Likes

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 Beginner ,
Feb 28, 2022 Feb 28, 2022

Copy link to clipboard

Copied

Hi Mark!

 

wow, i didnt expect such an extensive response! Thank you very much!

 

Sadly, your script currently gives me an error message.

I tried testing it by just changing a foldername in the path were all links in my document are also placed.

But at the moment it tells me that the file or folder does not exist (but it does).

Maybe the XMP Metadata in my ai-file is not set correctly?

 

Bildschirmfoto 2022-02-28 um 15.07.45.png

Likes

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
Adobe Community Professional ,
Mar 01, 2022 Mar 01, 2022

Copy link to clipboard

Copied

Hi @Hannes5CE3, sorry to hear it isn't working. The error you have comes earlier than any of the path stuff. Is your file saved as .ai file? What version of Illustrator are you using? Maybe the path to AdobeXMPScript is different for you... I'll look into it further, but unfortunately it won't be very soon due to my time constraints at the moment.

- Mark

Likes

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 Beginner ,
Mar 02, 2022 Mar 02, 2022

Copy link to clipboard

Copied

Hi @m1b,

 

yeah we are working with .ai files and are using AI 25.4.1 (CC21).

Thank you very much for your help!

Likes

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
Adobe Community Professional ,
Mar 14, 2022 Mar 14, 2022

Copy link to clipboard

Copied

LATEST

Hi @Hannes5CE3, could you please try just this script and see if you get the same error message?

- Mark

 

var result = ExternalObject.search("lib:AdobeXMPScript");
alert('result = '+result);

 

Likes

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