9 Replies Latest reply on Oct 15, 2015 2:53 PM by Qwertyfly...

    Assign an .onClick() function to a generic/automatically created button?

    williamadowling Level 4

      Hey Scripters.


      I'm back again with another noodle scratcher. I'm attempting to write a script UI dialog with one simple checkbox at the top, then a group of buttons underneath. The number of buttons generated and their display text will vary from doc to doc, so I am creating the buttons with a loop. However, because of this i encountered an issue I hadn't anticipated.


      I want to set each button to run a function that sets a couple variables and then closes the dialog window. Normally, I would use:



                set variables





      unfortunately, since I'm using a loop to create each button, i don't have a unique variable set for each button, so I don't know how to assign the .onClick().... Any thoughts on this?


      here's the jumbled mess of code I'm working with as of right now.. I apologize in advance..


      function placeAdditionalArt(addArtLayer){
              var addArt = addArtLayer.pageItems[0];
              var layerName = addArtLayer.name;
              var pieces = wearerLayer.layers["Prepress"].layers[0].pageItems;
              //begin script ui window// determine destination shirt piece and whether or not to scale
              var aaLoc = new Window("dialog", "Additional Artwork");
              var aaLocInfo = createAALoc();
              function createAALoc(){
                  var result;
                  var scale;
                  var scaleGroup = aaLoc.add("group");
                      var yesNoscale = scaleGroup.add("checkbox", undefined, "Scale Artwork");
                  var pieceGroup = aaLoc.add("group");
                      for(var a=0;a<pieces.length;a++){
                          var thisPiece = pieces[a];
                          var thisName = pieces[a].name;
                          if(thisName.indexOf("Waistband")<0 && thisName.indexOf("Cuff")<0 &&
                          thisName.indexOf("Collar")<0 && thisName.indexOf("Placard")<0){
                              pieceGroup.add("button", undefined, thisName);
                              this.onClick = function(){ //this.onClick doesn't work because i imagine this refers to pieceGroup in this context..
                                  result = thisPiece;
                                      scale = true;
                  return [result,scale];
              var destPiece = aaLocInfo[0];
              var scaleAddArt = aaLocInfo[1];
              // end script ui
              for(var a=0;a<prepressLayer.layers.length;a++){
                  var curSize = prepressLayer.layers[a].name;
                  var dest = prepressLayer.layers[a].groupItems.getByName(curSize + " " + destPiece); 
                  var scale = (newWidth / addArt.width)*100;
                  var addArtCopy = addArt.duplicate(dest);
                  newWidth = newWidth + addscale;
                  addArtCopy.name = curSize + " Additional Art";