How to quit a script

Help with writing and running scripts

How to quit a script

Postby Danh15 » Fri Nov 23, 2007 10:40 pm

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
User avatar
Danh15
Newbie
 
Posts: 4
Joined: Thu Nov 15, 2007 12:00 am

Re: How to quit a script

Postby jorrasdk » Sat Nov 24, 2007 1:43 pm

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.
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: How to quit a script

Postby Danh15 » Sat Nov 24, 2007 5:54 pm

Thanks, Jorrasdk. That helps.

Dan
User avatar
Danh15
Newbie
 
Posts: 4
Joined: Thu Nov 15, 2007 12:00 am

Re: How to quit a script

Postby Windex » Fri Oct 08, 2010 5:28 pm

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 :)
Windex
Newbie
 
Posts: 2
Joined: Fri Oct 08, 2010 4:18 pm

Re: How to quit a script

Postby Mofi » Sun Oct 10, 2010 6:54 am

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).
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4055
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: How to quit a script

Postby Windex » Mon Oct 11, 2010 11:10 am

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.
Windex
Newbie
 
Posts: 2
Joined: Fri Oct 08, 2010 4:18 pm


Return to Scripts