Copy link to clipboard
Copied
Hello!
I am trying return byte arrays or binary buffers from Indesign Server so that I can hit the server requesting e.g. a certain JPG, and have that JPG returned in a buffer without any intermediary service.
So far, I have found that after exporting a file object, I can read it back into the server using e.g:
var file = new File('C:/Users/.../Desktop/file_example_JPG_100kB.jpg')
file.encoding = "Binary"
file.open("r");
var arr = [];
for (var i = 0; i < file.length; i+=4) {
file.seek(i, 0);
arr.push(file.read(4))
}
This will produce an array of unicode characters.
I am having trouble, however, turning those unicode characters into a bytestring which I can save out to a buffer, and eventually to a valid JPG on the other side.
I believe the issue has to do with this note on how the binary reader works
cc Jongware / https://estk.aenhancers.com/3%20-%20File%20System%20Access/file-object.html
"A special encoder, BINARY, is used to read binary files. It stores each byte of the file as one Unicode character regardless of any encoding. When writing, the lower byte of each Unicode character is treated as a single byte to write."
Has anyone been able to implement the removal of all but the lowest byte from each Unicode char that the Extendscript File reader reads in?
What's with that array and the loop and 'push'? To read an entire (binary) file, this is enough:
data = file.read();
and after that, 'data' will contain the correct bytes. It's just that if you want to "do" something with these bytes, they might need to be converted to Unicode, because that's what JavaScript uses internally, and lots of its functions expect the input to be a Unicode string.
But if you only want to use it to check something (? your requirements are a bit unclear there), t
...Copy link to clipboard
Copied
In your case, you can use readch method like below.
function readBin(fl){ //fl as File object
fl.encoding = 'BINARY';
if (!fl.open("r")) return;
var bn = [];
while(!fl.eof) bn.push(fl.readch().charCodeAt(0).toString(16));
return bn;
}
$.writeln(readBin (File.openDialog("")));
Copy link to clipboard
Copied
What's with that array and the loop and 'push'? To read an entire (binary) file, this is enough:
data = file.read();
and after that, 'data' will contain the correct bytes. It's just that if you want to "do" something with these bytes, they might need to be converted to Unicode, because that's what JavaScript uses internally, and lots of its functions expect the input to be a Unicode string.
But if you only want to use it to check something (? your requirements are a bit unclear there), then this is enough.
You can see for yourself if you write that same data out to a new file; the new file will be exactly the same as the original, binary or otherwise.
Here is a fragment of a script of mine that reads MathType that may be embedded inside an image:
var file = File (filename);
file.open();
file.encoding = "binary";
var mfData = file.read();
file.close();
// perhaps it's a GIF?
if (mfData.substr(0,6) == 'GIF89a')
{
var pos = 6;
// skip Logical Screen Descriptor but save Global Color byte
var GCB = mfData.byteValueAt(pos+4);
...
where I added a new function
String.prototype.byteValueAt = function(index)
{
index = index || 0;
return this.charCodeAt(index);
}
to make sure JS does not try to treat the data as characters, but exclusively as a list of bytes.
(Newer versions of JavaScript don't have this problem as they do have a native Byte type. Alas, Adobe lags behind enourmously in updating JavaScript.
> The Legacy ExtendScript expression engine is based on ECMAScript 3 (1999).
(https://helpx.adobe.com/after-effects/using/legacy-and-extend-script-engine.html)
My proposal is to finally ditch it and replace with Python 🙂 See https://indesign.uservoice.com/forums/601021-adobe-indesign-feature-requests/suggestions/32193772-ad..., and vote if you like. My "almost 2 decades old" has been surpassed, it is 20 years old now.)
(Also, thanks for the mention but those pages are not mine. You may have originally been looking at http://jongware.mit.edu/idcs6js/pc_File.html)
Copy link to clipboard
Copied
Dude, you are a LEGEND!!! This still hasn't been updated (at least for Audition)