Skip to main content
Inspiring
January 11, 2023
Question

activeDocument array?

  • January 11, 2023
  • 1 reply
  • 1546 views

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 has been closed for replies.

1 reply

Legend
January 11, 2023

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.

 

Inspiring
January 12, 2023

 

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.

Legend
January 14, 2023

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 MarshI noticed your message when I sketched the code.