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.

Known Participant
April 16, 2014

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.


Thanks csuebele,

I have over 700 line of code and I would really prefer to run the check before start running all of the rest of the code.  I would hate to mess-up the code by adding or deleting a forgotten '}' somewhere.  If I could just kill the script at the begining after a warning that would be faster to implement, and I think easier to read for my taste.  I'm still learning coding, so I'm not too familiar with best practices. But, I think there has to be a way to kill the script if it doesn't meet a condition.