7 Replies Latest reply on Dec 8, 2015 7:38 AM by Silly-V

    app.open(); Doubts

    Vinicius Baptista Level 2

      Hello guys

       

      I wrote a script to open files of a specific folder, simple. Just get some items from a folder and insert it to a listbox, but when there is no one document open in illustrator the script works, and when there is a document open not works.

      And I would like open multiple files using the multiselection function of listbox, but not work, only open the first item of selection.

       

      I think the problem is in app.open() function, maybe it's not a best way to do this.

      This is the script:

      var win = new Window('palette', 'Open documents');
      win.alignChildren = 'fill';
      
      
      var folder = Folder.selectDialog ("Select a folder to open your files.");
      
      
      var list = win.add('listbox', undefined, '', {multiselect: true, numberOfColumns: 1, showHeaders: true, columnTitles: ['Path']});
      list.preferredSize = [400, 200];
      
      
      var btnOpen = win.add('button', undefined, 'Open selected');
      
      
      var files = folder.getFiles ();
      
      
      for (i = 0; i < files.length; i++){
          var populateList = list.add ('item', files[i]);
      };
      
      
      btnOpen.onClick = function (){
      for (i = 0; i < list.selection.length; i++){
          var openSel = app.open( new File(list.selection[i]));
      //    alert('Testing loop');
      };    
      };
      
      
      win.show();
      

       

      I'm very grateful if anyone can help me, please!

       

      Thanks in advance, best regards,

      -Vinícius

        • 1. Re: app.open(); Doubts
          Silly-V Adobe Community Professional

          SHouldn't it be list.items instead of list.selection?

          • 2. Re: app.open(); Doubts
            Vinicius Baptista Level 2

            Hi Silly, thanks for your time and collaboration,

             

            Your suggestion doesn't work to me, look I tried to changed the btnOpen.onClick function using list.items:

             

            btnOpen.onClick = function (){  
            for (i = 0; i < list.items.length; i++){
                if (list.items[i].selected){
            //        alert(list.items[i]);
                var openSel = app.open( new File(list.items[i]));  
                };
            };      
            };  
            

             

            but, failed!

             

            I think the problem certainly is on app.open() function, if you can, please try test my code, when I hide the function app.open() and use whatever thing, the code works without a problem! Look:

             

            btnOpen.onClick = function (){  
            for (i = 0; i < list.items.length; i++){
                if (list.items[i].selected){
                    alert(list.items[i]);
            //    var openSel = app.open( new File(list.items[i]));  
                };
            };      
            };  
            

             

            Note: I done this script to filtering .pdf files in a list and open only the selected. But I'm bugging my brain with this simple task! Now I'm not so sure if it's possible.

             

            Please somebody help me! lol

            Thanks.

             

            Best regards

            -Vinícius

            • 3. Re: app.open(); Doubts
              Silly-V Adobe Community Professional

              EDIT: Forget everything I said!

               

              Your code was fine!

               

              Problem was that you are having a window of type "palette" and it doesn't use the Illustrator DOM anymore, so you have to send the function through BridgeTalk object.

               

              Try this. Notice that because bt functions are sending a whole script as a string, the filePath variable has to be encased inside either single or double quotes within the string to be of proper syntax.

               

              #target illustrator
              function test(){
                  function btFunc_openFile(filePath){
                      var bt = new BridgeTalk();
                      bt.target = "illustrator";
                      bt.body = "function myScript(){app.open(File('" + filePath + "'))};myScript();";
                      bt.send();
                  };
                  var win = new Window('palette', 'Open documents');  
                  win.alignChildren = 'fill';  
                  var folder = Folder.selectDialog ("Select a folder to open your files.");  
                  var list = win.add('listbox', undefined, '', {multiselect: true, numberOfColumns: 1, showHeaders: true, columnTitles: ['Path']});  
                  list.preferredSize = [400, 200];  
                  var btnOpen = win.add('button', undefined, 'Open selected');  
                  var files = folder.getFiles ("*.pdf");  
                  for (var i = 0; i < files.length; i++){  
                      var populateList = list.add ('item', decodeURI(files[i].fsName));  
                  };  
                  btnOpen.onClick = function (){  
                      for (var i = 0; i < list.selection.length; i++){  
                          var filePath = list.selection[i].text;
                          btFunc_openFile(filePath);
                      };      
                  };  
                  win.show();  
              }
              test();
              
              • 4. Re: app.open(); Doubts
                Vinicius Baptista Level 2

                Greetings Silly, you're absolutely and brilliantly right, if I simply replace the type of windows to dialog it works like a charm...

                 

                How could I not think about it? It makes perfect sense, working with the pallete windows is complex and I forgot this.

                Congratulations and thanks a lot for the quick help.

                 

                Best regards,

                -Vinícius

                • 5. Re: app.open(); Doubts
                  tpk1982 Level 4

                  Hi Vinicius & Silly,

                   

                  I tried the above coding to collect scripts from folder and run. But it is not working. Is it possible to do so?

                   

                  //#target illustrator 
                  function test(){ 
                      function btFunc_openFile(filePath){ 
                          var bt = new BridgeTalk(); 
                          bt.target = "estoolkit"; 
                          bt.body = "function myScript(){app.doScript(File('" + filePath + "'))};myScript();"; //changed open to doScript.. but stil not running
                          bt.send(); 
                      }; 
                      var win = new Window('palette', 'Open documents');   
                      win.alignChildren = 'fill';   
                      var folder = Folder.selectDialog ("Select a folder to open your files.");   
                      var list = win.add('listbox', undefined, '', {multiselect: true, numberOfColumns: 1, showHeaders: true, columnTitles: ['Path']});   
                      list.preferredSize = [600, 200];   
                      var btnOpen = win.add('button', undefined, 'Open selected');   
                      var files = folder.getFiles ("*.jsx");   
                      for (var i = 0; i < files.length; i++){   
                          var populateList = list.add ('item', decodeURI(files[i].fsName));   
                      };   
                      btnOpen.onClick = function (){   
                          for (var i = 0; i < list.selection.length; i++){   
                              var filePath = list.selection[i].text; 
                              btFunc_openFile(filePath); 
                          };       
                      };   
                      win.show();   
                  } 
                  test();
                  
                  

                   

                  Thanks,

                  Karthi

                  • 6. Re: app.open(); Doubts
                    Silly-V Adobe Community Professional

                    I am not sure about the estoolkit methods, because I've never used them yet and don't know what documentation is on that.

                    However, if you plant to run the scripts in Illustrator, you can target illustrator and use some other commands such as file.execute() or read the file as tex and eval();

                    I think it's possible, because this is how I've done my script panels, but it's probably a bit of work and I'm not sure if this would even work- and like I said, I haven't messed with ESTK running scripts other than from Applescript controlling ESTK from the Script Editor, in a clumsy workaround...

                     

                    I found this thread where they use shell scripts to control ESTK: Create a build script for Extend Script

                     

                    And here's my own method from my Script Panel 1 experiment:

                    Adobe-Illustrator/ScriptPanel.jsx at master · Silly-V/Adobe-Illustrator · GitHub

                    This function is extremely useful to me and it's the only thing I ever use, big thanks to Robert_Stucky for the encoding function which I would not have been able to figure out in a million years.

                     

                    function bridgeTalkEncode( txt ) { 
                        txt = encodeURIComponent( txt ); 
                        txt = txt.replace( /\r/, "%0d" ); 
                        txt = txt.replace( /\n/, "%0a" ); 
                        txt = txt.replace( /\\/, "%5c" ); 
                        txt = txt.replace(/'/g, "%27"); 
                        return txt.replace(/"/g, "%22"); 
                    } 
                    
                    function runScriptFromFile(file){
                        var sf = file;
                    
                        if(!(file instanceof File)){
                            sf = File(file);
                        }
                        if(!sf.exists){
                            alert("Sorry, it appears that this script file cannot be located at '"+decodeURI(sf.toString())+"'");
                            return;
                        }
                        sf.open('r');
                        var scriptString = sf.read().replace("#target illustrator",'');
                        sf.close();
                    
                    
                        // Thanks to: https://forums.adobe.com/thread/287506?tstart=0
                        var pathToScript = "var ScriptPanel_MyLocation = '"+sf.fsName+"';";
                        var script = "var scp ='" + bridgeTalkEncode(pathToScript+"\r"+scriptString) + "'";
                    
                    
                        script += ";\nvar scpDecoded = decodeURI( scp );\n";
                        script += "eval( scpDecoded );";
                    
                    
                        var bt = new BridgeTalk();
                        bt.target = 'illustrator';
                        bt.body = script;
                        bt.onError = function(errObj){
                            alert(errObj.body);
                        }
                        bt.send();
                    }
                    
                    
                    • 7. Re: app.open(); Doubts
                      Silly-V Adobe Community Professional

                      Of course, if you actually want to run several of these scripts at once, I don't know how that would work out as BT scripts are asynchronous, I think, so they may be sent at the same time and no work. Need to experiment.