Back

If your email is not recognized and you believe it should be, please contact us.

  • You must be logged in to reply to this topic.Login

Export All Open Doc to JPG

Return to Member Forum

  • Author
    Posts
    • #85979
      Kathy Cote
      Member

      Hi all,
      I hope someone could help me with this.
      I’m trying to export all open documents to jpg. Export to jpg works well but It doesn’t export all others open documents.
      Can someone help me?

      here is my code:

      var myFilePath, myFile;
      var docs = app.documents;
      var myDoc = app.activeDocument;

      for (var i = 0; i < app.documents.length; i++) {
      app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.high;
      app.jpegExportPreferences.exportResolution = 200;
      var myFilePath = “/Users/kcote/Desktop/TEST update/JPG/” + myDoc.name.slice (0, -5) + “.jpg”;
      var myFile = new File(myFilePath);
      myDoc.exportFile(ExportFormat.jpg, myFile, false);
      }

    • #85982

      Hi Kathy,

      if you want to export all documents, you must iterate over all documents ;-) But in your script ‘myDoc’ is always the same document.
      Did you realize, that you export always the first page, nothing more?

      Try the following:

      var myFilePath;
      var myFile;
      var allDocs = app.documents;
      var myDoc;
      
      app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.high;
      app.jpegExportPreferences.exportResolution = 200;
      
      for (var i = 0; i < allDocs.length; i++) {
        myDoc = allDocs[i];
        var myFilePath = “/Users/kcote/Desktop/TEST update/JPG/” + myDoc.name.replace(/indd$/, "jpg");
        var myFile = new File(myFilePath);
        myDoc.exportFile(ExportFormat.jpg, myFile, false);
      } 
      

      best
      Kai

    • #85985
      Kathy Cote
      Member

      oh wow it works! Thanks a lot! I really appreciate your help.

      I have one more question …
      Sometimes I have documents with one page and others with multiple pages. If it is a document with multiple pages, do you know if it’s possible to export all page, per page for all open documents?

      Thanks

      • #85986
        Matt Isaac
        Participant

        /*This is just a question not a solution to the problem as far as i know*/
        Kai, for the multiple pages could you call a separate function within the for loop eg.

        for var i = 0; i < allDocs.length; exportPages i++)
        function exoprtPages(){
        for (var n = 0; n < app.document.pages.length; n++){
        }}

        Then use the same export method for each page within a document before moving to the next document?

    • #85990
      Ari Singer
      Member

      Skemicle, you can use a separate loop for that, but it has to be nested in the outer document loop, and the variables has to be different.

      To export individual pages is a bit more involved. So here it is:

      var myFilePath;
      var myFile;
      var allDocs = app.documents;
      var myDoc;
      var myPages;
      var myPage;
      var myPageNum;
      
      app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.high;
      app.jpegExportPreferences.exportResolution = 200;
      app.jpegExportPreferences.jpegExportRange = ExportRangeOrAllPages.EXPORT_RANGE;
      
      for (var i = 0; i < allDocs.length; i++) {
        myDoc = allDocs[i];
        myPages = myDoc.pages;
        for (var j = 0; j < myPages.length; j++) {
            myPage = myPages[j];
            myPageNum = myPage.name;
            var myFilePath = "/Users/kcote/Desktop/TEST update/JPG/" + myDoc.name.replace(/indd$/, "") + " page " + myPageNum + ".jpg";
            var myFile = new File(myFilePath);
            app.jpegExportPreferences.pageString = myPageNum;
            myDoc.exportFile(ExportFormat.jpg, myFile, false);
            } 
        }
      
      • #85992
        Matt Isaac
        Participant

        In your script, I notice that you have some variables that are not assigned a value. What is the purpose of assigning a variable but not giving it a value?
        example:var myFilePath;
        var myFile;
        etc...

        Also within the loop you call the variables without indicating they are variables
        example:myPage = myPages[j]; whereas you do in other instances: var myFile = new File(myFilePath); Is there reason for this?

      • #85997
        Ari Singer
        Member

        There are two steps to a variable: 1. It has to be declared before it’s used anywhere. 2. Then you assign any value to it.

        But this can be done in various ways. When you know initially what value the variable is going to hold, then you can assign the value while it’s declared, as such: var allDocs = app.documents;.

        But what if you don’t know yet what value it’s going to hold initially, then you just declare it without any assignment, as such: var myPage;, and then when you’re ready to assign anything to it, you do without declaring it again (because once a variable is declared it doesn’t have to be declared again), as such: myPage = myPages[j];.

        If you want to be able to write serious scripts then you need to have a basic understanding of the language. I would advise you to read the book Head First JavaScript. It’s an excellent book for absolute beginners.

    • #85996

      Skemicle, these are js-basics! https://www.w3schools.com/js/js_variables.asp or google by words such as “declaring” & “initializing”.

      Ari and I adopt the code from Kathy, so there is a little inconsistency in that.

    • #86002
      Abdulaziz Junaid
      Participant

      If the exporting is a routine task for all projects try to use Tree Shade plugin where it auto exporting to jpg after saving.
      The InDesign code is open source and you can customize.
      https://sourceforge.net/projects/treeshade/

    • #86087
      Kathy Cote
      Member

      Thank you to everyone for your help! it works great!

Viewing 6 reply threads
  • You must be logged in to reply to this topic.
Forum Ads