在Extendscript中使用编写二进制文件.档案大小不正确 [英] Writing binary file using in Extendscript. Incorrect file size

查看:127
本文介绍了在Extendscript中使用编写二进制文件.档案大小不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的问题的进一步说明此处我正在编写一个十六进制颜色列表使用Extendscript在Photoshop中创建一个二进制文件.到目前为止一切顺利.

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.

仅使用下面的代码编写的二进制文件是119个字节.使用Sublime Text 3剪切和粘贴并保存时,只有48个字节,这会在以后引起复杂性.

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.

这是我第一次进入二元土地,所以我可能会有点迷路.我怀疑这是编码问题(可以解释2.5文件大小),还是尝试以字面意义重新创建文件(出于字符意义)而做的事情非常错误. *

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);

* ...或者是我的VHS上的跟踪.

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

推荐答案

我不喜欢JavaScript,但是我一起破解了一些东西,将向您展示如何向二进制文件中写入3字节的十六进制.我希望您足以解决剩下的问题!

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!

我将此文件另存为/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");


如果转储data.bin,您将看到3个字节:


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";


我还发现了如何在Terminal的shell脚本中运行ExtendScript,这是我的快乐的地方" ,因此,我将在此处添加该消息供自己参考:


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


此答案的相应阅读部分是此处.

这篇关于在Extendscript中使用编写二进制文件.档案大小不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆