2 Replies Latest reply on Jan 28, 2015 7:37 AM by superchitilin

    Export specified pages as jpg at defined resolution

    superchitilin

      Hello inDesign Community -

      I have a script that allows me to export pages specified from a dialog prompt as jpgs at a specific resolution (500px wide).

      This is great for making thumbnails, however It uses the dimensions of the active document page to set the resolution formula.

      This works unless there are different sized pages, at which point you get different sized jpg.

      How can use the page dimensions of each page that is being exported individually.

      I assume there is a way to get an array from jpegExportPreferences.pageString that can then be looped through.

       

      Thanks in advance

       

       

      // which pages to export

      var pagesToExport

      // final jpg demensions

      var myLargeWidth = 500;

      var myLargeHeight = 500;

       

      // create a variable for current active document

      var docRef = app.activeDocument;

       

      // remove ententions from end of file path

      myFilename = docRef.name.replace(/\.[^\.]+$/, '');

       

      // set variable to save file in same directory as active document

      myFolder = docRef.filePath;

       

      // set document measurments to points and reset ruler orgin

      docRef.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;

      docRef.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;

      docRef.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;

      docRef.zeroPoint = [0,0];

       

      //bounds    Array of Measurement Unit (Number or String)    readonly    The bounds of the Page, in the format [y1, x1, y2, x2].

      // find width of page

      var myCurrentWidth = app.activeWindow.activePage.bounds[3]-app.activeWindow.activePage.bounds[1];

      // find height of page

      var myCurrentHeight = app.activeWindow.activePage.bounds[2]-app.activeWindow.activePage.bounds[0];

       

       

      function exportJPG() {

       

        // export large jpg

                  if (myLargeWidth > 0) {

                      // Calculate the scale percentage

                      var myResizePercentage = myLargeWidth/myCurrentWidth;

                      var myExportRes = myResizePercentage * 72;

                      // exportResolution is only accurate to 1 decimal place, so round

                  }

                  else {

                      // Calculate the scale percentage

                      var myResizePercentage = myLargeHeight/myCurrentHeight;

                      var myExportRes = myResizePercentage * 72;

                  }

                  // use caluclated percentage as resolution

                  app.jpegExportPreferences.exportResolution = myExportRes;

                  // export jpg in myfolder add .jpg to end.

                  docRef.exportFile(ExportFormat.JPG, File(myFolder+"/"+myFilename+"-500px.jpg"));

                 

                  // reset back to inches

                  docRef.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.inches;

                  docRef.viewPreferences.verticalMeasurementUnits = MeasurementUnits.inches;

                 

      }               

                     

      var myName = myInput ();

      // rest of the script

      function myInput ()

      {

        var myWindow = new Window ("dialog", "Pages to export as JPG");

        var myCommentGroup = myWindow.add ("group");

        myCommentGroup.add ("statictext", undefined, "export some jpgs", {multiline:true});

        //myCommentGroup.maximumSize.width = 200;

        var myInputGroup = myWindow.add ("group");

        myInputGroup.add ("statictext", undefined, "Pages: (1-3,8,10)");

        var myText = myInputGroup.add ("edittext", undefined, "1");

        myText.characters = 20;

        myText.active = true;

        var myButtonGroup = myWindow.add ("group");

        myButtonGroup.alignment = "right";

        myButtonGroup.add ("button", undefined, "OK");

        myButtonGroup.add ("button", undefined, "Cancel");

        if (myWindow.show () == 1)

        return myText.text, pagesToExport = myText.text;

        else

        exit ();

      }

       

      // jpg export preferences

      app.jpegExportPreferences.exportingSpread= false;

      app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.HIGH;

      app.jpegExportPreferences.jpegExportRange = ExportRangeOrAllPages.EXPORT_RANGE;

      app.jpegExportPreferences.pageString = pagesToExport;

      app.jpegExportPreferences.embedColorProfile = false;

      app.jpegExportPreferences.jpegColorSpace= JpegColorSpaceEnum.RGB;

      app.jpegExportPreferences.antiAlias = true;

       

      exportJPG();

      // run function

        • 1. Re: Export specified pages as jpg at defined resolution
          Jump_Over Level 5

          Hi,

           

          1. You need to insert myCurrentWidth & Height calculation into function exportJPG() and execute it for each page;

          2. This function supposes to be run with each chosen page and different exportPreferences;

          3. So  you need split a string variable pageToExport into array collecting separate pages.

           

          Like this:

          // which pages to export
          var pagesToExport;
          // final jpg demensions
          var myLargeWidth = 500;
          var myLargeHeight = 500;
          
          // create a variable for current active document
          var docRef = app.activeDocument;
          
          // remove ententions from end of file path
          myFilename = docRef.name.replace(/\.[^\.]+$/, '');
          
          // set variable to save file in same directory as active document
          myFolder = docRef.filePath;
          
          // set document measurments to points and reset ruler orgin
          docRef.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;
          docRef.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;
          docRef.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;
          docRef.zeroPoint = [0,0];
          
          function exportJPG(cPage) {
          // export large jpg
          //bounds    Array of Measurement Unit (Number or String)    readonly    The bounds of the Page, in the format [y1, x1, y2, x2].
          // find width of page
          var myCurrentWidth = cPage.bounds[3]-cPage.bounds[1];
          // find height of page
          var myCurrentHeight = cPage.bounds[2]-cPage.bounds[0];
          if (myLargeWidth > 0) {
          // Calculate the scale percentage
          var myResizePercentage = myLargeWidth/myCurrentWidth;
          var myExportRes = myResizePercentage * 72;
          // exportResolution is only accurate to 1 decimal place, so round
          }
          else {
          // Calculate the scale percentage
          var myResizePercentage = myLargeHeight/myCurrentHeight;
          var myExportRes = myResizePercentage * 72;
          }
          // use caluclated percentage as resolution
          app.jpegExportPreferences.exportResolution = myExportRes;
          app.jpegExportPreferences.pageString = cPage.name;
          // export jpg in myfolder add .jpg to end.
          docRef.exportFile(ExportFormat.JPG, File(myFolder+"/"+myFilename + "_" + cPage.name + "-500px.jpg"));
          }             
          // SUI Dialog       
          myInput ();
          // jpg export preferences
          app.jpegExportPreferences.exportingSpread= false;
          app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.HIGH;
          app.jpegExportPreferences.jpegExportRange = ExportRangeOrAllPages.EXPORT_RANGE;
          app.jpegExportPreferences.embedColorProfile = false;
          app.jpegExportPreferences.jpegColorSpace= JpegColorSpaceEnum.RGB;
          app.jpegExportPreferences.antiAlias = true;
          
          // run function (loop through chosen pages)
          var b, c, d, stop;
          b = pagesToExport.split(",");
          while (c = b.shift() ) {
            d = c.split("-");
            stop = parseInt(d[0]);
            while (stop <= parseInt(d[d.length-1]) ) {
                 exportJPG(docRef.pages.item(stop-1));
                 stop++;
                 }
            }
          // reset back to inches
          docRef.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.inches;
          docRef.viewPreferences.verticalMeasurementUnits = MeasurementUnits.inches;
          // rest of the script
          function myInput ()
          {
            var myWindow = new Window ("dialog", "Pages to export as JPG");
            var myCommentGroup = myWindow.add ("group");
            myCommentGroup.add ("statictext", undefined, "export some jpgs", {multiline:true});
            //myCommentGroup.maximumSize.width = 200;
            var myInputGroup = myWindow.add ("group");
            myInputGroup.add ("statictext", undefined, "Pages: (1-3,8,10)");
            var myText = myInputGroup.add ("edittext", undefined, "1");
            myText.characters = 20;
            myText.active = true;
            var myButtonGroup = myWindow.add ("group");
            myButtonGroup.alignment = "right";
            myButtonGroup.add ("button", undefined, "OK");
            myButtonGroup.add ("button", undefined, "Cancel");
            if (myWindow.show () == 1)
            return pagesToExport = myText.text;
            else
            exit ();
          }
          

           

          Notice that different page sizes can lead to different width/height proportions.

          In this case JPGs will still differ. (by height)

           

          Jarek

          • 2. Re: Export specified pages as jpg at defined resolution
            superchitilin Level 1

            Thanks Jump_Over

            This is exactly what I needed but I did not know how to write the loop.

            Coming from a design background, coding is still a mystery. I looking forward to breaking apart the loop to see how it works.

             

            Thanks Again, this is a tremendous help