Skip to main content
Inspiring
Jan 11, 2023 7:41 PM UTC
Question

activeDocument array?

Does Photoshop keep an array of it's activeDocuments? Not a list of the documents history, which is held in  documents. But Photoshop files that are currently open. So app.activeDocument[1] would be the document you used last and app.activeDocument[0] is the current one.

 

for (var i = 0; i < activeDocuments.length; i++)
{
 // Something
}

Obviously the above doesn't work. - But you get the idea.

 

Hopefully.

 

This topic is closed to new replies. Start a new post to keep the conversation going.

7 replies

Legend
Jan 11, 2023 8:12 PM UTC

Only one document can be active at one time. So, no, that is not stored in an array, just an application property.

You might be looking for app.documents instead, which has nothing to do with history.

 

Translate
Inspiring
Jan 12, 2023 4:44 PM UTC

 

var theDocs = app.documents;

for (var i = 0; i < theDocs.length; i++)
{
  msg += theDocs[i].name + "\n";
}
alert(msg)

That gives the cocuments that wer opened in order, which not quite what I was after. I'm sure I'll work something out.

Translate
Legend
Jan 14, 2023 9:13 AM UTC

That's a clever script. Possibly overkill for my needs - My script is just grabbing or putting guides from one document to another - for a one shot/ one button script it just needs to work out is there a document with the same dimensions already open? And is it missing guides? 


Copies guides from the active document to all open documents of the same size:

var startRulerUnits = preferences.rulerUnits,
  startTypeUnits = preferences.typeUnits,
  doc = activeDocument,
  activeDocumentW = doc.width.value,
  activeDocumentH = doc.height.value;
app.preferences.rulerUnits = Units.PIXELS
app.preferences.typeUnits = TypeUnits.PIXELS
if (doc.guides.length) {
  var guides = [];
  for (var i = 0; i < doc.guides.length; i++) {
    guides.push({ direction: doc.guides[i].direction, coordinate: doc.guides[i].coordinate })
  }
  var len = documents.length;
  for (var i = 0; i < len; i++) {
    var cur = documents[i];
    if (cur == doc) continue;
    activeDocument = cur;
    if (cur.width == activeDocumentW && cur.height == activeDocumentH && !cur.guides.length) {
      for (var x = 0; x < guides.length; x++) {
        cur.guides.add(guides[x].direction, guides[x].coordinate)
      }
    }
  }
}
activeDocument = doc;
app.preferences.rulerUnits = startRulerUnits
app.preferences.typeUnits = startTypeUnits

@Stephen Marsh, I noticed your message when I sketched the code.

 

Translate