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

Renaming links for already-placed image files

New Here ,
Dec 14, 2021 Dec 14, 2021

Copy link to clipboard

Copied

Hi, 

 

We're hoping to rename a large number of image files that have already been placed in an InDesign document, as we're making some changes to a structured, chapter-by-chapter naming system we have for images. Is there a way to rename and relink these files at the same time from within InDesign, without having to relink the files afterwards by hand? 

 

There is a method described helpfully in this externally-posted guide, but it's unfortunately become outdated and unusable, as it was posted in 2007. 

 

Thanks for your help. For reference, we use Mac for our OS. 

TOPICS
Feature request , How to , Scripting

Views

710

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 14, 2021 Dec 14, 2021

Copy link to clipboard

Copied

Hi @dncolburn, this is how I do it. I wrote a script that just does a find/replace within the links file paths, so the idea is that the new changed file path lines up with the files you want it to. Let me know if it doesn't make sense.

- Mark

/*
    Relinker
    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.
*/

var replaceThis = "old-part/of/link/path", // use regex here if necessary, eg. /\d+/
    withThis = "new-part/of link path";


function main() {
    var _links = app.activeDocument.links;

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

        // comment this out if you want to relink all links
        if (_links[i].status !== LinkStatus.LINK_MISSING) continue;

        // new file by replacing text within file path
        var newLinkedFile = File(_links[i].filePath.replace(replaceThis, withThis));

        // if file exists at the new file path, relink
        if (newLinkedFile.exists)
            _links[i].relink(newLinkedFile);
    }

}

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

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 15, 2021 Dec 15, 2021

Copy link to clipboard

Copied

A bit more info: since you are using Mac, the file paths use colons between folders, eg. "MacintoshHD:Users:mark:Desktop:myLinkedFile.pdf". You can see this in the Links panel. You can get a copy of it by selecting a link in the Links panel, going to the fly-out panel menu and choosing "Copy Info > Platform Style Path".

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 14, 2021 Dec 14, 2021

Copy link to clipboard

Copied

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
Enthusiast ,
Dec 21, 2021 Dec 21, 2021

Copy link to clipboard

Copied

I have a few ways to do this, but at this time maybe not as automated as you would like. I have an idea for a new script that does rename/relink from a CSV spreadsheet of "old name" and "new name" columns, but it's still on the drawing board. Anyone interested in such a thing, chime in. It's what users ask for that rises to the top of the to-do list.

 

For now, have a look at these scripts and see if either is any help:

https://www.marspremedia.com/software/indesign/links-rename-selected

https://www.marspremedia.com/software/indesign/links-grep-rename

 

William Campbell

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

Copy link to clipboard

Copied

That would be very useful for renaming styles in multiple documents. 

 

There is something similar for Adobe FrameMaker by Silicon Prairie Software (actually, a series of plugins). It creates a document with a table with all styles or all used styles; you then fill in the new names in the table. 

David Creamer: Community Expert (ACI and ACE 1995-2023)

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
Enthusiast ,
Dec 21, 2021 Dec 21, 2021

Copy link to clipboard

Copied

Interesting. If I understand correctly, such a script is not difficult. Is the goal to simply rename the existing styles, nothing more? If style name equals "this," change its name to "that"? A list of "this" and "that" being rows of CSV.

 

William Campbell

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

Copy link to clipboard

Copied

Hi @willcampbell7 ,

 

I been trying to do the same thing. I been able to save out to an extrenal .txt file the names of the linked images. I have also been able to read back a .csv file with the new names. But I get this error when it tries to rename the file.

 

Error.PNG

 

Here is my code:

 

// Open the CSV file
var file = File.openDialog("Open CSV File", "*.csv");
if (!file) {
exit();
}
file.open("r");

// Read the CSV file into an array
var csvArray = [];
while (!file.eof) {
csvArray.push(file.readln().split(","));
}
file.close();

// Get all linked images in the document
var myLinks = app.activeDocument.links;

// Loop through each row in the CSV file
for (var i = 0; i < csvArray.length; i++) {
// Get the current name and new name from the CSV file
var currentName = csvArray[i][0];
var newName = csvArray[i][1];

// Loop through each linked image in the document
for (var j = 0; j < myLinks.length; j++) {
var myLink = myLinks[j];
// If the current name matches the linked image name, rename it
if (myLink.name == currentName) {
myLink.name = newName;
myLink.relink(myLink.filePath, true);
break;
}
}
}

 

And yes, I am using your Test files!

 

Thanks in advance.

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

Copy link to clipboard

Copied

Hi @Deprive, can you try this modification? Sorry that I haven't tested because I don't have the test files. - Mark

// Open the CSV file
var file = File.openDialog("Open CSV File", "*.csv");
if (!file) {
    exit();
}
file.open("r");
// Read the CSV file into an array
var csvArray = [];
while (!file.eof) {
    csvArray.push(file.readln().split(","));
}
file.close();
// Get all linked images in the document
var myLinks = app.activeDocument.links;
// Loop through each row in the CSV file
for (var i = 0; i < csvArray.length; i++) {
    // Get the current name and new name from the CSV file
    var currentName = csvArray[i][0];
    var newName = csvArray[i][1];

    // Loop through each linked image in the document
    for (var j = 0; j < myLinks.length; j++) {
        var myLink = myLinks[j];
        // If the current name matches the linked image name, rename it
        if (myLink.name == currentName) {
            // make a new File object
            var newLinkedFile = File(myLink.filePath.replace(myLink.name, newName));
            // if file exists at the new file path, relink
            if (newLinkedFile.exists)
                myLink.relink(newLinkedFile);
        }
    }
}

 

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
Enthusiast ,
Feb 07, 2023 Feb 07, 2023

Copy link to clipboard

Copied

LATEST

That's not how it works. Changing the property "name" of the document link object does not rename the file on disk. You need to rename the file itself, then relink to the new name. See the script code for "Links Rename Selected" on my website: https://www.marspremedia.com/software/indesign/links-rename-selected

The code is open source and right there on the webpage. This script isn't exactly what you're doing, but the code gives a clue how it works.

Here's a simple bit of code that might help make it clearer:

// Get a link. For example, first link in document.
var link = app.activeDocument.links[0];
// Make a file object of the file on disk.
var file = new File(link.filePath);
// Rename the file.
// Use the current file extension.
var nameNew = "The new file name" + String(file.name.match(/\.[^\.]+$/));
file.rename(nameNew);
// Make a file object of the renamed file.
var fileNew = new File(file.path + "/" + nameNew);
// Relink to the new file and update.
link.relink(fileNew);
link.update();

 

William Campbell

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