Discussion:
Extract indesign preview
(too old to reply)
T***@adobeforums.com
2009-01-19 09:35:33 UTC
Permalink
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?
T***@adobeforums.com
2009-01-19 13:57:49 UTC
Permalink
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?
T***@adobeforums.com
2009-01-19 16:35:20 UTC
Permalink
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("&#xA;", "\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>
Peter Kahrel
2009-01-19 17:35:45 UTC
Permalink
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();
T***@adobeforums.com
2009-01-19 17:53:46 UTC
Permalink
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

Loading...