Check for open document.

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: Tom, Kukurykus

GuyOxford

Check for open document.

Post by GuyOxford »

I have a JavaScript function that gets the document reference of the current document. However, if there is no document open, it falls over and does not return 'null' as intended. How do I change the following code to check first that there is a document open before trying to execute the rest of the code?

Code: Select allfunction getDocRef() {
     var docRef = activeDocument;
     if (docRef != null) {
        return docRef;
    }
    else {
        return null;
    }
}
Thanks.

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

undavide

Check for open document.

Post by undavide »

You could try this?

Code: Select allfunction getDocRef() {
    if (app.documents.length == 0) return null;
     var docRef = activeDocument;
     if (docRef != null) {
        return docRef;
    }
    else {
        return null;
    }
}

Davide
Paul MR

Check for open document.

Post by Paul MR »

You could try this..
Code: Select allfunction getDocRef() {
if(!documents.length) return null;
return activeDocument;
}
GuyOxford

Check for open document.

Post by GuyOxford »

Brilliant! Thanks so much to both of you.