Skip to main content
Royi A
Inspiring
April 13, 2009
Question

How To Exit A Script?

  • April 13, 2009
  • 1 reply
  • 41915 views

Hello.

I'm trying to write a script (My First) for Photoshop.

I want to check a condition and if it's true to exit the script.

The problem I can't find any place writing what is the "Exit" command in a script.

Thanks.

This topic has been closed for replies.

1 reply

Paul Riggott
Inspiring
April 13, 2009

Write your code in a function then you can test for your condition and use return to exit

function main(){

if(someThingIsWrong) return;

}

main();

Royi A
Royi AAuthor
Inspiring
April 17, 2009

I wonder if there's an option that if there's no Document, it will just stop running the script.

Something like:

if (!documents.length) {

     alerts ("There are no Documents");

     Exit (); // A function that stops the script

}

Has anyone succeeded doing it?

How can I write such "Exit" function?

Thanks.

Chuck Uebele
Community Expert
Community Expert
April 16, 2014

I'm going nuts here.  Granted I'm not a programmer but I think I'm willing to learn and give things a try and I think I've tried and tried and tried.

I'm working on Photoshop CS5 on a mac

This is what I want to do:

1. check all layers or layer groups for names "K" and "Color"

2. if non of the layers or layer groups names are "K" or "Color" tell the user to set the correct name

3. terminate the script and don't go to any other lines of code

This is the code I've got

var TotLayers = app.activeDocument.layers.length;

var layer = app.activeDocument.layers;

// Makes sure that the K and Color layer are there

function LayerCheck(){

    // Check for the presence of the K layer containing black

    for (i=0; i<TotLayers;i++){

        if(layer.name == "K" ){

            break;

        }else if ( i==TotLayers-1 && layer.name != "K"){

            // here alert user

            alert ("Make sure your all your blacks are in a layer or layer folder labeled 'K'");

            // here exit the script (don't process any more code)

              /**

              //I've tried the following options at this location in the code

              exit();

              return;

              throw "My message here";

              throw new Error("My message here");

              */

        }

     }

    // Check for the presence of the Color layer containing the underlying CMY colors of the illustration

    for (i=0; i<TotLayers;i++){

        if(layer.name == "Color" ){

            break;

        }else if ( i==TotLayers-1 && layer.name != "Color"){

            // here alert

            alert ("Make sure your all your color work is in a layer or layer folder labeled 'Color'")

            // here exit the script (don't process any more code)

    }

}

};

LayerCheck();

I've also tried

function main() {

       try {

       // insert the real script here

       } catch (e) {

          if (e.number == 9999) {

             return ;        // just return; this is our 'exit' exception

          }

          // handle the exception, alert the user, rethrow the error....

      }

};


main();

and

Error.runtimeError(9999, "Exit Script");

but I had no luck.

If anyone can help I'd appreciate it. Thanks


What I do is set up a variable at the very start to shut down things and then have that variable checked to either run the script to stop.  so something like this:

var exitProg = false;

if(some condition met){runProg}

else{exitProg = true}

function runProg{

Your code here...

if(another condition checked){

secondFunction()

More code}

else{

exitProg = true;

return}

}

function secondFunction(){

More code here.....

}

By setting the exitProg= true, you can then write your program to skip over everything else and just finish without running any more code.