4 Replies Latest reply on Jan 27, 2013 11:55 AM by Gustavo Del Vechio

    How to abort the run of a code in the middle

    Gustavo Del Vechio Level 3

      Hi friends

       

      I'm writting a JavaScript script for Photoshop and in the middle of the code I inserted a Confirm call (variable receiving this is called "continue1")...If user click Yes button Then, continue to run.

       

      So I need to tell the code "If continue1==false then abort and do not read the code lines remaining.

       

      Sure I could embrace the remaining code with an If (continue1==true){

      ...

      }

       

      So code wont be executed if user clicked no in the confirmation.

      But...

       

      Is there any reserved word or method that stops the execution of a JavaScript in the middle of the process?

       

      (If I was inside a function I could use "return") but I'm not inside a function...so a return would not work.

       

      Thank you for the tip.

      Best regards

       

      Gustavo

        • 1. Re: How to abort the run of a code in the middle
          Muppet Mark Level 5

          I don't think there is… When scripting PS, AI or BR… I wrap in functions to return and exit script… With ID you can just call exit(); and bow out as you probably wish…

          • 2. Re: How to abort the run of a code in the middle
            Gustavo Del Vechio Level 3

            Thank you Muppet

             

            So the way to go is to embrace the remaining code with an "if" statement....so it continues only if the condition is true.

             

            Thank you a lot

             

            Best Regards

            Gustavo

            • 3. Re: How to abort the run of a code in the middle
              JJMack Most Valuable Participant

              An other way it to wrap the whole script in a try catch structure and through an error coder for errors encountered.  Here is a simple script I wrote to throw image into a document to be printed one roll paper. I knew before hand users would be able to make setting that would not work or select too may image for the remaining roll length to make it easy to exit at any point I just raped it all in a try catch.

              /* ==========================================================
              // 2012  John J. McAssey (JJMack) 
              // ======================================================= */
              
              
              // This script is supplied as is. It is provided as freeware. 
              // The author accepts no liability for any problems arising from its use.
              
              
              /* Help 
              <javascriptresource>
              <about>$$$/JavaScripts/PasteImageRoll/About=JJMack's PasteImageRoll^r^rCopyright 2012 Mouseprints.^r^rCreate a document for printing on roll paper^rcan also be used as a wall hanging when^rall selected images have the same orientation.^rImages will be rotated to match cell orientation</about>
              <category>JJMack's Collage Script</category>
              </javascriptresource>
              */
              
              
              //Set Defaults here
              var dfltRes = 300;          // default print DPI
              var dfltCpys = 1;          // default image copies
              var dfltPw  = 16;          // default roll paper width in inches
              var dfltPl  = '';          // default roll paper length in feet. if set to null script will use 100 ft.
              var dfltCw  = 4;          // default cell width in inches best if it divides paper with evenly.
              var dfltCh  = 6;          // default cell height in inches
              //End Defaults
              
              
              var startRulerUnits = app.preferences.rulerUnits;
              app.preferences.rulerUnits = Units.PIXELS; // tell ps to work with pixels
              
              
              try {
                        // begin dialog layout
                        var RollPaperDialog = new Window('dialog');
                        RollPaperDialog.text = 'Paste Image Roll';
                        RollPaperDialog.frameLocation = [70, 70];
                        RollPaperDialog.alignChildren = 'center';
              
              
                        RollPaperDialog.PrintResPnl = RollPaperDialog.add('panel', [2, 2, 200, 56], 'Print Resolution');
                                  RollPaperDialog.PrintResPnl.add('statictext', [10, 16, 50, 48], 'DPI ');
                                  RollPaperDialog.PrintResPnl.docResEdt = RollPaperDialog.PrintResPnl.add('edittext', [50, 13, 90, 34], dfltRes, {name:'prtRes'});
                                  RollPaperDialog.PrintResPnl.docResEdt.helpTip = 'Image Resolution';
                                  RollPaperDialog.PrintResPnl.add('statictext', [96, 16, 140, 48], 'Copies ');
                                  RollPaperDialog.PrintResPnl.imgCpysEdt = RollPaperDialog.PrintResPnl.add('edittext', [140, 13, 175, 34], dfltCpys, {name:'imgCpys'});
                                  RollPaperDialog.PrintResPnl.imgCpysEdt.helpTip = 'Number of copies of selected Images';
              
              
                        RollPaperDialog.PaperSizePnl = RollPaperDialog.add('panel', [2, 2, 200, 56], 'Roll Paper Size');
                                  RollPaperDialog.PaperSizePnl.add('statictext', [10, 16, 50, 48], 'Width ');
                                  RollPaperDialog.PaperSizePnl.aspectWidthEdt = RollPaperDialog.PaperSizePnl.add('edittext', [50, 13, 90, 34], dfltPw, {name:'pprWth'});
                                  RollPaperDialog.PaperSizePnl.aspectWidthEdt.helpTip = 'Roll width in inches';
                                  RollPaperDialog.PaperSizePnl.add('statictext', [96, 16, 140, 48], 'Length ');
                                  RollPaperDialog.PaperSizePnl.aspectHeightEdt = RollPaperDialog.PaperSizePnl.add('edittext', [140, 13, 175, 34], dfltPl, {name:'pprLnth'});
                                  RollPaperDialog.PaperSizePnl.aspectHeightEdt.helpTip = 'Remaing roll length in feet';
              
              
                        RollPaperDialog.CellSizePnl = RollPaperDialog.add('panel', [2, 2, 200, 56], 'Tile Cell Size');
                                  RollPaperDialog.CellSizePnl.add('statictext', [10, 16, 50, 48], 'Width ');
                                  RollPaperDialog.CellSizePnl.aspectWidthEdt = RollPaperDialog.CellSizePnl.add('edittext', [50, 13, 90, 34], dfltCw, {name:'cllWth'});
                                  RollPaperDialog.CellSizePnl.aspectWidthEdt.helpTip = 'Width in inches';
                                  RollPaperDialog.CellSizePnl.add('statictext', [96, 16, 140, 48], 'Height ');
                                  RollPaperDialog.CellSizePnl.aspectHeightEdt = RollPaperDialog.CellSizePnl.add('edittext', [140, 13, 175, 34], dfltCh, {name:'cllHgt'});
                                  RollPaperDialog.CellSizePnl.aspectHeightEdt.helpTip = 'Height in inches';
              
              
                        var buttons = RollPaperDialog.add('group');
                        buttons.orientation = 'row';
                                            var okBtn = buttons.add('button');
                                  okBtn.text = 'OK';
                                  okBtn.properties = {name: 'ok'};
                                            var cancelBtn = buttons.add('button');
                                  cancelBtn.text = 'Cancel';
                                  cancelBtn.properties = {name: 'cancel'};
              
              
                        // display dialog and only continues on OK button press (OK = 1, Cancel = 2)
                        if (RollPaperDialog.show() == 1) {
                                  //variables passed from user interface
                                  var res                    = String(RollPaperDialog.PrintResPnl.prtRes.text);
                                  var copies          = String(RollPaperDialog.PrintResPnl.imgCpys.text);
                                  var pprwidth    = String(RollPaperDialog.PaperSizePnl.pprWth.text);
                                  var pprlength   = String(RollPaperDialog.PaperSizePnl.pprLnth.text); if (pprlength=='') { pprlength= 100; }
                                  var cellwidth   = String(RollPaperDialog.CellSizePnl.cllWth.text);
                                  var cellheight  = String(RollPaperDialog.CellSizePnl.cllHgt.text);
              
              
                                  var maxpaperwidth=pprwidth*res;                    // Printer Paper width in pixels inches*res  
                                  var maxpaperlnth=pprlength*12*res;          // Printer Paper Roll length in pixels
                                  var width=cellwidth*res;                    // Document Cell width in pixels inches*res
                                  var height=cellheight*res;                    // Document Cell height in pixels inches*res
                                  var cols=0;                                        // Document number of columns will be determined by script using paper width and cell width  
                                  var rows=0;                                        // Document rows will be determined by script using columns and # of images selected 
              
              
                                  if (width>maxpaperwidth) { throw "error1"; }
                                  cols=Math.round((maxpaperwidth/width)-.499); //round down
                                  if (height>maxpaperlnth) { throw "error2"; }
                                  var file = new Array();
                                  file = app.openDialog();//opens dialog,choose images
                                  if (file.length<1) { throw "error3"; }
                                  rows=Math.round((file.length*copies/cols)+.499); //round up
                                  if (height*rows>maxpaperlnth) { throw "error4"; }
                                  var doc = app.documents.add(width*cols, height*rows, res);
                                  var currrow=0; var pasted=0; 
                                  for (var i=0;i<file.length;i++) {
                                            app.load(file[i]); //load it into documents
                                            var backFile= app.activeDocument; //prepare your image layer as active document
                                            flatten(); //handle layered images
                                            if (backFile.width.value<backFile.height.value&&width>height ) { backFile.rotateCanvas(-90.0);  } // Rotate portraits
                                            if (backFile.height.value<backFile.width.value&&height>width ) { backFile.rotateCanvas(-90.0);  } // Rotate landscapes
                                            if (backFile.width.value/backFile.height.value > width/height) { backFile.resizeImage(null, height, null, ResampleMethod.BICUBIC); } // wider
                                            else {backFile.resizeImage(width, null, null, ResampleMethod.BICUBIC);} // same aspect ratio or taller
                                            backFile.selection.selectAll();
                                            backFile.selection.copy(); //copy resized image into clipboard
                                            backFile.close(SaveOptions.DONOTSAVECHANGES); //close image without saving changes
                
                                            for (var n=0;n<copies;n++) { 
              
              
                                                      var x =pasted*width;
                                                      var y =currrow*height;
                                                      var selectedRegion = Array(Array(x,y), Array(x+width,y), Array(x+width,y+height), Array(x,y+height));
              
              
                                                      doc.selection.select(selectedRegion);
                                                      doc.paste(true); //paste image into masked layer your document
                                                      doc.selection.select(selectedRegion);
                                                      align('AdCH'); align('AdCV');
                                                      doc.selection.deselect();
                                                      pasted++
                                                      if ( pasted==cols ) { pasted=0; currrow++; }
                                                      }
                                            }
                                  }
                        else {
                                  //alert('Operation Canceled.');
                                  }
                        // Return the app preferences
                        app.preferences.rulerUnits = startRulerUnits;
                        }
              catch(err){
                        // Return the app preferences
                        app.preferences.rulerUnits = startRulerUnits;
                        if (err=="error1") {alert("Paper width exceeded reduce the cell width");}
                        else if (err=="error2") {alert("Paper roll length exceeded reduce cell height");}
                        else if (err=="error3") {alert("No Images Selected");}
                        else if (err=="error4") {alert("Paper roll length exceeded try selecting fewer images or reducing cell height");}
                        // Lot's of things can go wrong, Give a generic alert and see if they want the details
                        else if ( confirm("Sorry, something major happened and I can't continue! Would you like to see more info?" ) ) { alert(err + ': on line ' + err.line ); }
                        }
              
              
              // -----------------------------------------
              // flatten Image
              // -----------------------------------------
              function flatten() {
                        try{
                                  executeAction( charIDToTypeID( "FltI" ), undefined, DialogModes.NO );
                        }catch(e){}
              }
              
              
              // -----------------------------------------
              // Align Layers to selection
              // -----------------------------------------
              function align(method) {
                        var desc = new ActionDescriptor();
                        var ref = new ActionReference();
                        ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
                        desc.putReference( charIDToTypeID( "null" ), ref );
                        desc.putEnumerated( charIDToTypeID( "Usng" ), charIDToTypeID( "ADSt" ), charIDToTypeID( method ) );
                        try{
                                  executeAction( charIDToTypeID( "Algn" ), desc, DialogModes.NO );
                        }catch(e){}
              }
              
              
              1 person found this helpful
              • 4. Re: How to abort the run of a code in the middle
                Gustavo Del Vechio Level 3

                Thank you Muppet and JJMack...That´s what I was thinking.

                 

                Thank you a lot

                Best Regards

                Gustavo.