2 Replies Latest reply on Jan 30, 2013 4:15 AM by [Jongware]

    Export pages to jpg with custom filenames

    d_mancini

      Hi everyone,

       

      I have found – and slighty edited – this script that exports each InDesign page to jpg, naming it with the relative page number.

      I need to assign to the filename the value of a text that I have placed in each page, that is styled with a paragraph style named "filename".

      It also would be nice to have filenames web-friendly, replacing spaces with dashes and transforming to lowercase.

       

      Thanks in advance for your help.

       

      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;
                app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.high; // low medium high maximum
                app.jpegExportPreferences.exportResolution = 72;
                app.jpegExportPreferences.jpegExportRange = ExportRangeOrAllPages.exportRange;
                app.jpegExportPreferences.pageString = myPageName;
      
                var myFilePath = "~/Desktop/" + myPageName + ".jpg";
                var myFile = new File(myFilePath);
                myDoc.exportFile(ExportFormat.jpg, myFile, false);
           }
      }
      
        • 1. Re: Export pages to jpg with custom filenames
          dulajun Level 2

          This code is the same as above but with some modification and a new fuction

           

          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("filename");
                    }
                    catch (myError) {
                        isStyleExist = false;
                    }
          
                    if (isStyleExist)
                      myJpegPrefix = getParagraphContent(myDoc.paragraphStyles.item("filename"), 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 = "~/Desktop/" + 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 = "";
              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;
                          return paragraphContent;
                      }
                  }
              }
              return paragraphContent;
          }
          
          

           

          Missed checking the file naming rules.

          • 2. Re: Export pages to jpg with custom filenames
            [Jongware] Most Valuable Participant

            Abdulaziz Junaid wrote:

             

            [..] Missed checking the file naming rules.

             

            Something like this:

             

            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;
            }
            

             

            It does not check for any other illegal characters ((back)slashes, asterisk, question mark, colons, semicolons) -- and neither for ill-advised characters (anything else than plain ASCII such as accented characters or en-dashes). While possible, it's best to avoid this right from the start, rather than relying on the script to remove them or replace with something harmless.

            1 person found this helpful