Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Abort Script If No Document Open

Contributor ,
Nov 05, 2018 Nov 05, 2018

I am trying to stop the script from running if No Documents are open.

I have a variable at the beginning of the script which reads...  var docRef = app.activeDocument but it's throwing an error in ESTK if no document is open so I am trying to figure out a way to prevent the script from running if no document is open when the script is started.

I tried the following thinking that if the document length was greater then 0 then it would set the variable otherwise it would exit but this is wrong somewhere.

if(app.documents.length > 0){

var docRef = app.activeDocument

}else{

  return;

  }

TOPICS
Actions and scripting
952
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Contributor , Nov 05, 2018 Nov 05, 2018

try{

var docRef = app.activeDocument;

}

catch(e){

    var noDocs = false;

    }

if (noDocs== false){

    alert ("You do not have a document open.")

}

else {

// run script

}

Translate
Adobe
Contributor ,
Nov 05, 2018 Nov 05, 2018

try{

var docRef = app.activeDocument;

}

catch(e){

    var noDocs = false;

    }

if (noDocs== false){

    alert ("You do not have a document open.")

}

else {

// run script

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Nov 05, 2018 Nov 05, 2018

Thank you, that has help me

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 05, 2018 Nov 05, 2018
LATEST

Hi ian-barber​,

'return' only works in a function.

Your own code should be:

areThereDocuments ();

function areThereDocuments () {

if (app.documents.length > 0) {

    var docRef = app.activeDocument;

    // your code

    // alert (docRef.name);

    } else {

    alert ("no open document");

    return;

    }

}

Have fun

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines