Why is manual link folder replacement faster than script in InDesign?
Hello everyone,
I have a question to ask. When I use this script to change the target folder of links, if there are many linked files, the entire script takes a long time to complete. However, if I rename the source directory first and then use InDesign's Links panel to replace the target folder for finding missing files, this method is much faster, only a fraction of the time it takes for the script to run.
What could be the reason for this significant difference in speed?
Is there any way to optimize this script?
Thank you all.
var originalFolderPath = "F:/source";
var targetFolderPath = "e:/target";
var doc = app.activeDocument;
var links = doc.links;
var updatedLinksCount = 0;
for (var i = 0; i < links.length; i++) {
var link = links[i];
var originalLinkPath = link.filePath;
if (originalLinkPath.indexOf(originalFolderPath) === 0 || originalLinkPath.indexOf(originalFolderPath.replace(/\//g, "\\")) === 0) {
var originalFileName = originalLinkPath.substring(originalLinkPath.lastIndexOf('/') + 1);
if (originalFileName == originalLinkPath) {
originalFileName = originalLinkPath.substring(originalLinkPath.lastIndexOf('\\') + 1);
}
var targetFilePath = targetFolderPath + "/" + originalFileName;
//$.writeln("Target File Path: " + targetFilePath);
var targetFile = new File(targetFilePath);
if (targetFile.exists) {
link.relink(targetFile);
updatedLinksCount++;
}
}
}
if (updatedLinksCount > 0) {
alert("Changed: " + updatedLinksCount + " links to files with the same names in the target folder!");
} else {
alert("No links to update found!");
}


