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,665573
          Grand MasterGrand Master
          6,665573

            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,665573
                Grand MasterGrand Master
                6,665573

                  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!

                    63
                    Advanced UserAdvanced User
                    63

                      Positioning the "Cancel Operation" Dialog Box

                      13:14 - 5 days ago#10

                      Hi everyone,
                      I'm facing an issue where the "Cancel Operation" dialog box appears in the center of the screen when I run a script in UltraEdit. I'd like to change its display position to the top-left corner.
                      I've attached two screenshots to illustrate the current behavior:
                      [Screenshot 1: Current Behavior]
                      Clipboard01.jpg (205.26KiB)

                      [Screenshot 2: Desired Behavior]
                      Clipboard02.jpg (208.75KiB)


                      I've tried searching the forums and online documentation, but haven't found a specific solution. Does anyone know how to adjust the display position of this dialog box?

                      Any help or advice would be greatly appreciated.

                      6,665573
                      Grand MasterGrand Master
                      6,665573

                        16:50 - 5 days ago#11

                        There is no possibility to configure the position of the Cancel Operation dialog window. It is only possible to configure that this dialog window is not displayed at all on execution of an UltraEdit script since UE v21.30 and UES v14.40.

                        The dialog window is opened by UE/UES v2024.1.0.32 in all my test cases with top left corner of the dialog window 100 pixels right to left window border of the main application window and 100 pixels below the top window border of the main application window. It is therefore not displayed in the middle of the screen, except the top left corner or the main application window of UE/UES is near the center of the screen which is a very unlikely main application window position for a text editor or an IDE.

                        It looks like a very old version of UE/UES is used by you which centers the Cancel Operation dialog window. Purchasing license(s) for latest version of UE/UES suitable to the used operating system version would be a solution for this issue.
                        Best regards from an UC/UE/UES for Windows user from Austria

                        63
                        Advanced UserAdvanced User
                        63

                          19:50 - 5 days ago#12

                          Hi Mofi,

                          Thank you for your reply. I understand that there is no direct configuration option to change the location of the "Cancel Operation" dialog window.

                          I would double check my UltraEdit installation and settings to see if there is anything that could be affecting the behavior.

                          Thanks again for your help.