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

"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

1.1K

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 Expert ,
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;
        }
    }
}

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

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

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 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!

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

Copy link to clipboard

Copied

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

 

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 ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

@Hannes5CE3, it seems the error you saw was a bug specific to AI CC2021 and fixed in 2022. Did you get it working?

- 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
New Here ,
May 11, 2023 May 11, 2023

Copy link to clipboard

Copied

Hi @m1b 
Thanks for this script. It works pretty well for me on most files. Is there a way for it to ignore embedded links in a file? Being a link they error out because the placed item count doesnt match the manifest count. With any embedded images it works great!
Cheers

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

Copy link to clipboard

Copied

Hi @Christopher29467334hk54, if you get a chance, can you post a sample file showing the problem? Save as pdf because the forum doesn't allow attaching .ai files.

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
New Here ,
Nov 06, 2023 Nov 06, 2023

Copy link to clipboard

Copied

LATEST

In case you still need this @Christopher29467334hk54, I just modified @m1b 's solution to skip files that don't include the search term in the path which ignores embedded files.

 

Remove the sanity check and replace the item loop to check for the search path before replacing:

    // sanity check
    // if (manifestItemCount != items.length)
    //     throw ('Aborting: Manifest item count does not match placed item count.');
    
    var j = 0; // count doc items independtly from manifest items
    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);
        // skip if manifest path does not contain search term
        if(path.indexOf(findWhat)==-1) {
            continue;
        }
        var filename = newPath.split('/').pop(),
            item = items[j], // index items with j
            success = false;
        
        j++; // increase j if search term exists

        if (path == newPath) {
            notChanged.push(filename);
            continue;
        }
        try {
            // 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);
        }
    }

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
New Here ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

Thank you for this. I've been struggling with collaborative Google Drive files needing to be relinked every time a file changes hands and although this is a hacky solution to the problem it's the best I've found ... until Adobe finally makes Illustrator links relative 😒

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