6 Replies Latest reply on Sep 15, 2017 5:52 AM by lf.corullon

    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.

              • 4. Re: Script pallete
                lf.corullon Level 2

                Just to share...

                The idea is provide ever updated scripts to the users in a newsroom, avoiding having to copy the jsx files to the user desktop.

                So, I create this palette to call the jsx files from the server.

                 

                #target indesign
                #targetengine "Menu de Scripts"
                
                
                //DESCRIPTION: Descrição do script (tooltip, helptip)
                //=============================================================
                //  Script by Luis Felipe Corullón
                //  Contato: lf@corullon.com.br
                //  Site: http://lf.corullon.com.br
                //=============================================================
                
                
                
                
                var myFolder = Folder("server folder where the scripts are");
                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";
                        myMenu.margins = [5,5,5,5];
                        myG.spacing = 5;
                        var minimizar = myG.add ("button", [0,0,200,25], "*** Minimize ***");
                        minimizar.onClick = function() { if (myMenu.size.height == size) { myMenu.size.height = 35 } else { myMenu.size.height = size } }
                        
                        var btns = [];
                        for (var s=0; s<myScriptsList.length; s++) {
                            var myFile = File(myFolder+"/"+myScriptsList[s]+".jsx");
                            btns.push ( myG.add ("button", [0,0,200,25], myScriptsList[s]) );
                            btns[btns.length-1].file = myFile;
                            btns[btns.length-1].onClick = function() { app.doScript(this.file); }
                            }
                        myMenu.show();
                        var size = myMenu.size.height;
                        }
                    else {
                        alert("There is no script files in the specified folder.", "Script by LFCorullón");
                        }
                    }
                else {
                    alert("The specified folder doesn't exists.", "Script by LFCorullón");
                    }
                
                
                
                
                // 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));
                            }
                        }
                    }
                

                 

                Thanks for Loic.Aigon for help me in the buttons.

                • 5. Re: Script pallete
                  Loic.Aigon Adobe Community Professional

                  For what it's worth, you can use a shortcut to the script inside the script panel. So you could have one unique location and have all your users use the file through the shortcut. In some occasions, that may not work if the script needs dependencies that you will try to reach through $.fileName for example.

                  Apart from that, I had one client on mac who was able to dispatch a script update to several dozens of colleagues through an app. He could monitor everyone's computer and put a file to some locations. I can't remember the app name right now but just in case.

                  • 6. Re: Script pallete
                    lf.corullon Level 2

                    Yes. Are you referring to the shortcut in the palette, right? If I just set

                    a shortcut to the script file (using the InDesign shortcut dialog window),

                    this didn't work. Everytime I start InDesign, I don't know why, the

                    shortcuts are gone. As I understand, InDesign sets different ID for each

                    script file everytime it starts.

                     

                    Using the shortcut key parameter in the palette I wrote, I just be able to

                    use the combinations with Alt key, right? I don't find any javascript

                    reference to use other key combinations.

                     

                    I have an app called SyncBack (that have free and paid versions) that could

                    be used to sync those files. But, the IT department on my clients doesn't

                    depends on me so, it's easier to use this palette calling the files from

                    the server (where I can keep all of them updated) instead of involving my

                    boss to ask the IT coordinator of each client of us, etc...

                     

                    Thanks.