How to quit a script

How to quit a script

4
NewbieNewbie
4

    Nov 24, 2007#1

    What is the correct way to end a script? Many times I am in the middle of an infinite While loop or something and when some condition occurs, I just want to have the script stop. I have tried "quit()" and that stops the script but returns an error on that line.

    What is the 'officially suggested' way to quit?

    Also it seems that sometimes a break() will not work or give you a script error. In what constructs can you use the break() command?

    Dan

    262
    MasterMaster
    262

      Nov 24, 2007#2

      You have no emergency break as such in scripts in UE. You have to design your script in such a way that you can abort when you want.

      The most simple way is to put your main logic in a main function like this:

      Code: Select all

      main(); /* run main */
      
      function main() {
         var myLocalVariable = UltraEdit.getString("Enter a letter",1);
      
         if (myLocalVariable=="A") {
            return; /* exit script, or rather exit function */
         }
      
         UltraEdit.activeDocument.write(myLocalVariable);
      }
      Another approach is to use a design based on try/catch/throw - example:

      Code: Select all

      try {
         myFuncA();
         myFuncB();
      }
      catch (exception) {
         if (exception=="myEmergencyExit")
            UltraEdit.activeDocument.write("Emergency break");
         else
            UltraEdit.activeDocument.write("Other error");
      }
      
      function myFuncA() {
         UltraEdit.activeDocument.write("A1"+"\r\n");
         throw("myEmergencyExit"); /* EXIT SCRIPT NOW */
         UltraEdit.activeDocument.write("A2"+"\r\n");
      }
      
      function myFuncB() {
         UltraEdit.activeDocument.write("B"+"\r\n");
      }
      There is no such thing as break() in JavaScript unless you yourself have written such a function.

      But there is a break command (note: no parentheses following break). break can be used to break all kinds of loops.

      Some examples:

      Code: Select all

      var i=0;
      while (true) {
         UltraEdit.activeDocument.write(String(i));
         if(++i==2)
            break;
      }
      
      loopLabel1:
      for(var j in UltraEdit.document) {
         if(UltraEdit.document[j].path.substr(0,4)=="Edit")
            continue loopLabel1; /* go to next iteration of loop */
            
         var pathParts = UltraEdit.document[j].path.split("\\");
      
         loopLabel2:
         for(var i in pathParts) {
            if(pathParts[i]=="scripts")
               break loopLabel1; /* break both inner loop as well as outer loop
         }
      }
      For more on break, loops, try/catch/throw etc. see the links in the sticky for JavaScript references and tutorials.

      4
      NewbieNewbie
      4

        Nov 24, 2007#3

        Thanks, Jorrasdk. That helps.

        Dan

        2
        NewbieNewbie
        2

          Oct 08, 2010#4

          Hello all, newbie here with a dumb question...

          Anyone know if it's possible to change where the "break" popup window opens when a script runs (it stays visible if you've used a messageBox() in a script).

          The window reads: "Press Cancel to abort the current operation" & always opens in the same upper left corner & is always on top, covering the beginning of an open file. You can move it out of the way manually, but that gets old if you run the script often & repeatedly.

          This thread implies that the popup wasn't always a feature & while it's a handy, it'd be nice to have some control over it.

          Nit picking question I know, but thought I'd ask anyway just in case there's an obvious solution I'm overlooking.

          I'm running UE v16.20.0.1011

          Thanks :)

          6,687586
          Grand MasterGrand Master
          6,687586

            Oct 10, 2010#5

            The dialog to cancel current script execution is built-in since UE v13.00 - the first version of UE supporting scripts - and there is nothing to control the dialog, whether dialog position nor dialog visibility (unfortunately).

            Update on 2015-09-08: There is the check box setting Show cancel dialog at Advanced - Configuration - Scripting available since UE v21.30 and UES v14.40 which is by default checked to control visibility of this small dialog on execution of a script. I recommend having enabled this setting on developing a script in case of running by mistake into an endless loop to be able to break script execution.
            Best regards from an UC/UE/UES for Windows user from Austria

            2
            NewbieNewbie
            2

              Oct 11, 2010#6

              Thank you for your reply. The work-around I ended up using is literally a work around. I included code in the script to check for/insert 10 blank lines at the beginning of the file so the "start" of the file is below the pop-up window.

              7
              NewbieNewbie
              7

                Sep 08, 2015#7

                Hi-- it's been years since the last post. Is there still no way to control that dialogue box? (I'd like to remove it.)

                6,687586
                Grand MasterGrand Master
                6,687586

                  Sep 08, 2015#8

                  There is the check box setting Show cancel dialog at Advanced - Configuration - Scripting available since UE v21.30 and UES v14.40 which is by default checked.
                  Best regards from an UC/UE/UES for Windows user from Austria

                  7
                  NewbieNewbie
                  7

                    Sep 08, 2015#9

                    Beautiful. Thanks!