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

Abort Script If No Document Open

Participant ,
Nov 05, 2018 Nov 05, 2018

Copy link to clipboard

Copied

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

Views

762

Translate

Translate

Report

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

}

Votes

Translate

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

Copy link to clipboard

Copied

try{

var docRef = app.activeDocument;

}

catch(e){

    var noDocs = false;

    }

if (noDocs== false){

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

}

else {

// run script

}

Votes

Translate

Translate

Report

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
Participant ,
Nov 05, 2018 Nov 05, 2018

Copy link to clipboard

Copied

Thank you, that has help me

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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