Writing binary file using in Extendscript. Incorrect file size

一个人想着一个人 提交于 2020-08-05 10:06:06

问题


Further to my question here I'm writing a list of hex colours to a binary file from within Photoshop using Extendscript. So far so good.

Only the binary file written with the code below is 119 bytes. When cut and pasted and saved using Sublime Text 3 it's only 48 bytes, which then causes complications later on.

This is my first time in binary land, so I may be a little lost. I suspect it's an either an encoding issue (which could explain the 2.5 file size), or doing something very wrong trying to recreate the file in a literal, character for character sense. *

// Initially, my data is a an array of strings
var myArray = [
"1a2b3c",
"4d5e6f",
"a10000",
"700000",
"d10101",
"dc0202",
"c30202",
"de0b0b",
"d91515",
"f06060",
"fbbaba",
"ffeeee",
"303030",
"000000",
"000000",
"000000"
]

// I then separate them to four character chunks
// in groups of 8
var data = "1a2b 3c4d 5e6f a100 0070 0000 d101 01dc\n" + 
"0202 c302 02de 0b0b d915 15f0 6060 fbba\n" +
"baff eeee 3030 3000 0000 0000 0000 0000";

var afile = "D:\\temp\\bin.act"

var f = new File(afile);
f.encoding = "BINARY";
f.open ("w");
// f.write(data);

// amended code
for (var i = 0; i < data.length; i++)
{
   var bytes = String.fromCharCode(data.charCodeAt(i));
   f.write(bytes);   
}

f.close();
alert("Written " + afile);

* ...or it's the tracking on my VHS.


回答1:


I'm rubbish at JavaScript but I have hacked something together that will show you how to write 3 bytes of hex to a file in binary. I hope it is enough for you to work out how to do the rest!

I saved this file as /Users/mark/StackOverflow/AdobeJavascript.jsx

alert("Starting");

// Open binary file
var afile = "/Users/mark/StackOverflow/data.bin"
var f = new File(afile);
f.encoding = "BINARY";
f.open ("w");

// Define hex string
str = "1a2b3c"
for(offset=0;offset<str.length;offset+=2) {
   i = parseInt(str.substring(offset, offset+2), 16)
   f.write(String.fromCharCode(i));
}

f.close();
alert("Done");

If you dump the data.bin you'll see 3 bytes:

xxd data.bin
00000000: 1a2b 3c

You can write more of your values by simply changing the string to:

str = "1a2b3c"+ "4d5e6f"+ "a10000";

I also discovered how to run ExtendScript from a shell script in Terminal which is my "happy place" so I'll add that in here for my own reference:

#!/bin/bash

osascript << EOF
tell application "Adobe Photoshop CC 2019"
    do javascript "#include /Users/mark/StackOverflow/AdobeJavascript.jsx"
end tell
EOF

The corresponding reading part of this answer is here.



来源:https://stackoverflow.com/questions/63173144/writing-binary-file-using-in-extendscript-incorrect-file-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!