3 Replies Latest reply: Mar 3, 2011 9:32 AM by Muppet Mark-QAl63s RSS

    Need Help Converting Appscript to Javascript

    hoovdan Community Member

      I tried my best converting Larry G. Schneider's "Delete Invisible Layers" script to Javascript, but to no avail.  Little help for us Windows folks? 

       

      Here's a direct quote:

       

      Here's a script that I have to delete hidden layers in a file. It could be reworked to target page items. Remember you can only delete something if it is visible.

       

      --get a sourceFolder that holds the files to process
      set sourceFolder to (choose folder with prompt "Choose a folder with files to process:") as text

       

      -- get a list of files of the files to process in the sourceFolder
      tell application "Finder" to set workingFiles to (every file of folder sourceFolder) as alias list

       


      repeat with aFile in workingFiles
          tell application "Adobe Illustrator"
              open aFile
             
              set currentFile to front document
             
              tell application "Adobe Illustrator"
                  set myLayers to every layer of currentFile
                 
                  repeat with i from 1 to count of items in myLayers
                     
                      set myLayer to item i of myLayers
                      set locked of myLayer to false
                     
                      if visible of myLayer is true then
                          set visible of myLayer to false
                      else if visible of myLayer is false then
                          set visible of myLayer to true
                      end if
                     
                  end repeat
                 
                  delete (every layer of currentFile whose visible is true)
                 
              end tell
             
              tell application "Adobe Illustrator"
                 
                  set visible of (every layer of currentFile) to true
                 
              end tell
             
              close currentFile with saving
             
          end tell
      end repeat

       

       

      Thank you!

        • 1. Re: Need Help Converting Appscript to Javascript
          CarlosCanto Community Member

          not exactly a translation

           

          #target Illustrator
          
          // var script.description = deletes hidden layers
          //  var script.parent = CarlosCanto; // 3/2/11
          //  var script.elegant = false;
          
          var idoc = app.activeDocument;
          
          for (i = idoc.layers.length-1 ; i>=0 ; i--)
               {
                    var ilayer = idoc.layers[i];
                    if (ilayer.visible == false) 
                         {
                              ilayer.visible = true;
                              ilayer.locked = false;
                              ilayer.remove();
                         }
                    }
          

          • 3. Re: Need Help Converting Appscript to Javascript
            Muppet Mark-QAl63s Community Member

            I only ran a basic test with this so do check it first… It should look for all files in sub folders and all sub layers too?

             

            #target illustrator
            
            var df = Folder(Folder.desktop);
            
            var topLevel = Folder.selectDialog('Please choose your Top Level Folder…', df);
            
            if (topLevel != null) {
            
                 var fileList = Array();
            
                 fileListRecursive(topLevel.fsName, /\.ai$/i);
            
                 if (fileList.length > 0) {
                      
                      removeInvisLayers();
                      
                 } else {
                      
                      alert('This Folder or sub folders contained NO Illustrator AI files?');
                      
                 }
            }
             
            function removeInvisLayers() {
                 
                 var uIL = app.userInteractionLevel;     
                 app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
            
                 for (var i = 0; i < fileList.length; i++) {
                 
                      app.open(fileList[i]);
                 
                      var docRef = app.activeDocument;
              
                      recurseLayers(docRef.layers);
                      
                      docRef.close(SaveOptions.SAVECHANGES);
                      
                 }
            
                 app.userInteractionLevel = uIL;
                 
            };
            
            function recurseLayers(objArray) {
            
                 for (var i = objArray.length-1 ; i >= 0; i--) {
                      
                      if (objArray[i].layers.length > 0) {
                           recurseLayers(objArray[i].layers)
                      }
                      
                      if (objArray[i].locked) objArray[i].locked = false;
                      
                      if (!objArray[i].visible) objArray[i].visible = true, objArray[i].remove();
                           
                 }
            };
            
            function fileListRecursive(f, exp) {
                 
                 var temp = Folder(f).getFiles();
                  
                 for (var i = 0; i < temp.length; i++) {
            
                      if (temp[i] instanceof File && RegExp(exp).test(temp[i].fsName))
                      fileList.push(temp[i]);
            
                      if (temp[i] instanceof Folder)
                      fileListRecursive(temp[i].fsName, exp);
                      
                 } 
            };