6 Replies Latest reply on Jun 21, 2013 6:27 AM by Mark Ashton

    ExtendToolscript opens when I call script from photoshop

    Mark Ashton Level 1

      Hey,

       

       

      I asked this question over in general discussions by mistake so I though I would re-ask it here.

       

      I have a Javascript that I have written that builds a interface of buttons in organised groups based on the contents of certain folders. Everything works fine but when you call it from photoshop and extendtoolscript isn't running it always opens it up. I have written a lot of scripts with interfaces and I have never had this problem. Its not a huge problem for me as I usually have this program open anyway but I intend to have a team of people using this script so if I could stop it somehow it would be much better.

       

      Has anyone experienced this before? Or perhaps knows of functions that cause extendscript to open?

       

      I can't see anything unusual in the script...

       

      Any help would be appreciated!

       

      Thanks,

       

      Mark

       

       

       

       

      var strtRulerUnits = app.preferences.rulerUnits;
      var strtTypeUnits = app.preferences.typeUnits;
      app.preferences.rulerUnits = Units.PIXELS;
      app.preferences.typeUnits = TypeUnits.PIXELS;
      
      
      
      
      var templatepath = "~/Desktop/BANNERS/";
      
      
      var bannerfile = ""
      var docRef = app.activeDocument;
      
      
      
      
      var fileArray = new Array();
      
      
                      var sourceFolder = Folder (templatepath + "MAIN/");
      
      
                      var extRE = /\.(?:png|gif|jpg|bmp|tif|psd)$/i;
                      
                      // get all files in source folder
                      var docs = sourceFolder.getFiles();
                      var len = docs.length;
                      for (var i = 0; i < len; i++) {
                                      var doc = docs[i];
      
      
                                      // only match files (not folders)
                                      if (doc instanceof File) {
                                                      // store all recognized files into an array
                                                      var docName = doc.name;
                                                      if (docName.match(extRE)) {
                                                                      fileArray.push(doc);
                                                      }
                                      }
                      }
                  
                  
                  var w = new Window ("dialog", "Choose a Banner");
                  w.preferredSize = [400, 100];
                  w.orientation = 'row';
                  w.alignChildren = "left"
                  w.alignChildren = [ "fill", "fill"];
                  
                  w.btnPnl = w.add('panel', undefined, 'MAIN');
                  
                  w.btnPnl.btn = w.add ("group");
                  w.btnPnl.btn.alignChildren = "left"
                  
                  var buttonarray = new Array ();
                  var key = {};
                  
                  
                  
                  for (var i = 0; i < fileArray.length ; i++) {
                      
                      var delimeter = '/';
                      var string = String (fileArray[i]);
                      var splitted = string.split(delimeter);
                      var thelength = ((splitted.length) - 1);
                      var bannername = splitted[thelength];
                      var delimeter = '.';
                      var splitted = bannername.split(delimeter);
                      var bannername = splitted[0];
                      
                      
                      var checkdelim = "%20";
                      var checkthat =  bannername.indexOf(checkdelim);
                      if (checkthat == "-1") {}
                      else {
                          
                      var delimeter = '%20';
                      var splitted = bannername.split(delimeter);
                      var counter = 0;
                      while (counter < splitted.length){
                      
                      if (counter == 0){
                      var bannername = (splitted[counter] + " ");    
                      }
                      else if (counter == ((splitted.length) - 1)){
                      var bannername = (bannername + splitted[counter]);
                      }
                      else{
                      var bannername = (bannername + splitted[counter] + " ");
                      }
                      var counter = (counter + 1);
                      }
                  
                      }
                  if (fileArray.length > 6){
                          var divider = 2
                          }
                      else{
                          var divider = 1
                          }
                      
                      var remainder = (i % divider);
                      if (remainder == 0){  
                      w.btnPnl.btn = w.btnPnl.add ("group");
                      w.btnPnl.btn.alignChildren = "left"
                      } else {
                      //x is not a multiple of y
                      }
                      
                      key = (String (fileArray[i]));
                      w[key] = w.btnPnl.btn.add("button", undefined ,bannername ); 
                      w[key].alignment = 'left';
                      buttonarray.push(w[key]);
                      
                      w.btnPnl.btn.addEventListener('click', button_pressed);
                      
                      function button_pressed (e)
                      {
                     if (e.target.type == "button")
                      $.writeln (e.target.text + " from panel " + e.target.parent.text);
                      w.close()
                      ////////////////////
                      var buttonname = e.target.text;
                      var checkdelim = " ";
                      var checkthat = buttonname.indexOf(checkdelim);
                      if (checkthat == "-1") {}
                      else {
                          
                      var delimeter = ' ';
                      var splitted = buttonname.split(delimeter);
                      var counter = 0;
                      while (counter < splitted.length){
                      
                      if (counter == 0){
                      var buttonname = (splitted[counter] + "%20");    
                      }
                      else if (counter == ((splitted.length) - 1)){
                      var buttonname = (buttonname + splitted[counter]);
                      }
                      else{
                      var buttonname = (buttonname + splitted[counter] + "%20");
                      }
                      var counter = (counter + 1);
                      }
                  }
                  
                      ///////////////////////
                      
                      bannerfile = new File (templatepath + "MAIN/" + buttonname + '.png');
                      
                       }
                   }
               
       ///////////////////////////////////////////////////////////
       ////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////
       ////////////////////////////////////////////////////////////
      
      var fileArrayneon = new Array();
      
      
                      var sourceFolder = Folder (templatepath + "NEON/");
      
      
                      var extRE = /\.(?:png|gif|jpg|bmp|tif|psd)$/i;
                      
                      // get all files in source folder
                      var docs = sourceFolder.getFiles();
                      var len = docs.length;
                      for (var i = 0; i < len; i++) {
                                      var doc = docs[i];
      
      
                                      // only match files (not folders)
                                      if (doc instanceof File) {
                                                      // store all recognized files into an array
                                                      var docName = doc.name;
                                                      if (docName.match(extRE)) {
                                                                      fileArrayneon.push(doc);
                                                      }
                                      }
                      }
                  
                  
                  
                  
                  w.btnPnl = w.add('panel', undefined, 'NEON');
                  
                  w.btnPnl.btn = w.add ("group");
                  
                  var buttonarray = new Array ();
                  var key = {};
                  
                  
                  
                  for (var i = 0; i < fileArrayneon.length ; i++) {
                      
                      var delimeter = '/';
                      var string = String (fileArrayneon[i]);
                      var splitted = string.split(delimeter);
                      var thelength = ((splitted.length) - 1);
                      var bannername = splitted[thelength];
                      var delimeter = '.';
                      var splitted = bannername.split(delimeter);
                      var bannername = splitted[0];
                      
                      
                      var checkdelim = "%20";
                      var checkthat =  bannername.indexOf(checkdelim);
                      if (checkthat == "-1") {}
                      else {
                          
                      var delimeter = '%20';
                      var splitted = bannername.split(delimeter);
                      var counter = 0;
                      while (counter < splitted.length){
                      
                      if (counter == 0){
                      var bannername = (splitted[counter] + " ");    
                      }
                      else if (counter == ((splitted.length) - 1)){
                      var bannername = (bannername + splitted[counter]);
                      }
                      else{
                      var bannername = (bannername + splitted[counter] + " ");
                      }
                      var counter = (counter + 1);
                      }
                  
                      }
                  
                  if (fileArrayneon.length > 6){
                          var divider = 2
                          }
                      else{
                          var divider = 1
                          }
                      
                      var remainder = (i % divider);
                      if (remainder == 0){  
                      w.btnPnl.btn = w.btnPnl.add ("group");
                      w.btnPnl.btn.alignChildren = "left"
                      } else {
                      //x is not a multiple of y
                      }
                      
                      key = (String (fileArrayneon[i]));
                      w[key] = w.btnPnl.btn.add("button", undefined ,bannername ); 
                      w[key].alignment = 'left';
                      buttonarray.push(w[key]);
                      
                      w.btnPnl.btn.addEventListener('click', button_pressed);
                      
                      function button_pressed (e)
                      {
                     if (e.target.type == "button")
                      $.writeln (e.target.text + " from panel " + e.target.parent.text);
                      w.close()
                      ////////////////////
                      var buttonname = e.target.text;
                      var checkdelim = " ";
                      var checkthat = buttonname.indexOf(checkdelim);
                      if (checkthat == "-1") {}
                      else {
                          
                      var delimeter = ' ';
                      var splitted = buttonname.split(delimeter);
                      var counter = 0;
                      while (counter < splitted.length){
                      
                      if (counter == 0){
                      var buttonname = (splitted[counter] + "%20");    
                      }
                      else if (counter == ((splitted.length) - 1)){
                      var buttonname = (buttonname + splitted[counter]);
                      }
                      else{
                      var buttonname = (buttonname + splitted[counter] + "%20");
                      }
                      var counter = (counter + 1);
                      }
                  }
                  
                      ///////////////////////
                      
                      bannerfile = new File (templatepath + "NEON/" + buttonname + '.png');
                      
                       }
                   } 
        ///////////////////////////////////////////////////////////
       ////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////
       ////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////
      var fileArray180 = new Array();
      
      
                      var sourceFolder = Folder (templatepath + "180/");
      
      
                      var extRE = /\.(?:png|gif|jpg|bmp|tif|psd)$/i;
                      
                      // get all files in source folder
                      var docs = sourceFolder.getFiles();
                      var len = docs.length;
                      for (var i = 0; i < len; i++) {
                                      var doc = docs[i];
      
      
                                      // only match files (not folders)
                                      if (doc instanceof File) {
                                                      // store all recognized files into an array
                                                      var docName = doc.name;
                                                      if (docName.match(extRE)) {
                                                                      fileArray180.push(doc);
                                                      }
                                      }
                      }
                  
                  
                  
                  
                  w.btnPnl = w.add('panel', undefined, '180');
                  
                  w.btnPnl.btn = w.add ("group");
                  
                  var buttonarray = new Array ();
                  var key = {};
                  
                  
                  
                  for (var i = 0; i < fileArray180.length ; i++) {
                      
                      var delimeter = '/';
                      var string = String (fileArray180[i]);
                      var splitted = string.split(delimeter);
                      var thelength = ((splitted.length) - 1);
                      var bannername = splitted[thelength];
                      var delimeter = '.';
                      var splitted = bannername.split(delimeter);
                      var bannername = splitted[0];
                      
                      
                      var checkdelim = "%20";
                      var checkthat =  bannername.indexOf(checkdelim);
                      if (checkthat == "-1") {}
                      else {
                          
                      var delimeter = '%20';
                      var splitted = bannername.split(delimeter);
                      var counter = 0;
                      while (counter < splitted.length){
                      
                      if (counter == 0){
                      var bannername = (splitted[counter] + " ");    
                      }
                      else if (counter == ((splitted.length) - 1)){
                      var bannername = (bannername + splitted[counter]);
                      }
                      else{
                      var bannername = (bannername + splitted[counter] + " ");
                      }
                      var counter = (counter + 1);
                      }
                  
                      }
                          
                     if (fileArray180.length > 6){
                          var divider = 2
                          }
                      else{
                          var divider = 1
                          }
                      
                      var remainder = (i % divider);
                      
                      
                      if (remainder == 0){  
                      w.btnPnl.btn = w.btnPnl.add ("group");
                      w.btnPnl.btn.alignChildren = "left"
                      } else {
                      //x is not a multiple of y
                      }
                      
                      key = (String (fileArray180[i]));
                      w[key] = w.btnPnl.btn.add("button", undefined ,bannername ); 
                      w[key].alignment = 'left';
                      buttonarray.push(w[key]);
                      
                      w.btnPnl.btn.addEventListener('click', button_pressed);
                      
                      function button_pressed (e)
                      {
                     if (e.target.type == "button")
                      $.writeln (e.target.text + " from panel " + e.target.parent.text);
                      w.close()
                      ////////////////////
                      var buttonname = e.target.text;
                      var checkdelim = " ";
                      var checkthat = buttonname.indexOf(checkdelim);
                      if (checkthat == "-1") {}
                      else {
                          
                      var delimeter = ' ';
                      var splitted = buttonname.split(delimeter);
                      var counter = 0;
                      while (counter < splitted.length){
                      
                      if (counter == 0){
                      var buttonname = (splitted[counter] + "%20");    
                      }
                      else if (counter == ((splitted.length) - 1)){
                      var buttonname = (buttonname + splitted[counter]);
                      }
                      else{
                      var buttonname = (buttonname + splitted[counter] + "%20");
                      }
                      var counter = (counter + 1);
                      }
                  }
                  
                      ///////////////////////
                      
                      bannerfile = new File (templatepath + "180/" + buttonname + '.png');
                      
                       }
                   }   
        
      
      
      
       ///////////////////////////////////////////////////////////
       ////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////
       ////////////////////////////////////////////////////////////
      
      
      var fileArraymisc = new Array();
      
      
                      var sourceFolder = Folder (templatepath + "MISC/");
      
      
                      var extRE = /\.(?:png|gif|jpg|bmp|tif|psd)$/i;
                      
                      // get all files in source folder
                      var docs = sourceFolder.getFiles();
                      var len = docs.length;
                      for (var i = 0; i < len; i++) {
                                      var doc = docs[i];
      
      
                                      // only match files (not folders)
                                      if (doc instanceof File) {
                                                      // store all recognized files into an array
                                                      var docName = doc.name;
                                                      if (docName.match(extRE)) {
                                                                      fileArraymisc.push(doc);
                                                      }
                                      }
                      }
                  
                  
                  
                  
                  w.btnPnl = w.add('panel', undefined, 'MISC');
                  
                  w.btnPnl.btn = w.add ("group");
                  
                  var buttonarray = new Array ();
                  var key = {};
                  
                  
                  
                  for (var i = 0; i < fileArraymisc.length ; i++) {
                      
                      var delimeter = '/';
                      var string = String (fileArraymisc[i]);
                      var splitted = string.split(delimeter);
                      var thelength = ((splitted.length) - 1);
                      var bannername = splitted[thelength];
                      var delimeter = '.';
                      var splitted = bannername.split(delimeter);
                      var bannername = splitted[0];
                      
                      
                      var checkdelim = "%20";
                      var checkthat =  bannername.indexOf(checkdelim);
                      if (checkthat == "-1") {}
                      else {
                          
                      var delimeter = '%20';
                      var splitted = bannername.split(delimeter);
                      var counter = 0;
                      while (counter < splitted.length){
                      
                      if (counter == 0){
                      var bannername = (splitted[counter] + " ");    
                      }
                      else if (counter == ((splitted.length) - 1)){
                      var bannername = (bannername + splitted[counter]);
                      }
                      else{
                      var bannername = (bannername + splitted[counter] + " ");
                      }
                      var counter = (counter + 1);
                      }
                  
                      }
                      if (fileArraymisc.length > 6){
                          var divider = 2
                          }
                      else{
                          var divider = 1
                          }
                      
                      var remainder = (i % divider);
                      if (remainder == 0){  
                      w.btnPnl.btn = w.btnPnl.add ("group");
                      w.btnPnl.btn.alignChildren = "left"
                      } else {
                      //x is not a multiple of y
                      }
                      
                      key = (String (fileArraymisc[i]));
                      w[key] = w.btnPnl.btn.add("button", undefined ,bannername ); 
                      w[key].alignment = 'left';
                      buttonarray.push(w[key]);
                      
                      w.btnPnl.btn.addEventListener('click', button_pressed);
                      
                      function button_pressed (e)
                      {
                     if (e.target.type == "button")
                      $.writeln (e.target.text + " from panel " + e.target.parent.text);
                      w.close()
                      ////////////////////
                      var buttonname = e.target.text;
                      var checkdelim = " ";
                      var checkthat = buttonname.indexOf(checkdelim);
                      if (checkthat == "-1") {}
                      else {
                          
                      var delimeter = ' ';
                      var splitted = buttonname.split(delimeter);
                      var counter = 0;
                      while (counter < splitted.length){
                      
                      if (counter == 0){
                      var buttonname = (splitted[counter] + "%20");    
                      }
                      else if (counter == ((splitted.length) - 1)){
                      var buttonname = (buttonname + splitted[counter]);
                      }
                      else{
                      var buttonname = (buttonname + splitted[counter] + "%20");
                      }
                      var counter = (counter + 1);
                      }
                  }
                  
                      ///////////////////////
                      
                      bannerfile = new File (templatepath + "MISC/" + buttonname + '.png');
                      
                       }
                   }  
      
      
      
      
      
         
                       
       w.show();
      
      
      
      
      
      
      
      
        ///////////////////////////////////////////////////////////
       ////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////
       ////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////
       ////////////////////////////////////////////////////////////
      
       if (bannerfile == ""){}
       else{
      
      var targetDocumentName =  docRef.name;
      var tempfile = app.open (bannerfile);
      var sourceDocumentName = app.activeDocument.name;
      
      
      
      
      var thewidth = tempfile.width;
      var theheight  = tempfile.height;
      
      
      
      
      app.activeDocument = docRef;
      docRef.resizeImage (null, theheight, null, ResampleMethod.BICUBIC);
      
      
      if ((String(thewidth)) == (String(docRef.width))){
      var thelayer = docRef.activeLayer;
      copyLayers(sourceDocumentName, targetDocumentName );
      docRef.activeLayer = thelayer;
      }
      else {
      alert ("Document width Incorrect")   
      
      
      docRef.activeLayer.name = "main";
      var thelayer = docRef.activeLayer;
      docRef.resizeCanvas (thewidth, null, AnchorPosition.BOTTOMCENTER);
      copyLayers(sourceDocumentName, targetDocumentName );
      docRef.activeLayer = thelayer;
       }
        imageView ("FtOn");
       }
      
      
      
      
      
      
      
      
      
        ///////////////////////////////////////////////////////////
       ////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////
       ////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////
       ////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////
       ////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////
       ////////////////////////////////////////////////////////////
      
      
      
      
       function copyLayers(sourceDocumentName, targetDocumentName) {
        var sourceDoc = app.documents[sourceDocumentName];
        var targetDoc = app.documents[targetDocumentName];
        var sourceLayer, targetLayer, sourceGroup;
      
      
        if (app.activeDocument != sourceDoc) {
          app.activeDocument = sourceDoc;
        }
      
      
      var thelayer = tempfile.layers[0];
      var layersToCopy = thelayer.name;
      
      
        if ( typeof layersToCopy === 'string' ) {
          sourceGroup = tempfile.layers[0];
          targetLayer = sourceGroup.duplicate( targetDoc, ElementPlacement.PLACEATBEGINNING )
        }
        else if ( Object.prototype.toString.call( layersToCopy ) === '[object Array]' ) {
          for (var i = 0; i < layersToCopy.length; i++) {
            sourceLayer = sourceDoc.artLayers.getByName( layersToCopy[i] );
            targetLayer = sourceLayer.duplicate( targetDoc, ElementPlacement.PLACEATBEGINNING );
          }
        } 
      tempfile.close(SaveOptions.DONOTSAVECHANGES);
      }
      
      
      
      
      
      
      function imageView (view) {
          desc= new ActionDescriptor();
              ref = new ActionReference();
            if (view.length < 5){
               var viewID = charIDToTypeID( view );
            }else{
               var viewID = stringIDToTypeID( view );
            }
            ref.putEnumerated( charIDToTypeID( "Mn  " ), charIDToTypeID( "MnIt" ), viewID );
          desc.putReference( charIDToTypeID( "null" ), ref );
         executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
      };