15 Replies Latest reply on Jan 6, 2010 7:52 AM by Li3149

    Extract indesign preview

    Thomas B. Nielsen Level 3
      Is it possible to get/extract the preview file saved in Indesign documents by scripting?

      We are exporting a JPG file of every document, but for large format documents / complex documents til is a slow process. So i thought, perhaps it is possible to use the file that indesign saves ("Preferences -> File handling")?

      Any ideas?
        • 1. Re: Extract indesign preview
          Thomas B. Nielsen Level 3
          The XMPdata under xmpGImg is base64 encoded, see this post:
          http://www.adobeforums.com/webx?128@@.3bbcb337

          Now i need to extract the data from the XMPdata("file info -> advanced tab").

          How is this done?
          • 2. Re: Extract indesign preview
            Thomas B. Nielsen Level 3
            var myString = myDocument.metadataPreferences.getProperty("http://ns.adobe.com/xap/1.0/","Thumbnails/*[1 ]/xmpGImg:image");

            // replace - http://www.adobeforums.com/webx?128@@.3bbcb337
            myString = myString.replace(" ", "\n");

            will extract the base64 encoded data.

            using the script http://www.webtoolkit.info/javascript-base64.html i thought i could decode the preview and then save to a file, but i get a invalid JPEG file.

            However, feeding the script on the folowing url with the encoded string, forcing download, will produce a valid jpeg.
            http://opinionatedgeek.com/dotnet/tools/Base64Decode/Default.aspx
            • 3. Re: Extract indesign preview
              Peter Kahrel Adobe Community Professional & MVP
              JavaScript implementations of Base64 decoders can be found on the web. Below is a script that uses Thomas's method to retrieve the thumbnail from the active document and a script I found (and slightly adapted) on Stephen Ostermiller's site (Google's first hit using "base64 decoder javascript").

              Peter

              /*The Base 64 decoder is from Stephen Ostermiller's site:
              
              http://ostermiller.org/calc/encode.html

              Preview extractor from Thomas Nielsen
              */

              file_name = '/d/thumb.jpg';

              var END_OF_INPUT = -1;

              var base64Chars = new Array(
                 'A','B','C','D','E','F','G','H',
                 'I','J','K','L','M','N','O','P',
                 'Q','R','S','T','U','V','W','X',
                 'Y','Z','a','b','c','d','e','f',
                 'g','h','i','j','k','l','m','n',
                 'o','p','q','r','s','t','u','v',
                 'w','x','y','z','0','1','2','3',
                 '4','5','6','7','8','9','+','/'
              );

              var reverseBase64Chars = new Array();
              for (var i=0; i < base64Chars.length; i++){
                 reverseBase64Chars[base64Chars[i]] = i;
              }

              function decodeBase64(str){
                 setBase64Str(str);
                 var result = "";
                 var inBuffer = new Array(4);
                 var done = false;
                 while (!done && (inBuffer[0] = readReverseBase64()) != END_OF_INPUT
                    && (inBuffer[1] = readReverseBase64()) != END_OF_INPUT){
                    inBuffer[2] = readReverseBase64();
                    inBuffer[3] = readReverseBase64();
                    result += ntos((((inBuffer[0] << 2) & 0xff)| inBuffer[1] >> 4));
                    if (inBuffer[2] != END_OF_INPUT){
                       result +=  ntos((((inBuffer[1] << 4) & 0xff)| inBuffer[2] >> 2));
                       if (inBuffer[3] != END_OF_INPUT){
                          result +=  ntos((((inBuffer[2] << 6)  & 0xff) | inBuffer[3]));
                       } else {
                          done = true;
                       }
                    } else {
                       done = true;
                    }
                 }
                 return result;
              }

              function setBase64Str(str){
                 base64Str = str;
                 base64Count = 0;
              }

              function readReverseBase64(){ 
                 if (!base64Str) return END_OF_INPUT;
                 while (true){    
                    if (base64Count >= base64Str.length) return END_OF_INPUT;
                    var nextCharacter = base64Str.charAt(base64Count);
                    base64Count++;
                    if (reverseBase64Chars[nextCharacter]){
                       return reverseBase64Chars[nextCharacter];
                    }
                    if (nextCharacter == 'A') return 0;
                 }
                 return END_OF_INPUT;
              }

              function ntos(n){
                 n=n.toString(16);
                 if (n.length == 1) n="0"+n;
                 n="%"+n;
                 return unescape(n);
              }

              var myString = app.activeDocument.metadataPreferences.getProperty("http://ns.adobe.com/xap/1.0/","Thumbnails/*[1]/xmpGImg:image");
              myString = myString.replace("&#xA;", "\n");
              jpeg_string = decodeBase64 (myString);
              jpeg_file = new File (file_name);
              jpeg_file.encoding = 'binary';
              jpeg_file.open ('w');
              jpeg_file.write (jpeg_string);
              jpeg_file.close();
              • 4. Re: Extract indesign preview
                Thomas B. Nielsen Level 3
                THANK YOU :D

                Once more you saved the day Peter, I have tried and tried, but I guess the converter i used just wasn't working (Did try different ones, but not the one you have used, which i did not find). Perhaps something else was wrong, but now it works.

                I'm a very happy man now, hate to give up on an idea.

                /Thomas
                • 5. Re: Extract indesign preview
                  jaymsparks Level 1

                  I think this line:

                  var myString = app.activeDocument.metadataPreferences.getProperty("http://ns.adobe.com/xap/1.0 /","Thumbnails/*[1]/xmpGImg:image");

                  should read:

                  var myString = app.activeDocument.metadataPreferences.getProperty("http://ns.adobe.com/xap/1.0/","Thumbnails/*[1]/xapGImg:image");

                   

                  I couldn't get the script to work otherwise.

                  • 6. Re: Extract indesign preview
                    Peter Kahrel Adobe Community Professional & MVP

                    Thanks.

                     

                    Peter

                    • 7. Re: Extract indesign preview
                      Li3149 Level 1

                      nice work.

                      i got it running on CS3.

                       

                      some has tested it on CS3 server yet ?

                      • 8. Re: Extract indesign preview
                        [Jongware] Most Valuable Participant

                        The only possible problem I see in that is that ID Server doesn't have an 'activeDocument' property (and alternatives for that are well documented).

                         

                        Or ... can't you set the Preferences to save image previews? I can see how that might be a problem.

                        • 9. Re: Extract indesign preview
                          Li3149 Level 1

                          the 'activeDocument' hav i replaced with "app.documents.item(0)" and seems to work but not with the xmp.

                           

                          "var myString = app.documents.item(0).metadataPreferences.getProperty("http://ns.adobe.com/xap/1.0/","Thumbnails/*[1]/xapGImg:image");" gives nothing back.

                           

                          is there another way to get a preview in ID CS3 Server ?

                          • 10. Re: Extract indesign preview
                            [Jongware] Most Valuable Participant

                            Is there actually any kind of info in these files? It might be a problem to check since IDS lacks a proper UI, but on a regular CS3 I can check File -> File Info -> (Huge Dialog) -> Advanced (in the list on the left) -> Save (on the bottom). The resulting XML file contains all kinds of stuff, among which:

                             

                                     <xap:Thumbnails>
                                        <rdf:Alt>
                                           <rdf:li rdf:parseType="Resource">
                                              <xapGImg:format>JPEG</xapGImg:format>
                                              <xapGImg:width>256</xapGImg:width>
                                              <xapGImg:height>256</xapGImg:height>
                                              <xapGImg:image>/9j/ .. (ten billion meaningless characters omitted) .. Yq//Z</xapGImg:image>
                                           </rdf:li>
                                        </rdf:Alt>
                                     </xap:Thumbnails>

                            and the red marked line is what the script retrieves. (I have no idea how that magical XML query locates this. It's in the middle of 433 lines of XML, plus an additional 2K (exact!) of whitespace.)

                            • 11. Re: Extract indesign preview
                              Dirk Becker  Level 4

                              The extra 2k of white space are meant as padding, so that external tools can add their fields without changing the overall file structure.

                               

                              Dirk

                              • 12. Re: Extract indesign preview
                                [Jongware] Most Valuable Participant

                                Of course... I assumed it the other way around: padding up to a certain size. That's why I was struck by it being exactly 2K (counting both spaces and returns).

                                • 13. Re: Extract indesign preview
                                  Li3149 Level 1

                                  the file was ok. only the server was set on [-nopreviews], thats the problem why i didnt get any data back.

                                   

                                  and here i got another problem.

                                  the preview size:

                                  <xapGImg:width>256</xapGImg:width>

                                  <xapGImg:height>256</xapGImg:height>

                                  is a bit too small.

                                   

                                  you can use the "setProperty" command to resize it. Like this

                                  ...metadataPreferences.setProperty("http://ns.adobe.com/xap/1.0/","Thumbnails/*[1]/xmpGImg:width","1024");

                                   

                                  but IDSCS4 sends a preview jpg back with format "256 x 256", when i use "getProperty("http://ns.adobe.com/xap/1.0/","Thumbnails/*[1]/xmpGImg:image")" to get the preview.

                                   

                                  Is there a way to set the preview size in IDSCS4 ?

                                  • 14. Re: Extract indesign preview
                                    Thomas B. Nielsen Level 3

                                    In Indesign CS3 i did:

                                    with (app.generalPreferences) {
                                        includePreview = true;
                                        previewSize = PreviewSizeOptions.large;
                                    }

                                     

                                    Perhaps it's the same for IDS CS4

                                     

                                    --

                                     

                                    Thomas B. Nielsen

                                    http://www.lund-co.dk

                                    • 15. Re: Extract indesign preview
                                      Li3149 Level 1

                                      the property "previewSize = PreviewSizeOptions.large" works well.

                                       

                                      thx for help Thomas.