Copy link to clipboard
Copied
Hello, there.
I have an InDesign document with lots of indd files placed in.
Those placed indd files have another indd files placed in.
I need to recursively open all (including the nested ones) indd placed files (links) and update the out of date links. I always get stucked in recursive things...
Any help?
Just realized you have to do the updating AFTER you've done the recursive call. You also shouldn't be opening the root doc since it's already open. How's this?
//========================================================================================
//========================================================================================
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;
var doc = app.activeDocument;
var f = doc.fullName;
doc.linkedPageItemO
...
Copy link to clipboard
Copied
Just decompose the problem of what you need the recursive function to do, then operate on it at the top level. Something like this:
var openDocAndUpdateLinks = function(docFile) {
var d = app.open(docFile);
d.links.everyItem().update();
var allLinks = d.links.everyItem().getElements();
for (var i = 0; i < allLinks.length; i++) {
if (/\.indd$/gi.test(allLinks[i].name)) {
openDocAndUpdateLinks(File(allLinks[i].filePath));
}
}
}
var main = function() {
var f = File.openDialog("Select root file");
openDocAndUpdateLinks(f);
}
Copy link to clipboard
Copied
Thanks for your quick reply, Brian.
Nothing happens when I run it.
I made some changes, since I need to run it in the opened document, and need to save the linked indd files after the links update. So, I tried this code below in the attached file(s).
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;
var f = File.openDialog("Select root file");
openDocAndUpdateLinks(f);
function openDocAndUpdateLinks(docFile) {
var d = app.open(docFile);
d.links.everyItem().update();
var allLinks = d.links.everyItem().getElements();
for (var i = 0; i < allLinks.length; i++) {
if (/\.indd$/gi.test(allLinks[i].name)) {
openDocAndUpdateLinks(File(allLinks[i].filePath));
}
}
d.close(SaveOptions.YES);
}
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;
Copy link to clipboard
Copied
Yeah, guess I left out the call to main(), and figured you might need to do other operations like saving after updating. Was just trying to demonstrate what the recursive call looks like. If you're running on an open doc, then your main loop would look a bit different, but you could still pass link.filePath to the recursive call.
Copy link to clipboard
Copied
Well, thanks.
I can not make it work. The final result is never a document with all links updated.
My last attempt:
//========================================================================================
//========================================================================================
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;
var doc = app.activeDocument;
var f = doc.fullName;
doc.linkedPageItemOptions.updateLinkWhileSaving = true;
var nm = File.decode(doc.name);
openDocAndUpdateLinks(f , nm);
doc.links.everyItem().update();
alert("Done." , "Script by LFCorullón");
//========================================================================================
//========================================================================================
function openDocAndUpdateLinks(docFile , nm) {
var d = app.open(docFile);
d.linkedPageItemOptions.updateLinkWhileSaving = true;
d.links.everyItem().update();
var allLinks = d.links.everyItem().getElements();
for (var i=allLinks.length-1; i>=0; i--) {
if (/\.indd$/gi.test(allLinks[i].name)) {
openDocAndUpdateLinks(File(allLinks[i].filePath));
}
}
d.save();
if (File.decode(d.name) != nm) d.close(SaveOptions.YES);
}
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;
//========================================================================================
//========================================================================================
Copy link to clipboard
Copied
Just realized you have to do the updating AFTER you've done the recursive call. You also shouldn't be opening the root doc since it's already open. How's this?
//========================================================================================
//========================================================================================
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;
var doc = app.activeDocument;
var f = doc.fullName;
doc.linkedPageItemOptions.updateLinkWhileSaving = true;
var nm = File.decode(doc.name);
openDocAndUpdateLinks(f , nm);
doc.links.everyItem().update();
alert("Done." , "Script by LFCorullón");
//========================================================================================
//========================================================================================
function openDocAndUpdateLinks(docFile , nm) {
if (File.decode(docFile.name) == nm) {
var d = app.activeDocument;
} else {
var d = app.open(docFile);
}
d.linkedPageItemOptions.updateLinkWhileSaving = true;
var allLinks = d.links.everyItem().getElements();
for (var i=allLinks.length-1; i>=0; i--) {
if (/\.indd$/gi.test(allLinks[i].name)) {
openDocAndUpdateLinks(File(allLinks[i].filePath));
}
if (allLinks[i].status == LinkStatus.LINK_OUT_OF_DATE) {
allLinks[i].update();
}
}
d.save();
if (File.decode(d.name) != nm) d.close(SaveOptions.YES);
}
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;
//========================================================================================
//========================================================================================
Copy link to clipboard
Copied
Wow, Brian, thank you so much for your help!
This is absolutely brilliant. Thanks!!!
Copy link to clipboard
Copied
Brian, I am new to InDesign scripts and using Creative Cloud version. Any scripting resources you recommend for doing batch updates to linked XML data in multiple InDesign files? Not sure if the earlier Javascript toolkit scripting guides work with InDesign CC. Any pointers appreciated.
Copy link to clipboard
Copied
Yes. The early scripting guides still apply. A nice searchable DOM is available here: https://www.indesignjs.de/extendscriptAPI/indesign-latest/#about.html
Copy link to clipboard
Copied
If you're after XML scripting the best source is
It covers CS5, but as Brian mentioned, early scripting guides still apply.