Skip to main content
Participating Frequently
September 19, 2024
Answered

Script to run app.menuActions on all open documents

  • September 19, 2024
  • 2 replies
  • 3242 views

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!

This topic has been closed for replies.
Correct answer Robert at ID-Tasker

@Donald32516386092f 

 

Push names of open documents into an array - then loop through this array and use string from the element to "activate" document.

 

2 replies

Charu Rajput
Community Expert
Community Expert
September 19, 2024

Hi @Donald32516386092f ,

You need to change the constant 1 to i, so that it can switch to the next document one by one and then call the sortNames function. Try the following way if that helps.

 

for (var i = 0; app.documents.length > i; i++) {
    app.activeDocument = app.documents[i]; //go to the next document
    sortByName();
}

function sortByName() {
    app.menuActions.itemByID(8505).invoke(); //sort p-styles
    app.menuActions.itemByID(8511).invoke(); //sort c-styles
}

 

Best regards
Participating Frequently
September 20, 2024

But that doesn't work. As soon as the script changes the activeDocument, the index numbers of the collection of documents change based on the new activeDocument. 

 

Consider 3 documents named Doc1, Doc2, Doc3. Doc1 is the activeDocument, index 0. Doc2 is index 1. Doc3 is index 2. The script is going to run on index 0, then 1, then 2. 

 

  1. In the first interation, Doc1 in is the active document, or app.documents[0], and the function runs on it.
  2. In the second iteration, Doc2, or app.documents[1], becomes the active document and the function runs on it.
  3. In the third iteration, app.documents[2] becomes the active document, but that index number is based on the previous iteration where Doc2 had become app.documents[0]. Once Doc2 became the active document, app.documents[2] was actually Doc 1 again.

 

That make sense?

 

Think of it this way: if you had whole bunch of documents open, it would run them like this:

 

Doc1 (The next doc is +1)

Doc2 (The next doc is +2)

Doc4 (The next doc is +3)

Doc 7 (The next doc is +4)

Doc 11 ....

 

By solution was just to process the activeDocument+1 every time. 

 

Robert at ID-Tasker
Robert at ID-TaskerCorrect answer
Legend
September 20, 2024

@Donald32516386092f 

 

Push names of open documents into an array - then loop through this array and use string from the element to "activate" document.

 

brian_p_dts
Community Expert
Community Expert
September 19, 2024

I think you can set the active doc to the current doc in the iteration:

app.activeDocument = app.documents[i];

 

I don't know why you are setting it to 1. 

Run your sort func after setting the active doc to i

Participating Frequently
September 20, 2024

That doesn't work. See my reply to Charu Rajput.