0

I have a problem that you may be able to clarify for me.
I'm updating an old script with a menu made in ScriptUI who has a button (this is just an example, the final script will have more buttons).
I was advised to put the menu inside a function, using "return" to stop the script when certain conditions were not met (apparently "exit" doesn't work in photoshop).
When the button is pressed it should activate a control function (CheckFunction) that stops everything in case of a problem (I chose to put the control in a function to avoid unnecessary repetitions as there will be a few buttons with the same control).
Not knowing if an external function can be called from inside a function (I just started doing simple scripts, I don't know much), I nested the functions.
The problem now is that the button calls multiple consecutive functions (I don't think it's avoidable), and using return on the first one (the control one), it stops the control function, but not the rest of the functions that are called generating various errors.
Is it possible to stop the other functions or do I have to write the script in some other way?
I give an example of how the menu is done for clarity.

    function Menu() {
        if (documents.length == 0) {
            alert ("Open a file first");
            return;
            }

// here goes the dialog window with the button I will call button1

        button1.onClick = function() {
            CheckFunction();
            Function2();
            Function3();
            }

        function CheckFunction() {
            if (condition is not met) {
                alert ("Condition not met");
                return;
                }
            }

// here goes the other functions

        DialogWindow.show ();
        }
    Menu();

Once used return in CheckFunction() does not stop Function2 and Function3 which are called creating errors.
If I need to rearrange the whole script I will gladly do it since scripting is not my strong suite.
Do you have any solutions/advice for me?

IDJSGUS
  • 97
  • 7
  • 2
    There's no way of stopping a script. You need to set a couple of variables to false and switch them to true once those conditions are met (ie a document is open and available). if they are ALL true then the image can then get processed. – Ghoul Fool Jun 22 '22 at 09:27
  • So basically adding a variable = false at the beginning of the script, adding a condition after the alert and adding a condition at the beginning of Function2 and Function3? – IDJSGUS Jun 24 '22 at 10:36
  • I don't know what Function 2 & 3 do but yeah, only allow the code to do the final processing if everything is in order. – Ghoul Fool Jun 24 '22 at 13:35

1 Answers1

2

You may have figured it out by now, but I will answer for anyone else who may come across this.

When you return, it simply stops/exits current function, not the entire script. So for the script to stop, you need to return from the main script function, not from a nested function (in your case CheckFunction()), or even the button click function.

You may need to adjust your logic a bit here. Basically, your Function2 and Function3 should only run if CheckFunction passes. That means, that the only purpose of clicking the button is to check something. If check OK, you can run Function2 and Function3. If check not OK, stop script.

For your code, I would suggesting returning something from CheckFunction, then returning something again from your button click and using the button click function return value to check if further functions should run.

Something like this:

...

var continue = true; //control variable

button1.onClick = function() {
    var check = CheckFunction();
    
    if (!check) { //if false
        continue = false;
    }
}

 if (continue) {
     
     // here goes the other functions
     Function2();
     Function3();
 }   



//check function
function CheckFunction() {
    if (condition is not met) {
        alert ("Condition not met");
        return false;
    }
}

...