Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Write your code in a function then you can test for your condition and use return to exit
function main(){
if(someThingIsWrong) return;
}
main();
Copy link to clipboard
Copied
Paul's advice is good. Two other techniques:
1) If you're deep down many levels of function calls, doing a simple return may not work. Use a 'throw' to bail out completely. If you don't want the user to see an error message, do a try/catch and see if the error is one you should ignore, one you should handle, or one you should let the user know about.
2) Hit ESC a bunch of times. Obviously, you can't do that from your script, but the user can do that from their keyboard.
-X
Copy link to clipboard
Copied
Hi X,
I understand how the call stack can effect an if(error) return statement but how do you use a try/catch to bail without the user seeing an error alert?
It seems to me that to stop the script you would have to re-throw an error inside the catch.
Copy link to clipboard
Copied
Let's say that we use a top-level main function for our script:
function main() {
// insert the real script here
};
main();
And way down in your code you're at a point where you need to bail out. Instead of doing a simple 'throw', do this:
Error.runtimeError(9999, "Exit Script");
You can change your main() function to look like this:
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();
If you have intervening try/catch blocks, they will need to check for 'e.number == 9999' and rethrow that exception so that it bubbles back up to main().
I normally advocate against using try/catch as a control structure, but this is one specific case where it can easily be the simplest solution.
But I never do this. I will restructure my code to avoid this because this can be very painful to maintain.
-X
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
This is the idiomatic way to do that.
function main() {
if (app.documents.length == 0) {
alert("Please open a document before running this script.");
return;
}
// insert code here
};
main();
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
I would strongly recommend putting everything you want to run in a function that you can then check for conitions at the beginning of the script. Then you can either run the function or not run it. Also, with every closing "}", make a comment on what it's for. like:
};//end function runProg
This way you can easily keep track of those. You should be able to just dump your entire script into a function after your if statement without breaking anything. 700 lines is not that much for scripting. The main script I use at work has 6,738 lines. It's broken down into functions for various parts so that if something is not met, the script can end.
Copy link to clipboard
Copied
Thanks for the advice. and I'll do that
Just out of curiosity is there a way to end a script with a comand, like 'exit()' ?
I know that exit() is not copatible with Extend Script. it just feels as if there has to be some comand like the esc button.
Copy link to clipboard
Copied
Not that I'm aware of. Only ways I know is to use the method I mentioned, or, again, put your script in a function and use "return" to exit the function and thus end the script with no further lines to excute. Or use wrap you code in a catch/try block and throw an error if a condition is not met.
Copy link to clipboard
Copied
The methods you mentioned are some of the ones I've tried but I couldn't get the
"return" or "wrap code in a catch/try block and throw an error if a condition is not met" to work with the code I've posted.
Copy link to clipboard
Copied
Here's an example for the catch/try block:
try{//all you code in here
var run = true
var errorName = 'stop'
alert('one');
if(!run){throw errorName} //the "!" stands for "not"
//so if your conditions are not met, the if statement throws the error
//and stops the script
alert('two');
}//end try
catch(e){}
Copy link to clipboard
Copied
Thaks for posting this, I'll give it a go.
Copy link to clipboard
Copied
I got the try-catch wrapper with error throw to work (thanks for that).
One thing to remember is that if in your AE prefs (General tab) you have "Enable JavaScript Debugger" checked, any thrown errors will be intercepted by the JS runtime before they get to your catch block, and rerouted to the ExtendScript editor, which is probably not what you want in this case if you just want your script to silently exit. So uncheck it if that's the behavior your want.
Really would be nice if Adobe just supported a simple exit() function or similar that immediately bails on the script but doesn't generate any errors.