我无法重命名名称包含“...%ef...”[ExtendScript Indesign]

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

提问人:Tomasz Galubiński 提问时间:9/28/2023 更新时间:10/6/2023 访问量:60

问:

我需要根据特定键自动重命名链接。在文件名包含“...%ef...”之前,一切正常

Mac/Indesign 将“%ef”替换为冒号 - (来自 api:filePath - 文件路径(在 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]))//...
}

我正在尝试使用 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
}

我正在尝试 linkResourceURI - 相同的效果或最坏的效果。

我想重命名包含任何字符的链接文件...在每个系统上 (Mac/Win)。我可以?

macOS 文件重命名 Adobe-InDesign 扩展脚本

评论

0赞 Yuri Khristich 9/28/2023
我能够重现这个问题,但是,唉,我没有解决方案。会很高兴知道答案。

答:

0赞 giova95 10/6/2023 #1

尝试根据其现有文件路径创建 File 对象,并将旧名称替换为新项。 通过这种方式,您可以重命名包含与 Mac 和 Windows 系统兼容的特殊字符的文件。

试试这个:

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

评论

0赞 Yuri Khristich 10/6/2023
唉,如果它的名称包含“%ef”(在 macOS 上),它会提醒我关于每个链接文件的“文件不存在”。