Skip to main content
Inspiring
August 8, 2022
Answered

Can you loop through a set number of open documents without closing them?

  • August 8, 2022
  • 1 reply
  • 415 views

Hi all,

Is that possible?

If I always have 8 documents open, I want to run the script on all 8, but without having to save and close each one - like a WHILE loop would do.

I just can't get this to work on more than one open file...

 

for (var i = 0; i < app.documents.length; i++) {
    var doc = app.activeDocument;
    var ABs = doc.artboards;
    ABs.setActiveArtboardIndex(176); //put KM variable in here
    doc.selectObjectsOnActiveArtboard();
    }

 Can anyone help at all? Many thanks, J

This topic has been closed for replies.
Correct answer femkeblanco

Add

doc.activate();

after var doc = app.documents[i];

1 reply

m1b
Community Expert
August 8, 2022

Hi @JustyR, change to

var doc = app.documents[i];
JustyRAuthor
Inspiring
August 8, 2022

Hmmm, I thought that would do it, but it still only works on the one open doc.

for (var i = 0; i < app.documents.length; i++) {
    var doc = app.documents[i];
    var ABs = doc.artboards;
    ABs.setActiveArtboardIndex(176); //put KM variable in here
    doc.selectObjectsOnActiveArtboard();
    }
JustyRAuthor
Inspiring
August 8, 2022

Add

doc.activate();

after var doc = app.documents[i];


That did the trick. Many thanks to you both.