18 Replies Latest reply on Jul 27, 2011 2:56 AM by Mac_06

    Making a script to do a mundane task

    Nosc

      Hi,

       

      I need a script, and indesign doesnt use actions, and i cant write scripts, i understand alot but this is getting tricky.

       

      What is need to do is convert every two weeks 1800 files in indesign!

       

      Doing every time the same task, it goes like this,

       

      Open "specific" file

       

      Select Masterpage

       

      Select All on masterpage

       

      Hit delete, deleting all on master page

       

      Export file as pdf to same folder

       

      Save

       

      Close

       

      So if i had a script where i can select a map with all the files and then do the task as a batch it would save me a lot of time.

       

      I have to do this 1800 times every two weeks

       

      Please can somebody help

       

      Thanks for reading

        • 1. Re: Making a script to do a mundane task
          Mac_06 Level 2

          You could try below code (din't check but should work)

           

          var filePaths = "/*give your indd folder path*/"
          var InddFile = filePaths.getFiles ("*.indd");
          if(File(InddFile.exists)
          {
                  for(var i=0; i<InddFile.length; i++)
                  {
                          var curDoc = InddFile[i]
                          curDoc.open ('w');
                          for(var j=0; j<curDoc.masterSpreads.length; j++)
                          {
                                  var k = curDoc.masterSpreads[j].allPageItems.length
                                  while( k--)
                                  {
                                          curDoc.masterSpreads[j].allPageItems[k].remove ()
                                      }
                              }               
                          curDoc.exportFile(ExportFormat.pdfType, File(filePaths+"/ExportToPDF_"+i+".pdf"), false);
                          curDoc.close (SaveOptions.yes)
                      }
              }
          


          Mac

          1 person found this helpful
          • 2. Re: Making a script to do a mundane task
            Nosc Level 1

            Thanks,

             

            Ill give it a try,

             

            I will let you know if it works,

             

            Thanks again its much appriciated,

             

            Best

             

            Henk

            • 3. Re: Making a script to do a mundane task
              Nosc Level 1

              It says "An identifier can't go after this identifier"

               

              if i paste the script like this, what should i do?


              var filePaths = "/Users/Instore2/Desktop/Test/"
              var InddFile = filePaths.getFiles ("*.indd");
              if(File(InddFile.exists)
              {
                      for(var i=0; i<InddFile.length; i++)
                      {
                              var curDoc = InddFile[i]
                              curDoc.open ('w');
                              for(var j=0; j<curDoc.masterSpreads.length; j++)
                              {
                                      var k = curDoc.masterSpreads[j].allPageItems.length
                                      while( k--)
                                      {
                                              curDoc.masterSpreads[j].allPageItems[k].remove ()
                                          }
                                  }              
                              curDoc.exportFile(ExportFormat.pdfType, File(filePaths+"/ExportToPDF_"+i+".pdf"), false);
                              curDoc.close (SaveOptions.yes)
                          }
                  }



              • 4. Re: Making a script to do a mundane task
                John Hawkinson Level 5

                Henk, you should be specific about the line number that an error is reported on.

                 

                Looking at Mac's script, though, this seems problematic:

                 

                if(File(InddFile.exists)
                {

                Both because the parens do not match and because InddFile is an array of File objects, and arrays do not have a .exists property.

                Honestly I'm not sure what the intention of the whole if() line is, but you can probably replace it with:

                 

                if (InddFile[0].exists) {
                
                1 person found this helpful
                • 5. Re: Making a script to do a mundane task
                  Mac_06 Level 2
                  var filePaths = File("/*path to folder*/")
                  var InddFile = filePaths.getFiles ("*.indd");
                  if(File(InddFile.exists))
                  {
                          for(var i=0; i<InddFile.length; i++)
                          {
                                  var curDoc = InddFile[i]
                                  var DocName = String(curDoc).split ("/").pop().split(".")[0]
                                  var newcurDoc= app.open(File(curDoc));
                                  for(var j=0; j<newcurDoc.masterSpreads.length; j++)
                                  {
                                          var k = newcurDoc.masterSpreads[j].allPageItems.length
                                          while( k--)
                                          {
                                                  newcurDoc.masterSpreads[j].allPageItems[k].remove ()
                                              }
                                      }               
                                  newcurDoc.exportFile(ExportFormat.pdfType, File(filePaths+DocName+".pdf"), false);
                                  newcurDoc.close (SaveOptions.yes)
                              }
                      }
                  

                   

                  Use above code. Should work perfectly

                   

                  Mac

                  • 6. Re: Making a script to do a mundane task
                    Nosc Level 1

                    hi Mac,

                     

                    I get this if i paste you latest post in a new applescript window,

                     

                    Picture 1.png

                    • 7. Re: Making a script to do a mundane task
                      Nosc Level 1

                      ps i am using 10.5.8

                       

                      CS4

                      • 8. Re: Making a script to do a mundane task
                        [Jongware] Most Valuable Participant

                        Nosc wrote:

                         

                        I get this if i paste you latest post in a new applescript window ..

                         

                        A-ha!

                         

                        (It's not Applescript, it's Javascript. Don't fret over that, it takes some minimal experience to distinguish these. Paste it into Adobe's Extendscript Toolkit Editor (ESTK) -- your Quickfind ought to be able to locate this for you. Then save under a good name in your scripts folder -- see http://indesignsecrets.com/how-to-install-scripts-in-indesign.php for the how-and-why's.)

                        • 9. Re: Making a script to do a mundane task
                          Nosc Level 1

                          Mac06

                           

                          you are awesome,

                           

                          you just saved me a hell of work

                           

                          Much much appriciated and thank you

                          • 10. Re: Making a script to do a mundane task
                            Marijan Tompa [tomaxxi] Level 4

                            Mac,

                             

                            you could replace complete for loop with just single line:

                             

                            newcurDoc.masterSpreads.everyItem().pageItems.everyItem().remove();
                            

                             

                            It's much faster than looping through master page items.

                             

                            Hope that helps.

                             

                            --

                            Marijan (tomaxxi)

                            http://tomaxxi.com

                            • 11. Re: Making a script to do a mundane task
                              Mac_06 Level 2

                              Thanks Marijan to get noticed but while using as your fragment I found some page items were not removed. don't know why?

                               

                              Not sure about Mac since I am using Windows.

                               

                              Mac

                              • 12. Re: Making a script to do a mundane task
                                Mac_06 Level 2

                                Nosc

                                 

                                If you have achieved your goal then please mark this thread as answered.

                                 

                                Mac

                                • 13. Re: Making a script to do a mundane task
                                  Nosc Level 1

                                  Hi Mac,

                                   

                                  I need your advice, you helped me out before and i hope you can again.

                                   

                                  The script works perfectly, now i have a question.

                                   

                                  I have to do some extra stuff, thus there have to be changes in the scipt.

                                   

                                  open the document,

                                   

                                  Select masterpage, select frame, change fill color of frame to red (cmyk=0/100/90/15), change size to W=297mm, H=150mm, X=0mm, Y=270mm, Referencepoint is top left corner.

                                   

                                  Go out of masterpage to the first page, select all text and change the font to Arial, and the font color to blue (cmyk=100/70/0/30)

                                   

                                  And the most tricky part, change all points to comma's.

                                   

                                  Export as pdf,

                                   

                                  save

                                   

                                  And close

                                   

                                  This is what i want for now, if you can help me maybe i can alter it my self to a further state, i hope that you can help me,

                                   

                                  thanks

                                   

                                  Nosc

                                  • 14. Re: Making a script to do a mundane task
                                    Mac_06 Level 2
                                    var filePaths = File("/*path to folder*/")
                                    var InddFile = filePaths.getFiles ("*.indd");
                                    if(File(InddFile.exists))
                                    {
                                            for(var i=0; i<InddFile.length; i++)
                                            {
                                                    var curDoc = InddFile[i]
                                                    var DocName = String(curDoc).split ("/").pop().split(".")[0]
                                                    var newcurDoc= app.open(File(curDoc));
                                                    newcurDoc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.millimeters;
                                                    newcurDoc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.millimeters;
                                    
                                                    for(var j=0; j<newcurDoc.masterSpreads.length; j++)
                                                    {
                                                            var k = newcurDoc.masterSpreads[j].allPageItems.length
                                                            while( k--)
                                                            {
                                                                    change_color_and_size(newcurDoc, newcurDoc.masterSpreads[j].allPageItems[k])
                                                                }
                                                        }
                                                    with(newcurDoc.pages.everyItem().textFrames.everyItem().paragraphs.everyItem()){
                                                    appliedFont = app.fonts.item("Arial");
                                                    fillColor = getSwatch(newcurDoc, "newBlue", [100, 70, 0, 30])
                                                    }
                                                    app.findTextPreferences = app.changeTextPreferences = null;
                                                    app.findChangeTextOptions.wholeWord = false;                
                                                    app.findTextPreferences.findWhat=".";
                                                    app.changeTextPreferences.changeTo=","
                                                    newcurDoc.changeText();
                                                    app.findTextPreferences = app.changeTextPreferences = null;
                                                    
                                                    newcurDoc.exportFile(ExportFormat.pdfType, File(filePaths+DocName+".pdf"), false);
                                                    newcurDoc.close (SaveOptions.yes)
                                                }
                                        }
                                    function change_color_and_size(curDoc, pagItem)
                                    {
                                        var XPos = 0, YPos = 270;    
                                        //calling the function          
                                         pagItem.fillColor = getSwatch(curDoc, "newRed", [0, 100, 90, 15]);
                                         
                                         pagItem.geometricBounds = [YPos, XPos, YPos+150, XPos+297];
                                        }
                                    function getSwatch(curDoc, swatchName, swatchValue)
                                    {   
                                        //Check and if not found create new swatch
                                         if (curDoc.swatches.item(swatchName) != null) 
                                         var swatch = curDoc.swatches.item(swatchName);
                                         else
                                          var swatch = curDoc.colors.add({ name:swatchName, model:ColorModel.PROCESS, space:ColorSpace.CMYK, colorValue:swatchValue });
                                          return swatch;
                                        }
                                    

                                     

                                    check this script should work for you

                                     

                                    Mac

                                    • 15. Re: Making a script to do a mundane task
                                      Nosc Level 1

                                      Hi mac,

                                       

                                      Thanks i am trying to work with the script, i am having trouble with the changing the font, it is now set to arial, and i have to change that to Helevtica Neue 95 Black, i have tried all the font family names, but i does find the font, its frusterating.

                                       

                                      Picture 2.png

                                      • 16. Re: Making a script to do a mundane task
                                        Mac_06 Level 2
                                        appliedFont = app.fonts.item("Arial");                
                                        

                                         

                                        Replace with

                                         

                                        appliedFont = app.fonts.item("Helvetica Neue");
                                                      fontStyle = "95 Black"
                                        

                                         

                                         

                                        Mac

                                        • 17. Re: Making a script to do a mundane task
                                          Nosc Level 1

                                          Mac,

                                           

                                          you are a genius

                                           

                                          I am going to ask you more questions

                                           

                                          I hope i can repay you some way for this.

                                           

                                          My next question is, i asked to change point in to comma's but now i want to exclude some points. At this point the script makes of all the point commas, but i want to exlude the comma after this sentence incl. so now it makes incl, an this has to be a point only for point after the word "incl"

                                           

                                          thanks for the help

                                          • 18. Re: Making a script to do a mundane task
                                            Mac_06 Level 2
                                                            app.findTextPreferences = app.changeTextPreferences = null;
                                                            app.findChangeTextOptions.wholeWord = false;                
                                                            app.findTextPreferences.findWhat=".";
                                                            app.changeTextPreferences.changeTo=","
                                                            newcurDoc.changeText();
                                                            app.findTextPreferences = app.changeTextPreferences = null
                                            


                                            Replace with

                                             

                                            app.findGrepPreferences = app.changeGrepPreferences = null;
                                            app.findGrepPreferences.findWhat = "(?<!Incl)\\.";
                                            app.changeGrepPreferences.changeTo = ",";
                                            app.activeDocument.changeGrep()
                                            app.findGrepPreferences = app.changeGrepPreferences = null;
                                            

                                             

                                            Mac