5 Replies Latest reply on Jan 12, 2012 6:56 AM by notannsteer

    Script to print just the story within InDesign

    notannsteer Level 1

      Does anyone know of  or could help me  with  a script that would just print or export to pdf by story from InDesing or failing that- to make individual files out of the stories?

      I have a client who insists on taking  my pdf file and breaking it apart [manually] to regroup and resort  by various  other groupings. The stories are  data merge files generated from CSV files and  pertain to grants. These grants have names,  types of industries and regions. I can sort by  region as each region has a different master page. And I can  generate a  linked table of contents that sorts by industry and by name of grant. But he really wants to make separate files for each grant and populate various directories  with  the individual pdf files.

      I need to be able to break up my InDesign file to  create a separate pdf file for each grant. Each grant is a story within my document.Some grants have 2 pages while others have 4 - so it would be  very  tricky to do manually. If I could get separate files for each grant or print by story, everyone would be happy.

       

      Thanks

      Ann

        • 1. Re: Script to print just the story within InDesign
          [Jongware] Most Valuable Participant

          Hi Not Ann,

           

          So each of your grants is one continuously threaded story, and each new grant starts a new thread? If so, it was an interesting challenge. Here is a Javascript that exports each "story" to a PDF of its own -- it follows the threads but does not output the same page twice. (In case you cross-moved story threads around.) I tried to handle a couple of possible pitfalls, such as "one should not export Master page stuff", and it works on my meticulously constructed test file ... but please do check if you got all of your grants, every time you use it. I'd hate to see someone loose his money cause of a script of mine.

           

          if (app.documents.length > 0)
          {
                    myFolder = Folder.selectDialog ("Choose a Folder");
                    if (myFolder != null)
                    {
                              var counter = 1;
            
                              storylist = app.activeDocument.stories;
                    //          alert (storylist.length+" stories to go");
                              for (s=0; s<storylist.length; s++)
                              {
                                        p = getPageFor (storylist[s].textContainers[0]);
                                        if (p == null || p.parent.constructor.name == "MasterSpread")
                                                  continue;
                                        exportStory (myFolder, "Story "+(counter), storylist[s]);
                                        counter++;
                              }
                    }
          }
          
          
          function exportStory (outputFolder, pdfName, aStory)
          {
                    var i,j, pg, pgnum;
                    var pglist = [];
          
          
                    for (i=0; i<aStory.textContainers.length; i++)
                    {
                              pg = getPageFor (aStory.textContainers[i]);
                              if (pg)
                              {
                                        pgnum = pg.name;
                                        for (j=0; j<pglist.length; j++)
                                        {
                                                  if (pglist[j] == pgnum)
                                                            break;
                                        }
                                        if (j== pglist.length)
                                                  pglist.push (pgnum);
                              }
                    }
          //          alert (pglist.join(", "));
                    app.pdfExportPreferences.pageRange = pglist.join(",");
                    app.activeDocument.exportFile(ExportFormat.PDF_TYPE, File(new File(outputFolder+"/"+pdfName+".pdf")));
            
          }
          
          
          function getPageFor (element)
          {
                    while (!(element instanceof Page || element instanceof Application))
                              if (element instanceof Character)
                                        element = element.parentTextFrames[0].parent;
                              else
                                        element = element.parent;
                    if (element instanceof Page)
                              return element;
                    return null;
          }
          
          • 2. Re: Script to print just the story within InDesign
            notannsteer Level 1

            W w- you are great - thank you so much - it works like a charm - I have a file with 96 stories - it worked! no missing grants - they're all there! Thank you! Waht a great resource this is!

            • 3. Re: Script to print just the story within InDesign
              notannsteer Level 1

              Hi Jongware,

              Thakn you for the script - we have modified it a bit to do what I want- namely  -pritn teh story as a pdf and name it with the first 3 letters of the fiel name [which is hte region] and then teh name  of the grant.  My husband has helped me withto modify it and it worked  beautifully unitl I  tried it on  CS5.5 where it stopped working.

              Could I ask you to look at it and maybe see what teh problem is in CS5.%?

              Thanks

              here it is

               

              #target indesign-7.5

              if (app.documents.length > 0)

              {

                        myFolder = Folder.selectDialog ("Choose a Folder");

                        if (myFolder != null)

                        {

                                  var counter = 1;

                  var myStoryText = new String;

                  var myGrantName = new String;

                  var myHeadOfficeLoc = new String;

                  var myFileName = new String;

                                      myDocument =  app.documents.item(0);

                                      storylist = app.activeDocument.stories;

                                      alert (storylist.length+" stories to go");

                                  for (s=0; s<storylist.length; s++)

                                  {

              //                                alert(storylist[s].itemByName(0));

                                            p = getPageFor (storylist[s].textContainers[0]);

                                            if (p == null || p.parent.constructor.name == "MasterSpread")

                                                      continue;

                     myStory = myDocument.stories.item(s);

                     myStoryText = myStory.contents;

                     if(myStoryText.length < 30) continue;

                     alert("Story Text Length = " + myStoryText.length + ", Story Text = " + myStoryText);

                     myGrantNameLoc = myStoryText.indexOf("NAME OF THE GRANT") + 1;

                     myRegionsLoc = myStoryText.indexOf("\r",myGrantNameLoc + 18);

                     myGrantName = myStoryText.substring(myGrantNameLoc+18, myRegionsLoc-1);

                     myHeadOfficeLoc = myStoryText.indexOf("HEAD OFFICE LOCATION") + 22;

                     myPurposeLoc = myStoryText.indexOf("PURPOSE OF THIS PROGRAM");

                     myRegionAbbrev = myStoryText.substring(myHeadOfficeLoc, myPurposeLoc-2);

                     myRegionAbbrev = app.activeDocument.name.substring(0,3);

                     myFileName = "";

                     myFileName = "\(" + myRegionAbbrev.toUpperCase() + "\)" + myGrantName;

                    

              //      myFileName = myFileName.concat("\(", myRegionAbbrev.toUpperCase(), "\)", myGrantName);

                                            exportStory (myFolder, myFileName, storylist[s]);

                                            counter++;

                                  }

                        }

                 alert("There were exactly " + (counter - 1) + " PDF files created from file " + app.activeDocument.name);

              }

               

               

              function exportStory (outputFolder, pdfName, aStory)

              {

                        var i,j, pg, pgnum;

                        var pglist = [];

               

               

                        for (i=0; i<aStory.textContainers.length; i++)

                        {

                                  pg = getPageFor (aStory.textContainers[i]);

                                  if (pg)

                                  {

                                            pgnum = pg.name;

                                            for (j=0; j<pglist.length; j++)

                                            {

                                                      if (pglist[j] == pgnum)

                                                                break;

                                            }

                                            if (j== pglist.length)

                                                      pglist.push (pgnum);

                                  }

                        }

              //          alert (pglist.join(", "));

                        app.pdfExportPreferences.pageRange = pglist.join(",");

                 app.pdfExportPreferences.includeBookmarks = true;

                 app.pdfExportPreferences.includeHyperlinks = true;

                 app.pdfExportPreferences.viewPDF = false;

                       app.activeDocument.exportFile(ExportFormat.PDF_TYPE, File(new File(outputFolder+"/"+pdfName+".pdf")));

               

              }

               

               

              function getPageFor (element)

              {

                        while (!(element instanceof Page || element instanceof Application))

                                  if (element instanceof Character)

                                            element = element.parentTextFrames[0].parent;

                                  else

                                            element = element.parent;

                        if (element instanceof Page)

                                  return element;

                        return null;

              }

              • 4. Re: Script to print just the story within InDesign
                [Jongware] Most Valuable Participant

                I'm glad you like it but I don't have immediate access to a CS5 ... Theoretically it ought to have worked because I don't think I use CS4-specific functions ...

                 

                Does it stop with an error message? If so, what & at what line?

                • 5. Re: Script to print just the story within InDesign
                  notannsteer Level 1

                  Thanks for looking at it - no there is no error message - it simply  seems to stop at

                  line 18

                    p = getPageFor (storylist[s].textContainers[0]);   and return  a null value

                   

                  A workaround for me would be to batch convert these files [I have over 200  now] to IDML but i've not found one that keeps the hyperlinks from teh original document