Export pages to jpg with custom filenames

Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / Export pages to jpg with custom filenames

Viewing 30 reply threads
  • Author
    Posts
    • #64011
      dmancini
      Participant

      Hi everyone,

      this is my first post. I have found this simple 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 all the string to lowercase.

      Thank you.

      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;

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

      }

      }

    • #64231
      Eugenyus
      Member

      if (app.documents.length != 0){
      var myDoc = app.activeDocument;
      MakeJPEGfile();
      }
      else{alert(“Please open a document and try again.”);}

      function myPS(){
      try{return myDoc.selection[0].appliedParagraphStyle;}
      catch(e){alert(“Place cursor to text with paragraph style for filenames”); exit();}
      }

      function MakeJPEGfile(){
      app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.high;
      app.jpegExportPreferences.exportResolution = 72;
      app.jpegExportPreferences.jpegExportRange = ExportRangeOrAllPages.exportRange;

      app.findGrepPreferences = null;
      app.findGrepPreferences.appliedParagraphStyle = myPS();
      var f = myDoc.findGrep();

      for (var myCounter = 0; myCounter < f.length; myCounter++){
      try{
      var curPage = f[myCounter].parentTextFrames[0].parentPage;
      if (curPage.appliedSection.name != “”) {curPage.appliedSection.name = “”;}
      var objName = f[myCounter].contents.replace(/ /g,”_”).toLowerCase();

      app.jpegExportPreferences.pageString = curPage.name;
      var myFilePath = myDoc.filePath + “/” + objName + “.jpg”; //export to a folder of the current document
      var myFile = new File(myFilePath);
      myDoc.exportFile(ExportFormat.jpg, myFile, false);
      }
      catch(e){}//pasteboard?
      }
      }

    • #70443

      Hi
      I was not able to get that to work but asked a developer friend of mine for help and we came up with this which saved me soooooo much time. It exports the jpgs into the same folder your InDesign document is saved.

      if (app.documents.length != 0){
      var myDoc = app.activeDocument;
      MakeJPEGfile();
      } else {
      alert(“Please open a document and try again.”);
      }

      function myPS() {
      try {
      return myDoc.selection[0].appliedParagraphStyle;
      } catch (e) {
      alert(“Place cursor to text with paragraph style for filenames”);
      exit();
      }
      }

      function MakeJPEGfile() {

      app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.high;
      app.jpegExportPreferences.exportResolution = 72;
      app.jpegExportPreferences.jpegExportRange = ExportRangeOrAllPages.exportRange;

      app.findGrepPreferences = null;
      app.findGrepPreferences.appliedParagraphStyle = myPS();

      var f = myDoc.findGrep();

      for (var myCounter = 0; myCounter < f.length; myCounter++) {
      try {
      var curPage = f[myCounter].parentTextFrames[0].parentPage;
      if (curPage.appliedSection.name != “”) {
      curPage.appliedSection.name = “”;
      }
      var objName = f[myCounter].contents.replace(/ /g,”_”).toLowerCase();
      app.jpegExportPreferences.pageString = curPage.name;
      var myFilePath = myDoc.filePath + “/” + objName + “.jpg”; //export to a folder of the current document
      var myFile = new File(myFilePath);
      myDoc.exportFile(ExportFormat.jpg, myFile, false);
      } catch(e) {
      //pasteboard?
      }
      }
      }

      • #97738
        Blue Li
        Member

        Hello, I can’t make it works on my Mac indesign cc2017
        it keeps telling me there is a syntax error alert(“Please open a document and try again.”);

        what can I do

        10x thanks!

      • #97799
        Blue Li
        Member

        after I changed all typographic quotation marks to the simple ” sign
        and the problem is solved~

    • #70445

      I forgot to mention that the text box needs to be on your page and not on the pasteboard. Since I did not need the text visible, I put my text box on a non printing layer so it did not show up on the images when the .jpg was created.

    • #72624

      Hi,
      Thanks for the posts above, I am a novice in creating scripts and am trying to amend the script so that I can use it to export the files as pngs rather than jpegs. Below is the script I have so far, but it doesn’t seem to save any files. I don’t get an error message however. Any ideas?
      Thanks very much in advance!

      if (app.documents.length != 0){
      var myDoc = app.activeDocument;
      MakePNG_FORMATfile();
      } else {
      alert(“Please open a document and try again.”);
      }
      function myPS() {
      try {
      return myDoc.selection[0].appliedParagraphStyle;
      } catch (e) {
      alert(“Place cursor to text with paragraph style for filenames”);
      exit();
      }
      }
      function MakePNG_FORMATfile() {
      app.pngExportPreferences.pngQuality = PNGQualityEnum.MAXIMUM;
      app.pngExportPreferences.exportResolution = 72;
      app.pngExportPreferences.pngExportRange = ExportRangeOrAllPages.exportRange;
      app.findGrepPreferences = null;
      app.findGrepPreferences.appliedParagraphStyle = myPS();

      var f = myDoc.findGrep();

      for (var myCounter = 0; myCounter < f.length; myCounter++) {
      try {
      var curPage = f[myCounter].parentTextFrames[0].parentPage;
      if (curPage.appliedSection.name != “”) {
      curPage.appliedSection.name = “”;
      }
      var objName = f[myCounter].contents.replace(/ /g,”_”).toLowerCase();
      app.png_formatExportPreferences.pageString = curPage.name;
      var myFilePath = myDoc.filePath + “/” + objName + “.png”; //export to a folder of the current document
      var myFile = new File(myFilePath);
      myDoc.exportFile(ExportFormat.png, myFile, false);
      } catch(e) {
      //pasteboard?
      }
      }
      }

    • #72630

      I am sorry but I am also a novice at writing any kind of script. I had a developer friend of mine help with the script I posted.

      I compared your .png script to my .jpg script and it looks like the only real difference is where mine says .jpg yours says PNG_FORMAT. I don’t know if it should just read .png or if there is a case sensitive issue but you might try changing that.

      Also, when I first ran my script, it created no .jpgs but I realized it was because my text boxes were on the pasteboard and not on the page. They need to be on the page.

      You could always create jpgs in InDesign and then run create a script in Photoshop to convert the jpgs to .pngs.

      I hope this helps.

    • #72651

      I had thought the same idea about running script/action in PhotoShop post making the jpegs, so I think that’s the answer!
      Thanks for your help (and the initial script too!)

    • #72827

      This exactly the script I need. So thank all for getting the foundations down.

      Is this script not suitable for CC?

      I get the following error message:

      ********************

      JavaScript Error!

      Error Number: 55
      Error String: Object does not support the property or method ‘applied ParagraphStyle’

      ********************

      I am a bit new to this so I might be missing something else. I have unique paragraph style used for the filename text boxes. Does it need a particular name or do I just need to highlight and example piece of text.

      Thanking you!

    • #72852
      Loic Aigon
      Member

      Hi,

      What is you active selection ? As the script tries to get back the applied paragraph style, having anything but “text” object selected will make the script to fail.

      Loic
      https://www.ozalto.com

    • #72859

      I did not have to select the text. I only had my cursor placed with in the text that had that particular paragraph style.

    • #73888

      I’m also running into the same issue with this script. I’d love to be able to use it as it seems like a genius fix for what I’m trying to do, however, I get the following error:

      Error Number: 55
      Error String: Object does not support the property or method ‘appliedParagraphStyle’

      Engine: main
      Line: 9
      Source: return
      myDoc.selection[0].appliedParagraphStyle;

      Any help would be greatly appreciated! Thanks in advance!

    • #73889

      Sairah
      I just tried to run the script and I got the same error. But then I realized that my TypeTool cursor was not blinking in the copy of the Paragraph Style. Once it was in there, I dounleckicked on the script name in the Scripts Panel to start the script and it worked.

      I am running OS X 10.10.2 and Indesign 2014.2 if that information helps at all.

      Good luck!

    • #73890

      You are correct!! Thank you SO MUCH, truly, I am so grateful for your help! =D I’ve never used InDesign scripts before, this makes them seem a lot less daunting! Hope you’re having a wonderful Wednesday!

    • #76164

      Hi

      This is amazing! Just got it to work after a bit of fiddling.

      Just a note … I copied your text (greg schneider) and created a jsx file however a message kept coming up when I ran the script that there was a syntax error because of the speech quotation marks used in your code. I then remembered coming across this before when adding code from a website into Adobe Acrobat:

      When one copies your script form this page your quotation marks in the code look like this: “ or ” but they should all look like this: ”

      If you notice they look a bit different. Just in case anyone else gets the same error.

      Thank you so much. This is the first time I’ve ever used a script in InDesign :)

    • #76225

      So glad it worked for you. And thanks for the quotation marks tip.

    • #79564

      Hi! I was wondering if you could help?

      I just installed the new InDesign 2015.1 release on Friday from, I suppose, the one just before (2014?) and now this script does not work. It says:

      Error Number: 55
      Object does not support the property or method ‘applied ParagraphStyle’

      later it mentions:

      Line: 10
      Source: return myDoc.selection[0].appliedParagraphStyle

      I assume something has changed between the two versions of InDesign. Can you assist?

      • #79655

        Hello. I wish I could help.

        I too am using InDesign 2015.1 and 11.1.0.122 Build but HAVE BEEN SUCCESSFUL in getting the script to work.

        The only time I could get an error message similar to the one you describe is when I have the text box selected with the black arrow. Once I have my text cursor blinking in the copy, it works fine.

        Wish I could be more help. Have a good Thanksgiving!

    • #91104
      David Savini
      Member

      Hello, if you still looking for a png version, this one is workiing :

      if (app.documents.length != 0){
      var myDoc = app.activeDocument;
      MakePNGfile();
      } else {
      alert(“Please open a document and try again.”);
      }

      function myPS() {
      try {
      return myDoc.selection[0].appliedParagraphStyle;
      } catch (e) {
      alert(“Place cursor to text with paragraph style for filenames”);
      exit();
      }
      }

      function MakePNGfile() {

      app.pngExportPreferences.pngQuality = PNGQualityEnum.MAXIMUM;
      app.pngExportPreferences.exportResolution = 72;
      app.pngExportPreferences.transparentBackground = true;
      app.pngExportPreferences.pngExportRange = PNGExportRangeEnum.EXPORT_RANGE;

      app.findGrepPreferences = null;
      app.findGrepPreferences.appliedParagraphStyle = myPS();

      var f = myDoc.findGrep();

      for (var myCounter = 0; myCounter < f.length; myCounter++) {
      try {
      var curPage = f[myCounter].parentTextFrames[0].parentPage;
      if (curPage.appliedSection.name != “”) {
      curPage.appliedSection.name = “”;
      }
      var objName = f[myCounter].contents.replace(/ /g,”_”).toLowerCase();
      app.pngExportPreferences.pageString = curPage.name;
      var myFilePath = myDoc.filePath + “/” + objName + “.png”; //export to a folder of the current document
      var myFile = new File(myFilePath);
      myDoc.exportFile(ExportFormat.PNG_FORMAT, myFile, false);
      } catch(e) {
      //pasteboard?
      }
      }
      }

    • #91749

      Just wondering if anyone can add code so that if I need to export the pages again to jpegs, that it re-writes over the top of the files that exist automatically? Its a bit annoying to have to delete first each time.

    • #102127

      I found this script here and I’m trying to make it work on an InDesign doc I have.
      I’m sort of OK with scripting, but not much at home in InDesign of late.

      (I’m twisting the arm on InDesign a little bit, trying to make it work for me as a way to export lots of mobile screenshots with marketing copy in various languages, merged in.)

      The doc is set up using the recently added CC functionality which allows pages to be arranged in Spreads, up to 10 or so per Spread I think. The Spreads are named for each of the 13 languages I need.
      I have 6 pages per language and they’re named eg. EN1-EN6. (I can’t remember how I got that naming to work…)

      I need to be able to export the pages out as PNGs with the right names; the text fields with the names are set up and this does seem to be working, however only the 1st Spread will export (first 6 pages).
      Then I get this error alert, and I get it for each of the remaining pages:

      “Failed to export the PNG file : Invalid Page number specified”

      I’m guessing the problem here is that the indexing of pages is different when I’m using Spreads in this creative manner?
      Can anyone intuit what I might need to do to fix it?

      Thanks
      Rasmus

    • #102168

      It turns out I was able to fix this problem in the script. If anyone has the same problem, it has to do with the property continueNumbering of the current parent section – all spreads started with page 1.
      By forcing it to TRUE it worked:

      if (currentPage.appliedSection.name != “”) {
      currentPage.appliedSection.continueNumbering = true;
      currentPage.appliedSection.name = “”;
      }

    • #115771

      @ Eugenyus

      You the man… You the man!!!! WHOOP WHOOP
      This saved me so much time!

    • #115772

      PS for those which say it doesnt work, you need to clean it a bit. Copy Pasting it from this forum give the wrong ” replace those and it works just fine

    • #116046
      Alain Bolduc
      Participant

      This is exactly what I need… except I would like to export to a specific dossier or desktop. Possible?

    • #116128
      Brian Pifer
      Participant

      Alain, you can set the file destination by setting the path you desire in this line of the original code. Change Desktop to the destination you want:

      var myFilePath = “~/Desktop/” + myPageName + “.jpg”;

      • #116130
        Alain Bolduc
        Participant

        Thanks so much! This is perfect

        Is there a way for the scrip to ask user to create a folder on the desktop than saving into that folder?

      • #116271
        Brian Pifer
        Participant

        Sorry for the delayed reply.

        Add this somewhere in your code:

        var myPrompt = prompt(“Name your folder to create on the desktop”);

        var f = new Folder(‘~/Desktop/’ + myPrompt);

        if (!f.exists)
        f.create();

        Then you will want to update the myFilePath variable to:

        var myFilePath = f.fsName + myPageName + “.jpg”;

        Note: I do not have Indesign installed and am a bit rusty. I may be forgetting some slash marks in there, and can’t remember if you will want to use f.fsName or f.fullName. But play with that.

    • #116609

      Does someone have the complete script that is the sum of all these posts?

      I need to take an INDD file with many pages 1920 x 1080 Pixels, each of which contains one image with a text box that contains the image name wtihout the file type suffix, and create a folder with one JPG image per page with the name of the file as the name of the image.

    • #116610

      Addendum to my post of May 20, 2019 at 11:46 am:

      Correction, the name of the file should come from the text box on the same page as the image. The text box is in the Paragraph Style “Images” and in the Object Style “Image Desc”. I should mention that the text box may contain Forced Line Breaks, if that is a factor. If that poses too much of an issue I could generate the export prior to doing the reformatting that adds the Breaks.

    • #116633
      Alain Bolduc
      Participant

      Here’s the script I’ve been using and it’s working great. I’ve managed to have the JPG saved on my desktop though, not in a specific folder.
      I’ve changed the resolution to 300 dpi…
      Also, the script is looking for the Paragraph style “filenames”. As long as the text box for the names is set with that style, I think it will work, but I ain’t an expert!
      Hope this can help.


      if (app.documents.length != 0){
      var myDoc = app.activeDocument;
      MakeJPEGfile();
      } else {
      alert("Please open a document and try again.");
      }
      function myPS() {
      try {
      return myDoc.selection[0].appliedParagraphStyle;
      } catch (e) {
      alert("Place cursor to text with paragraph style for filenames");
      exit();
      }
      }
      function MakeJPEGfile() {
      app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.high;
      app.jpegExportPreferences.exportResolution = 300;
      app.jpegExportPreferences.jpegExportRange = ExportRangeOrAllPages.exportRange;
      app.findGrepPreferences = null;
      app.findGrepPreferences.appliedParagraphStyle = myPS();
      var f = myDoc.findGrep();
      for (var myCounter = 0; myCounter < f.length; myCounter++) {
      try {
      var curPage = f[myCounter].parentTextFrames[0].parentPage;
      if (curPage.appliedSection.name != "") {
      curPage.appliedSection.name = "";
      }
      var objName = f[myCounter].contents.replace(/ /g,"_").toLowerCase();
      app.jpegExportPreferences.pageString = curPage.name;
      var myFilePath = "~/Desktop/" + "/" + objName + ".jpg"; //export to a folder of the current document
      var myFile = new File(myFilePath);
      myDoc.exportFile(ExportFormat.jpg, myFile, false);
      }
      catch(e){}//pasteboard?
      }
      }

    • #116670

      Alain

      I got the script to work but I have used the “Insert Current Page” function in the INDD file to incorporate the page number into the file name. “100 File Name of this image”.
      But that does not seem to get passed to the script or is getting stripped off.

      Any suggestions?

      Thanks for your script!

    • #116671
      Alain Bolduc
      Participant

      Like I said, I’m not a script expert. I grab the same script here ! But managed to combine it with another one to make it save on the desktop.

      From what I experience, as long as the text box that will give the name of the files has to stylised with the “filename” style, it should work.

      In my case, I use something like this : [number##_150x90px], [number##_300x250px], etc. And I do a find and replace for the “number##” value.
      But I used this procedure for a 14 pages document that is pre-formatted… I guess if the Insert Current Page is also stylised, it should work… but I didn’t tried.

    • #116668

      Tried your script but got:

      Error Number 25
      Error String: Expected: )

      JavaScript Error!
      Engine: main
      File: /Users/galleria/Library/Preferences/Adobe lnDesign/Version
      14.0 /en_US/ Scripts /Scripts Panel/ ExportToJPGWithTitle.jsx
      Line: 22
      Source: for (var myCounter = O; myCounter &It; f.length ; myCounter+
      +){
      Offending Text: ;

      Any Suggestions?

    • #14374379

      Hi Members

      try this free Script

      (Export Pages to JPG Based on Paragraph Style v1.0.19)

      on the Link :

      https://www.hasaninscripts.com/free-scripts#h.p2yci15e4sya

Viewing 30 reply threads
  • You must be logged in to reply to this topic.
>
Notice: We use cookies on our websites to give you a great online experience. If you keep browsing, we'll assume you're ok with this. For more information, see our privacy policy. By closing this banner, you agree to the use of cookies.I AGREENo