7 Replies Latest reply on Mar 25, 2009 11:58 AM by Peter Kahrel

    Save each page as PDF naming mod

    Really Randy
      I had an issue I needed help with a while back, now I need to mod the script.
      Here's the original post
      Peter Kahrel, ""Save each page as PDF" naming mod?" #1, 16 Jan 2008 8:00 am

      I have a 456 page document that was made from a data merge. It has a background image, a foreground image and a job number/description.

      I need to save each page out as a jpg and name the file with the contents of the "jobnumber" text box. Previously Peter Kahrel helped me out tremendously with a little function that looked at the job number (which was always PNXX-xxxx) and swapped it out with the real job number.

      Now the contents of the jobnumber field isn't structured, i.e., "AvocadoSwirl_V", or "BurntSienna_G".

      Is there a way to set a script label for the "jobnumber" text box and just name the output file whatever is in there?

      Here is the existing script:

      var destinationFolder = Folder.selectDialog ("Choose a Folder");

      var myPDFExportPreset = app.pdfExportPresets.item("WebVidOverlay");

      doc = app.activeDocument;
      doc.sections.everyItem().includeSectionPrefix = false;
      app.findTextPreferences = null;
      app.findTextPreferences.findWhat = 'pnxx^9^9^9^9';
      found = doc.findText();
      for (i = 0; i < found.length; i++ )
      {
      f = File (destinationFolder + "/" + found[i].contents + '.pdf');
      app.pdfExportPreferences.pageRange = found[i].parentTextFrames[0].parent.name;
      doc.exportFile (ExportFormat.pdfType, f, false, myPDFExportPreset);
      }

      As always, Thanks a million,
      Randy
        • 1. Re: Save each page as PDF naming mod
          Peter Kahrel Adobe Community Professional & MVP
          How would you find each job number? Is it in a frame that you labelled? Does it always contain an underscore? Another tack: you say the page has two images and the job description. Is it then the case that all text on a page is the job number?

          Peter
          • 2. Re: Save each page as PDF naming mod
            Really Randy Level 1
            The job number/description would always be in the text frame labeled "jobnumber" (or whatever) but it doesn't always contain anything in particular. Sort of a freeform job description. If I could always capture anything in the "jobnumber" text field, that would be optimum. Also it would allow me to re-purpose that part of the script so I wouldn't have to bug you guys for advice about it. :-)

            All text at this time is the job number but I can foresee that changing in the very near future.

            Thank you so much for your help and advice BTW.

            Randy
            • 3. Re: Save each page as PDF naming mod
              Peter Kahrel Adobe Community Professional & MVP
              So if there are pages without a job number, there's not a lot that you can do, is there? Besides, what could a script do with text frames labelled '"jobnumber" or whatever'?

              Peter
              • 4. Re: Save each page as PDF naming mod
                Really Randy Level 1
                well, there would always be something in the jobnumber text field.
                Say I had three records in the datamerge file that were say "Avocado", "Burnt Sienna" and "pnga-0431". If I filled the "jobnumber" text field with that information then I should be able save each file with the names "Avocado.jpg", "Burnt Sienna.jpg" and "pnga-0431.jpg" respectively. Is that correct?

                Maybe something like this?

                +++++++++++++++++++++++++++++++++++++++++++
                Labeled textFrame "jobnumber";
                Label a textFrame by selecting it in the ui and then use the script palette, label toggle > enter a string such as "jobnumber in the field.

                var co = pg[i].textFrames.item("jobnumber").contents;

                ad.exportFile(ExportFormat.pdfType, File(docpath+"/"+docname.split(".indd")[0]+"_"+jobnumber+".pdf"), false, myPDFExportPreset);
                +++++++++++++++++++++++++++++++++++++++++++

                Logically it would go something like this:
                myFilename = contents of text box labeled "jobnumber"
                Save file as "jpg" with docpath of ("/" + "myFilename" + .jpg)

                Does that sound do-able?

                Thanks,
                Randy
                • 5. Re: Save each page as PDF naming mod
                  Peter Kahrel Adobe Community Professional & MVP
                  >Does that sound do-able?

                  Most things are doable, provided that your information is precise: first you say that pages may or may not have a text frame labelled "jobnumber" and that that frame may or may not have text in it (your post can be read like that anyway). Now you say that every page has a text frame labelled "jobnumber" and that that frame can be filled with either of three text strings. Or maybe all three strings? You say
                  >then I should be able save each file with the names "Avocado.jpg", "Burnt Sienna.jpg" and "pnga-0431.jpg" respectively.

                  Or do you mean "save each file with the names "Avocado.jpg", "Burnt Sienna.jpg" _OR_ "pnga-0431.jpg" respectively."? The way you phrased it, it looks as if you want to save three files from a page. But your example shows that you meant "or", not "and".

                  But anyway, the last two lines of your example appear to make clear what you want. The attached script exports each page of the active document to JPG, using the contents of the text frame labelled "jobnumber" on that page. The script assumes that each page has such a text frame and that there is something in it, and that whatever is in it provides a legal file name. It doesn't do any error checking/catching at all.

                  Peter

                  app.jpegExportPreferences.resolution = 300;
                  
                  app.jpegExportPreferences.jpegExportRange = ExportRangeOrAllPages.exportRange;

                  app.activeDocument.sections.everyItem().includeSectionPrefix = false;

                  path = app.activeDocument.filePath + '/';
                  p = app.activeDocument.pages.everyItem().getElements();
                  for (i = 0; i < p.length; i++)
                    {
                    jpg_name = p[i].textFrames.item ('jobnumber').contents + '.jpg';
                    app.jpegExportPreferences.pageString = p[i].name;
                    app.activeDocument.exportFile (ExportFormat.jpg, File (path + jpg_name));
                    }
                  • 6. Re: Save each page as PDF naming mod
                    Really Randy Level 1
                    Awesome, made a few mods with a chooseable filepath and little things like that. I'll post it for others when I have a real world example.

                    The whole thing works great but I don't quite understand hoe line 11 works:
                    app.jpegExportPreferences.pageString = p[i].name;

                    Specifically the "p[i].name" part. Why is it prefixed by "app.jpegExportPreferences.pageString"? Judging by the speed of this script, I'm sure it's the most efficient way of doing it. I'm just trying to learn a little from your example.

                    Again, Thanks a million

                    Randy
                    • 7. Re: Save each page as PDF naming mod
                      Peter Kahrel Adobe Community Professional & MVP
                      Randy,

                      For any i, p[i] is a page as an object. The name of a page is its number, so p[i].name is the number of p[i] as you see it when you display page numbers.

                      "app.jpegExportPreferences.pageString = ..." mimicks what you would do when you'd export in the UI: File > Export, JPEG, then type the number of the page you want to export in the "Export JPEG" dialog.

                      Peter