10 Replies Latest reply on Oct 11, 2008 9:32 AM by (ebanat)

    How terminate script?

      My script is sometimes long running. I want to add functionality to canceled one:

      var fl = true;
      var win = new Window('window', '');
      var cancelButton = win.add ('button', undefined, 'Cancel');
      cancelButton.onClick = function()
      {
      alert('canceled');
      fl = false;
      }

      win.show();

      for (var i = 0; fl && i < 100000; i++)
      {

      // my code here

      app.version;

      }

      win.close(1);

      ScriptUI does not click button.
      Here is something not lacking?
        • 1. Re: How terminate script?
          Level 1
          Not sure if I understand you, but you can stop execution of your code with: exit();
          • 2. Re: How terminate script?
            Level 1
            sorry

            how send this message? button not work
            • 3. Re: How terminate script?
              [Jongware] Most Valuable Participant
              Eh?

              cancelButton.onClick = function()
              {
              alert('canceled');
              exit(0);
              }
              • 4. Re: How terminate script?
                Level 1
                Thanks.
                However, thus too does not work.

                While running script mouse cursor is "beach ball". The button does not work, and alert does not show, etc.
                • 5. Re: How terminate script?
                  Level 1
                  Either call your for() loop from within the onClick or close the window there
                  instead, like this.

                  > cancelButton.onClick = function()
                  > {
                  > alert('canceled');
                  > fl = false;
                  win.close(1);
                  > }
                  >
                  > win.show();
                  >
                  > for (var i = 0; fl && i < 100000; i++) {
                  >
                  > // my code here
                  >
                  > app.version;
                  >
                  > }
                  >
                  // win.close(1);


                  -X
                  • 6. Re: How terminate script?
                    Level 1
                    hm..

                    Any way that the _user_ break a prolonged script?

                    Probably, keyboard shortcut?
                    • 7. Re: How terminate script?
                      Level 1
                      > Any way that the _user_ break a prolonged script?
                      >
                      > Probably, keyboard shortcut?

                      ESC-ESC-ESC-ESC-ESC-ESC-ESC-ESC


                      The other way is to have a dialog with progress bar and a 'Cancel' button. I
                      tend to do that with long running Photoshop scripts.

                      -X
                      • 8. Re: How terminate script?
                        Level 1
                        >ESC-ESC-ESC-ESC-ESC-ESC-ESC-ESC

                        ok its good, thanks

                        >The other way is to have a dialog with progress bar and a 'Cancel' button

                        In my first message attempt of a write of such dialogue (a progressbar I have removed - with it there are no problems). Difficulty that "beach ball" does not allow the user to press the button.
                        • 9. Re: How terminate script?
                          Level 1
                          ebanat@adobeforums.com wrote:
                          >
                          > ESC-ESC-ESC-ESC-ESC-ESC-ESC-ESC
                          >
                          > ok its good, thanks

                          Sometimes you have to repeat yourself with ScriptUI to make yourself heard :)

                          >
                          > In my first message attempt of a write of such dialogue (a progressbar I have removed - with it there are no problems).

                          My implementation is below.


                          > Difficulty that "beach ball" does not allow the user to press the button.

                          I've had mixed results. In some scripts it works fine, in others, not at all.
                          And it varies across Adobe Apps and versions and which OS you happen to be
                          running on.

                          Bob Stucky may have a better threaded solution from some of the Bridge UI work
                          that he's done in the past.


                          Yet another way of tackling the problem is to have the progress bar UI running
                          in Bridge. The ID script would launch the progress UI via BridgeTalk and have to
                          periodically check and update that UI. This has the added advantage that the
                          progress bar is in a completely separate process from ID so there is no UI
                          contention going on. I never got around to implementing it, though, as the need
                          was never really there.



                          //
                          // createProgressPalette
                          // title the window title
                          // min the minimum value for the progress bar
                          // max the maximum value for the progress bar
                          // parent the parent ScriptUI window (opt)
                          // useCancel flag for having a Cancel button (opt)
                          //
                          // onCancel This method will be called when the Cancel button is pressed.
                          // This method should return 'true' to close the progress window
                          //
                          function createProgressPalette(title, min, max,
                          parent, useCancel) {

                          var win = new Window('palette', title);
                          win.bar = win.add('progressbar', undefined, min, max);
                          win.bar.preferredSize = [300, 20];

                          win.parentWin = undefined;
                          win.recenter = false;
                          win.isDone = false;

                          if (parent) {
                          if (parent instanceof Window) {
                          win.parentWin = parent;
                          } else if (useCancel == undefined) {
                          useCancel = !!parent;
                          }
                          }

                          if (useCancel) {
                          win.onCancel = function() {
                          this.isDone = true;
                          return true; // return 'true' to close the window
                          }

                          win.cancel = win.add('button', undefined, 'Cancel');

                          win.cancel.onClick = function() {
                          var win = this.parent;
                          try {
                          win.isDone = true;
                          if (win.onCancel) {
                          var rc = win.onCancel();
                          if (rc != false) {
                          win.close();
                          }
                          } else {
                          win.close();
                          }
                          } catch (e) {
                          alert(e);
                          }
                          }
                          }

                          win.onClose = function() {
                          this.isDone = true;
                          }

                          win.updateProgress = function(val) {
                          var win = this;

                          if (val != undefined) {
                          win.bar.value = val;
                          }

                          if (win.recenter) {
                          win.center(win.parentWin);
                          }

                          win.show();
                          win.hide();
                          win.show();
                          }

                          win.recenter = true;
                          win.center(win.parent);

                          return win;
                          };
                          • 10. Re: How terminate script?
                            Level 1
                            BridgeTalk the rules way is similar. However much is unclear