Copy link to clipboard
Copied
I need to run a couple menuActions on a batch of documents and I'm not sure if what I've done is correct or if maybe there's a more common way to do it. Here's my script:
for (var i = 0; app.documents.length > i; i++) {
sortByName();
app.activeDocument = app.documents[1]; //go to the next document
}
function sortByName() {
app.menuActions.itemByID(8505).invoke(); //sort p-styles
app.menuActions.itemByID(8511).invoke(); //sort c-styles
}
This will sort all the paragraph and character styles alphabetically in all open documents. It seems to work just fine, but I'm not sure about that 3rd line.
I'm familiar with processing documents by their index, like app.documents[i].blahblah, but in this case, that app.menuActions would just run those menuActions over and over on the activeDocument. I need to make each document active one-by-one to run the app.menuActions, but once I've changed the activeDocument, obviously the index numbers change and my "i" value is wrong.
My solution was to just run the function, then make the next document the activeDocument, over and over as many times as there are open documents.
Is that a reasonable solution? Or is there some better/more common way to do this?
Thanks!
Push names of open documents into an array - then loop through this array and use string from the element to "activate" document.
Copy link to clipboard
Copied
Names could be risky if two docs with the same name in two different locations were opened.
Copy link to clipboard
Copied
Names could be risky if two docs with the same name in two different locations were opened.
By @brian_p_dts
Only if you store just the Name - not the FullName.
Copy link to clipboard
Copied
But then getByName doesn't work
Copy link to clipboard
Copied
But then getByName doesn't work
By @brian_p_dts
How about just app.documents.item()?
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Documents.html#d1e55803__d1e55957
Copy link to clipboard
Copied
But then getByName doesn't work
By @brian_p_dts
And there is no getByName?
Your search returned no results for GetByName
I think you meant itemByName?
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Documents.html#d1e55803__d1e56023
Copy link to clipboard
Copied
Oops. Been doing more Illustrator scripting lately (which uses getByName, stupidly)
Copy link to clipboard
Copied
Also, I'm 99.99% sure, I think I've tried this before, that if you try to open document that is already open - you won't get an error but reference to already open instance.