6 Replies Latest reply on Dec 9, 2013 5:41 AM by cathykka

    Script Export Single Pages with Custom Filename and Paragraph Style?

    cathykka Level 1

      I have a document called 'mydocument'

      I have a multiple pages in the document.

      Each page has one "title" styled with a pargraph style sheet called title.

      I need to export each page as individual .jpg with a custom file name

       

      mydocument-01-title.jpg

       

      with the 01 being the page number

       

      Seemed like a simple request... but turns out it is a bit of a challenge.

       

       

      This scipt below I found on another thread is kinda working

      it gives me a

      title1.jpg

       

      But I can't figure out how to....

      1. add "mydocument" name

      2. have a leading 0 on 1 digit numbers

       

      Can someone please guide me, I would be for ever grateful.

       

       

      if (app.documents.length != 0) {
           var myDoc = app.activeDocument;
          MakeJPEGfile();
      }
      else{  
           alert("Please open a document and try again.");  
      } 
      
      
      function MakeJPEGfile() { 
      
      
      
      
      
      
           for(var myCounter = 0; myCounter < myDoc.pages.length; myCounter++) {
      
                if (myDoc.pages.item(myCounter).appliedSection.name != "") {
                     myDoc.pages.item(myCounter).appliedSection.name = "";
                }
                  
                  var myPageName = myDoc.pages.item(myCounter).name;
                var myJpegPrefix = "";
                var isStyleExist = true;
      
                //Checking the existing of the paragraph style (filename)
                try {
                    myDoc.paragraphStyles.item("title");
                }
                catch (myError) {
                    isStyleExist = false;
                }
      
                if (isStyleExist)
      
      
                myJpegPrefix = getParagraphContent(myDoc.paragraphStyles.item("title"), myDoc.pages.item(myCounter)) + myPageName;
                app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.high; // low medium high maximum
                app.jpegExportPreferences.exportResolution = 72;
                app.jpegExportPreferences.jpegExportRange = ExportRangeOrAllPages.exportRange;
                app.jpegExportPreferences.pageString = myPageName;
      
      
                var myFilePath = "~/-client/JOYS - Just Organize Your Stuff/-Art/-art-book-kindle/" + myJpegPrefix + ".jpg";
                var myFile = new File(myFilePath);
                myDoc.exportFile(ExportFormat.jpg, myFile, false);
           }
      }
      
      
      // The new function, but needs to add checking the file name rules.
      
      
      function getParagraphContent (paragraphStyle, currentPage)
      {
          var paragraphContent = null;
          for (var c = 0; c < currentPage.textFrames.length; c++)
          {
              for (var i = 0; i < currentPage.textFrames.item(c).paragraphs.length; i++)
              {
                  if (currentPage.textFrames.item(c).paragraphs.item(i).appliedParagraphStyle == paragraphStyle)
                  {
                      paragraphContent = currentPage.textFrames.item(c).paragraphs.item(i).contents;
         // Remove spaces and returns at the end:
                      paragraphContent = paragraphContent.replace(/\s+$/, '');
         // Replace remaining spaces with hyphen:
                      paragraphContent = paragraphContent.replace(/\s+/g, '-');
         // Make lowercase:
                      paragraphContent = paragraphContent.toLowerCase();
                      return paragraphContent;
                  }
              }
          }
          return paragraphContent;
      }
      
      
        • 1. Re: Script Export Single Pages with Custom Filename and Paragraph Style?
          Mi_D Level 3

          change line

           

          var myFilePath = "~/-client/JOYS - Just Organize Your Stuff/-Art/-art-book-kindle/" + myJpegPrefix + ".jpg";

           

          var myFilePath ="~/-client/JOYS - Just Organize Your Stuff/-Art/-art-book-kindle/" +'/'+ myDoc.name+ '-0'+ myPageName+'-'+myJpegPrefix + ".jpg";

           

          and remove + myPageName from myJpegPrefix  variable

          1 person found this helpful
          • 2. Re: Script Export Single Pages with Custom Filename and Paragraph Style?
            cathykka Level 1

            Thanks Mi!

             

            One step closer.

            I just need to remove the extension .indd.

             

            My file name is now

             

            mydocument.indd-01-title.jpg

             

            if (app.documents.length != 0) {

                 var myDoc = app.activeDocument;

                MakeJPEGfile();

            }

            else{ 

                 alert("Please open a document and try again."); 

            }

             

             

             

             

            function MakeJPEGfile() {

                      for(var myCounter = 0; myCounter < myDoc.pages.length; myCounter++) {

             

                      if (myDoc.pages.item(myCounter).appliedSection.name != "") {

                           myDoc.pages.item(myCounter).appliedSection.name = "";

                      }

                       

                      var myPageName = myDoc.pages.item(myCounter).name;

                      var myJpegPrefix = "";

                      var isStyleExist = true;

                      

                      //Checking the existing of the paragraph style (filename)

                      try {

                          myDoc.paragraphStyles.item("title");

                      }

                      catch (myError) {

                          isStyleExist = false;

                      }

             

                      if (isStyleExist)

             

             

                      myJpegPrefix = getParagraphContent(myDoc.paragraphStyles.item("title"), myDoc.pages.item(myCounter))+ '-0' + myPageName;

                      app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.high; // low medium high maximum

                      app.jpegExportPreferences.exportResolution = 72;

                      app.jpegExportPreferences.jpegExportRange = ExportRangeOrAllPages.exportRange;

                      app.jpegExportPreferences.pageString = myPageName;

             

             

                      var myFilePath ="~/-client/JOYS - Just Organize Your Stuff/-Art/-art-book-kindle/" +'/'+ myDoc.name+"-"+ myJpegPrefix+".jpg";

                      var myFile = new File(myFilePath);

                      myDoc.exportFile(ExportFormat.jpg, myFile, false);

                 }

            }

             

             

            // The new function, but needs to add checking the file name rules.

             

             

            function getParagraphContent (paragraphStyle, currentPage)

            {

                var paragraphContent = null;

                for (var c = 0; c < currentPage.textFrames.length; c++)

                {

                    for (var i = 0; i < currentPage.textFrames.item(c).paragraphs.length; i++)

                    {

                        if (currentPage.textFrames.item(c).paragraphs.item(i).appliedParagraphStyle == paragraphStyle)

                        {

                            paragraphContent = currentPage.textFrames.item(c).paragraphs.item(i).contents;

               // Remove spaces and returns at the end:

                            paragraphContent = paragraphContent.replace(/\s+$/, '');

               // Replace remaining spaces with hyphen:

                            paragraphContent = paragraphContent.replace(/\s+/g, '-');

               // Make lowercase:

                            paragraphContent = paragraphContent.toLowerCase();

                            return paragraphContent;

                        }

                    }

                }

                return paragraphContent;

            }

             

             


            • 3. Re: Script Export Single Pages with Custom Filename and Paragraph Style?
              Laubender Adobe Community Professional & MVP

              @Kathy – for removing the extension ".indd" see:

               

              http://forums.adobe.com/message/5861074#5861074

               

              Uwe

              1 person found this helpful
              • 4. Re: Script Export Single Pages with Custom Filename and Paragraph Style?
                cathykka Level 1

                Thank you. The following worked to remove the .indd.

                 

                var myDocTitle = myDoc.name.replace(/\.indd$/i,"");

                          var myFilePath ="~/-client/JOYS - Just Organize Your Stuff/-Art/-art-book-kindle/" +'/'+ myDocTitle+"-"+ myJpegPrefix+".jpg";

                 

                Now... last thing.

                My filename is now mydocument-title-01.jpg

                The page number always appeared last. (typo in last message)

                 

                I need it to be

                mydocument-01-title.jpg

                 

                I am still trying to wrap my head around javascript. The lightbulb hasn't gone off....

                • 5. Re: Script Export Single Pages with Custom Filename and Paragraph Style?
                  Green4ever Level 3

                  Add the following line before the "myFilePath" statement.

                   

                  myJpegPrefix = myJpegPrefix.replace(/(.+)\-(\d+)$/i, "$2-$1");
                  

                   

                  It is assumed that your myJpegPrefix variable will have the pattern like xxxxxxx-00. If the pattern is diffent it will not work.

                   

                  ~Green4ever

                  1 person found this helpful
                  • 6. Re: Script Export Single Pages with Custom Filename and Paragraph Style?
                    cathykka Level 1

                    That did it. Thank you all. Your help is very much appreciated!