4 Replies Latest reply on Aug 8, 2017 8:46 AM by ca_saurabh02

    Batch Remove Guides If they Exists

    ca_saurabh02

      Hello guys,

      I am using Illustrator CC 2017 and I have a bunch of files which may or may not have guides.

      After going through various threads, I came up with a script to remove Guides automatically if they exist but somehow it is getting stuck while saving the file.

       

      Here is the javascript

       

      sourceFolder = Folder.selectDialog();

      // filesToProcess = sourceFolder.getFiles("*.ai");

      var TypeList = prompt("Enter file types to act on","ai,eps,pdf","File Types");  

      var reg = new RegExp("\.(" + TypeList.replace(/,/g, '|') + ")$", 'i');  

      var filesToProcess = sourceFolder.getFiles(reg);

       

      // Increment through the files

      for (i=0; i < filesToProcess.length; i++) {

       

      app.open(filesToProcess[i]);

      var myDoc=app.activeDocument;

       

       

      // Remove Guides

      for(var i = myDoc.pathItems.length-1; i >= 0; i--) {

      var p = myDoc.pathItems[i];

      if (p.guides == true) {

      p.remove();

      }

      }

       

      // Save

      var mySaveOptions = new IllustratorSaveOptions();

      mySaveOptions.pdfCompatible = true;

      myDoc.saveAs(filesToProcess[i],mySaveOptions);

       

      myDoc.close();

      }

       

      here is the link to the test Illustrator file  https://drive.google.com/open?id=0B8FDkTEOTC4POUNpLXFtSUl0YzQ

       

      it would be really great if someone could help me fix it.

      Thanks

        • 1. Re: Batch Remove Guides If they Exists
          pixxxel schubser Level 5

          ca_saurabh02  schrieb

           

          Hello guys,

          I am using Illustrator CC 2017 and I have a bunch of files which may or may not have guides.

          After going through various threads, I came up with a script to remove Guides automatically if they exist but somehow it is getting stuck while saving the file.

           

          Here is the javascript

           

          ...

           

          // Remove Guides

          for(var i = myDoc.pathItems.length-1; i >= 0; i--) {

          var p = myDoc.pathItems[i];

          if (p.guides == true) {

          p.remove();

          }

          }

           

          ...

          Instead of loop through all items - better use the following line of code:

          app.executeMenuCommand ('clearguide')
          

           

          Much more better performance.

          Have fun

           

          • 2. Re: Batch Remove Guides If they Exists
            ca_saurabh02 Level 1

            I could have used that but in the folder, there are files that may or may not have the guides so have to loop through them.

            The Error I am getting while Saving is in this Line myDoc.saveAs(filesToProcess[i],mySaveOptions);

            The error is "Illegal argument - argument 1 - Required value is missing"

            • 3. Re: Batch Remove Guides If they Exists
              pixxxel schubser Level 5

              ca_saurabh02  schrieb

               

              I could have used that but in the folder, there are files that may or may not have the guides so have to loop through them.

               

              No. That's the best reason for using the executeMenuCommand

              If guides exists they will delete. If no guides exists - ist's not a problem for the Command.

               

               

              And your code is not my kind of coding.

               

              But with your syntax you have to define the file name and the place to save. And if the name is the same as your opened file - the existing file wil be overwritten!!!

               

              With your syntax you can do something like this:

              sourceFolder = Folder.selectDialog();
              
              // filesToProcess = sourceFolder.getFiles("*.ai");
              var TypeList = prompt("Enter file types to act on","ai,eps,pdf","File Types");
              var reg = new RegExp("\.(" + TypeList.replace(/,/g, '|') + ")$", 'i');
              var filesToProcess = sourceFolder.getFiles(reg);
              
              
              // Increment through the files
              for (i=0; i < filesToProcess.length; i++) {
              app.open(filesToProcess[i]);
              var myDoc=app.activeDocument;
              
              // Remove Guides
              app.executeMenuCommand ('clearguide');
              /*
              for(var i = myDoc.pathItems.length-1; i >= 0; i--) {
              var p = myDoc.pathItems[i];
              if (p.guides == true) {
              p.remove();
              }
              }
              */
              
              
              var saveFile = File (filesToProcess[i].path + "/" + filesToProcess[i].name.replace(reg, "_withoutGuides.ai") );
              // Save
              var mySaveOptions = new IllustratorSaveOptions();
              mySaveOptions.pdfCompatible = true;
              myDoc.saveAs(saveFile,mySaveOptions);
              myDoc.close();
              }
              

               

              The new saved file will be saved as ai-file with the same name and in the same folder as your original file but with the suffix "_withoutGuides"

               

              Have fun

               

              1 person found this helpful
              • 4. Re: Batch Remove Guides If they Exists
                ca_saurabh02 Level 1

                Thank you so much, it worked as I wanted. I am a noob with scripting and have been trying for the whole day.

                Thanks again.

                Regards