I can't rename link file with name containing: "...%ef..." [ExtendScript Indesign]

68 Views Asked by At

I need to automatically rename links according to a specific key. Everything works until the file name contains "...%ef..."

Mac/Indesign replaces "%ef" with a colon - (from api: filePath - The file path (colon delimited on the Mac OS))

doc = app.activeDocument
links_list = doc.links
for(i=0;i<links_list.count();i++){

//[...some code...]
newKeyName[i] = links_list[i].name.replace(partOfOldName, newKey);
//[...some code...]

myFile = new File(links_list[k].filePath)
$.writeln(myFile.exists)//Console say - true. If name contains "...%ef..." - false
$.writeln(myFile.rename(newKeyName[i]))//...
}

I'm trying to use replace

if(myFile.name.indexOf(":")>0){
$.writeln(myFile.name)//Console say - example: aaa%efbbb.png > aaa:bbb.png
myFolder = myFile.path
myName = myFile.name
myName = myName.replace(":","%ef")
myFile = new File(myFolder+"/"+myName);
$.writeln(myFile.exists)//Console say - false.
$.writeln(myFile.name)//Console say - cut file name after "%ef" example: aaa%efbbb.png > bbb.png
}

I'm trying linkResourceURI - same effect or worst.

I want to rename link files containing any characters... on each system (mac/win). I can?

1

There are 1 best solutions below

1
giova95 On

Try creating a File object based on its existing file path and replacing the old name with the new key. In this way you rename files containing special characters that is compatible with both Mac and Windows systems.

Try this:

let doc = app.activeDocument;
let linksList = doc.links;

for (let i = 0; i < linksList.length; i++) {
    let oldFile = new File(linksList[i].filePath);
    if (oldFile.exists) {
        let newFileName = oldFile.name.replace(partOfOldName, newKey);

        let newFilePath = oldFile.parent + "/" + newFileName;

        if (oldFile.rename(newFilePath)) {
            // Update the link's file path in InDesign
            linksList[i].relink(File(newFilePath));
        } else {
            // Handle the case where renaming failed
            alert("Failed to rename the file: " + oldFile.name);
        }
    } else {
        alert("File does not exist: " + linksList[i].filePath);
    }
}