3 Replies Latest reply on Aug 1, 2017 12:43 AM by Loic.Aigon

    Script pallete

    lf.corullon Level 2

      Hello, there.

      I'm trying to populate a little pallete window with script files in a folder.

      I'm able to create the window, automaticaly create the buttons for each script file in the folder. But I can't set a function to each button. What am I doing wrong?

       

      #target indesign
      #targetengine "Menu de Scripts"
      
      
      var myFolder = Folder("~/Downloads/Nova pasta");
      if (myFolder.exists) {
          var myFiles = new Array;
          GetSubFolders(myFolder);
          if (myFiles.length > 0) {
              myFiles.sort();
              var myScriptList = myFiles.join(";");
              var scriptFolder = RegExp((String(Folder.decode(myFolder))+"/"),"gi");
              var scriptFile = String(myScriptList).replace(scriptFolder,"");
              var scriptFiles = String(scriptFile).replace(/\.jsx/g,"");
              var myScriptsList = scriptFiles.split(";");
              
              var myMenu = new Window ("palette","Script by LFCorullón", undefined, {resizeable: false});
              var myG = myMenu.add ("group");
              myG.orientation = "column";
              
              for (var ) {
              for (var s=0; s<myScriptsList.length; s++) {
                  var myFile = File(myFolder+"/"+myScriptsList[s]+".jsx");
                  var myBtn = myG.add ("button", [0,0,150,25], myScriptsList[s]);
                  myBtn.onClick = function() { app.doScript(myFile); }
                  alert(myFile);
                  }
              }
              myMenu.show();
              }
          }
      
      
      // FUNÇÕES ======================================================================================
      function GetSubFolders(theFolder) {
          var myFileList = theFolder.getFiles();
          for (var q = 0; q < myFileList.length; q++) {
              var myFile = myFileList[q];
              if (myFile instanceof Folder){
                  GetSubFolders(myFile);
                  }
              else if (myFile instanceof File && myFile.name.match(/\.js.+$/i)) {
                  myFiles.push(File.decode(myFile));
                  }
              }
          }
      
        • 1. Re: Script pallete
          Loic.Aigon Adobe Community Professional

          You have a reference issue as you redeclare the same variable over and over. A solution is to have separate references while storing buttons in an array:

           

          for (var s=0; s<myScriptsList.length; s++) {  
                      var myFile = File(myFolder+"/"+myScriptsList[s]+".jsx");  
          btns.push ( myG.add ("button", [0,0,150,25], myScriptsList[s]) );
                        btns[btns.length-1].file = myFile;
                      btns[btns.length-1].onClick = function() { 
          app.doScript(this.file);
          }  
                      }  
                  }  
          
          • 2. Re: Script pallete
            lf.corullon Level 2

            Thank you so much!!! Great!

            I didn't understand your fragment yet. But it works.

             

            You created a new array (btns) and, using the for loop, "push" each button from the length to the zero. Right?

            Then, set the property file for each button to its jsx file.

            Then, set the onClick function pointing to the property you declare in the past step? (this.file)


            Did I understand well?

            1 person found this helpful
            • 3. Re: Script pallete
              Loic.Aigon Adobe Community Professional

              Yes the idea was to establish a clear separation between buttons reference. The array helps in that goal. Adding a file property to the button instance is convenient because the on click listener is generic and will retrieve the File instance for a specific button.