Photoshop scripting: app.activeDocument is undefin

2019-08-05 21:52发布

问题:

I'm trying to access current opened document in script, but it's undefined. But i have opened document in Photoshop. Should i initialize it somehow? Here is my code

function ProcessDocumentWithoutXML()
{  
g_rootDoc      = app.activeDocument;
g_progBar      = new ProgressBar();

if (app.activeDocument != null)
{
    ProcessLayersWithoutXML(g_rootDoc);
    alert("Done!");
} else {
    alert("Missing active document");
}
}

ProcessDocumentWithoutXML();

回答1:

In order for it to work

g_rootDoc      = app.activeDocument;

needs to be outside the function (unless you pass in the source document to that function).

Amended code:

if (documents.length != 0)
{
   g_rootDoc = app.activeDocument;
   // g_progBar = new ProgressBar();  // no worky in cs2
   ProcessLayersWithoutXML(g_rootDoc);
   alert("Done!");
}
else
{
    alert("Missing active document");
}


function ProcessDocumentWithoutXML()
{  

}

ProcessDocumentWithoutXML();

function ProcessLayersWithoutXML()
{
}


回答2:

If you are running photoshop in one window and running your code in ExtendedScript in other window you need to add the first line

"#target photoshop"

(without double marks) on your js script.