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

[SCRIPT} Update links in placed InDesign files

Enthusiast ,
Nov 20, 2021 Nov 20, 2021

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?

TOPICS
Scripting

Views

706

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

correct answers 1 Correct answer

Community Expert , Nov 21, 2021 Nov 21, 2021

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
...

Votes

Translate

Translate
Community Expert ,
Nov 20, 2021 Nov 20, 2021

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

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 ,
Nov 20, 2021 Nov 20, 2021

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;

 

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 ,
Nov 20, 2021 Nov 20, 2021

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. 

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

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;
//========================================================================================
//========================================================================================

 

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

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;
//========================================================================================
//========================================================================================

 

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 ,
Nov 22, 2021 Nov 22, 2021

Copy link to clipboard

Copied

Wow, Brian, thank you so much for your help!

This is absolutely brilliant. Thanks!!!

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
New Here ,
Dec 04, 2021 Dec 04, 2021

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.

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

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

 

 

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

Copy link to clipboard

Copied

LATEST

If you're after XML scripting the best source is 

https://www.amazon.co.uk/InDesign-CS5-Automation-Using-JavaScript/dp/1460915380/ref=sr_1_1?keywords=...

 

It covers CS5, but as Brian mentioned, early scripting guides still apply.

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