Customized script to open/edit linked files is not working as expected
Dear community
I am at a loss here and can't find a solution. This is part of a script which opens linked InDesign files, changes the layer visibility on the documents placed in the opened links.
If I put an alert() somewhere between opening and saving the document, everything works fine. If I remove the alert() I get an error, telling me that the file is already open. The error points to the last command in the changeLayerVisibility() Function: layer.currentVisibility = checklist[ft][k].checkedState;
I'm thinking it has something to do with async or timeouts. The alert slowing the process, allowing the loop to finish. But I can't understand why it would ever try to open the same file again...
Here's the relevant part of the script. I hope someone sees where I'm going wrong. If not I can try to create a reduced version of the thing.
function changeNestedLayers(linksArr) {
var thisLink,
thisFile,
thisFileName,
thisDoc;
for (var l = 0; l < linksArr.length; l++) {
thisLink = linksArr[l];
thisFile = File(thisLink.filePath);
thisFileName = thisFile.fullName;
thisDoc = app.open(thisFile, true);
var currentLink,
currentLinkname,
currentLayer,
currentChecklistItem;
for(var s = 0; s < thisDoc.links.length; s++) {
currentLink = thisDoc.links[s];
currentLinkname = currentLink.parent.itemLink.name;
if (currentLink.parent.constructor.name == "ImportedPage" && currentLink.parent.itemLink != null && currentLink.parent.itemLink.linkType == "InDesign Format Name") {
if(hasSubstring(currentLinkname, "FiletypeA")) {
changeLayerVisibility(currentLink, "FiletypeA");
count++;
}
else {
changeLayerVisibility(currentLink, "FiletypeB");
count++;
}
}
}
thisDoc.save(new File(thisFileName));
thisDoc.close(SaveOptions.NO);
}
}
function changeLayerVisibility(thisLink, filetype) {
var ft = filetype.toLowerCase();
var layer;
for (var k = 0; k < layertypes[ft].length; k++) {
layer = thisLink.parent.graphicLayerOptions.graphicLayers.item(layertypes[ft][k].name);
if (layer.isValid) {
layer.currentVisibility = checklist[ft][k].checkedState;
}
}
}
Thanks!
