Scrollable icon list (bad param?)

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: Tom, Kukurykus

undavide

Scrollable icon list (bad param?)

Post by undavide »

Hello,
I've found an interesting script in an InDesign related german website - basically it looks in a folder and output a scrollable set of icons:



The script is as follows:

Code: Select allmain();

function main() {
   var folder = Folder.selectDialog ("");
   var files = folder.getFiles();
   var imgs = new Array();
   for (var n = 0; n < files.length; n++)
      if (files[n].name.search(/jpg$/i) != -1)
         imgs.push( files[n].fullName );
   var sui_imgs = new Array();
   for (var n = 0; n < imgs.length; n++)
      sui_imgs.push( ScriptUI.newImage( imgs[n] ) );
   
   var nr_of_rows = Math.floor( imgs.length / 4);
         
   var dlg =
      "dialog {                                     \
         orientation: 'row',                     \
         alignChildren:['fill', 'fill'],\
         pnl: Panel { preferredSize: [400,400], maximumSize: [400,400] },   \
         scrl: Scrollbar { preferredSize: [20,400], minvalue: 0, maxvalue: "+ (nr_of_rows) + ", jumpdelta: 3, stepdelta: 1, value: 0  }   \
         btns_grp: Group { \
            orientation: 'column', alignChildren: ['fill', 'top'],   \
            up: Button { text: 'up' },   \
            down: Button { text: 'down' }, \
            val: StaticText { text: '0' },   \
            cancel: Button {text: 'cancel', alignment: ['fill', 'bottom'] }   \
            }   \
         }";
   var w = new Window(dlg);

   // Die IconButtons erzeugen
   var zeile, n = 0;
   var img_btns = new Array();
   for ( var y = 0; y < 4; y++) {
      zeile = w.pnl.add("group");
      zeile.orientation = "row";
      for ( var x = 0; x < 4; x++) {
         img_btns.push( zeile.add("iconButton",[undefined, undefined, 85, 85]) );
//~          img_btns[n].ix = n;   // index merken
//~          img_btns[n].onDraw = function( state ) {
//~             this.graphics.drawImage( sui_imgs[this.ix], 0, 0, 85, 85);
//~          }
         n++;
      }
   }
   // Die Buttons für Rauf und Runter
   var shift = 0;
   fill_imgs( shift );
   w.btns_grp.up.onClick = function () {
      shift = (shift > 0) ? shift-1 : 0;
      fill_imgs(shift);
      w.scrl.value = shift;
   }
   w.btns_grp.down.onClick = function () {
      shift++ ;
      fill_imgs(shift);
      w.scrl.value = shift;
   }
   // Die Scrollbar
   w.scrl.onChange = function() {
      shift = Math.round(this.value);
      this.value = shift;
      this.window.btns_grp.val.text = shift;
      fill_imgs(shift);
   }


   w.show();

   alert( w.result );
   
   function fill_imgs( shift ) {
      var btn_index, img_index;
      for ( var y = 0; y < 4; y++) {
            for (var x = 0; x < 4 ; x++) {
               btn_index = y * 4 + x;
               img_index = shift * 4 + btn_index;
               if (img_index < imgs.length) {
                  img_btns[ btn_index ].image = sui_imgs[ img_index ];
//~                   img_btns[ btn_index ].ix = img_index;
//~                   img_btns[ btn_index ].onDraw = function( state ) {
//~                      this.graphics.drawImage( sui_imgs[this.ix], 0, 0, 85, 85);
//~                   }
                  img_btns[ btn_index ].asset = imgs[ img_index ];
                  img_btns[ btn_index ].visible = true;
                  img_btns[ btn_index ].onClick = function () {
                     this.window.result = this.asset;
                     this.window.close();
                  }
               } else {
                  img_btns[ btn_index ].visible = false;
               }
            }
      }
   }
}

As it is it works fine, but it crops the images. In order to resize them, uncomment the commented lines.
It works good in ESTK, while PS (CS6 and CC) returns a bad argument list in the .onDraw() function - I can't understand why.

Besides that (canceling the error), the images display fine, though the aliasing in PS is awful compared to ESTK (I guess we've to cope with that)
I'm wondering what's wrong with the .onDraw() - any suggestion?

Thank you,

Davide
Mike Hale

Scrollable icon list (bad param?)

Post by Mike Hale »

It is strange. If you insert debugger lines in the onDraw functions the code doesn't stop when the target is ESTK. When the target is Photoshop it does stop and the error is caused because of a bad index( this.ix is exceeds sui_imgs.length ). Just as a quick test I added a test to make sure sui_imgs[this.ix] is valid and it now works in Photoshop without errors.

Of course a better fix would be to correct the bad index.

Code: Select all    main();

    function main() {
       var folder = Folder.selectDialog ("");
       var files = folder.getFiles();
       var imgs = new Array();
       for (var n = 0; n < files.length; n++)
          if (files[n].name.search(/jpg$/i) != -1)
             imgs.push( files[n].fullName );
       var sui_imgs = new Array();
       for (var n = 0; n < imgs.length; n++)
          sui_imgs.push( ScriptUI.newImage( imgs[n] ) );
       
       var nr_of_rows = Math.floor( imgs.length / 4);
             
       var dlg =
          "dialog {                                     \
             orientation: 'row',                     \
             alignChildren:['fill', 'fill'],\
             pnl: Panel { preferredSize: [400,400], maximumSize: [400,400] },   \
             scrl: Scrollbar { preferredSize: [20,400], minvalue: 0, maxvalue: "+ (nr_of_rows) + ", jumpdelta: 3, stepdelta: 1, value: 0  }   \
             btns_grp: Group { \
                orientation: 'column', alignChildren: ['fill', 'top'],   \
                up: Button { text: 'up' },   \
                down: Button { text: 'down' }, \
                val: StaticText { text: '0' },   \
                cancel: Button {text: 'cancel', alignment: ['fill', 'bottom'] }   \
                }   \
             }";
       var w = new Window(dlg);

       // Die IconButtons erzeugen
       var zeile, n = 0;
       var img_btns = new Array();
       for ( var y = 0; y < 4; y++) {
          zeile = w.pnl.add("group");
          zeile.orientation = "row";
          for ( var x = 0; x < 4; x++) {
             img_btns.push( zeile.add("iconButton",[undefined, undefined, 85, 85]) );
           img_btns[n].ix = n;   // index merken
          img_btns[n].onDraw = function( state ) {
              $.level =1;
              debugger;
             if(sui_imgs[this.ix]!=undefined) this.graphics.drawImage( sui_imgs[this.ix], 0, 0, 85, 85);
         }
             n++;
          }
       }
       // Die Buttons für Rauf und Runter
       var shift = 0;
       fill_imgs( shift );
       w.btns_grp.up.onClick = function () {
          shift = (shift > 0) ? shift-1 : 0;
          fill_imgs(shift);
          w.scrl.value = shift;
       }
       w.btns_grp.down.onClick = function () {
          shift++ ;
          fill_imgs(shift);
          w.scrl.value = shift;
       }
       // Die Scrollbar
       w.scrl.onChange = function() {
          shift = Math.round(this.value);
          this.value = shift;
          this.window.btns_grp.val.text = shift;
          fill_imgs(shift);
       }


       w.show();

       alert( w.result );
       
       function fill_imgs( shift ) {
          var btn_index, img_index;
          for ( var y = 0; y < 4; y++) {
                for (var x = 0; x < 4 ; x++) {
                   btn_index = y * 4 + x;
                   img_index = shift * 4 + btn_index;
                   if (img_index < imgs.length) {
                      img_btns[ btn_index ].image = sui_imgs[ img_index ];
                     img_btns[ btn_index ].ix = img_index;
                      img_btns[ btn_index ].onDraw = function( state ) {
                          $.level =1;
                         debugger;
                      if(sui_imgs[this.ix]!=undefined) this.graphics.drawImage( sui_imgs[this.ix], 0, 0, 85, 85);
                    }
                      img_btns[ btn_index ].asset = imgs[ img_index ];
                      img_btns[ btn_index ].visible = true;
                      img_btns[ btn_index ].onClick = function () {
                         this.window.result = this.asset;
                         this.window.close();
                      }
                   } else {
                      img_btns[ btn_index ].visible = false;
                   }
                }
          }
       }
    }
undavide

Scrollable icon list (bad param?)

Post by undavide »

Thanks Mike!

Davide

("strange" is always there when PS scripting is involved )