1

Limited to using Extendscript in Photoshop I'm trying to write and then read in the same binary file. I can write the file okay, but I'm not sure where I'm going wrong with the read part.

The data will be RGB colours in hex, so I'll either want to return the data from the read function as array or a string. Only I can't even get it to tell me the file just written exists. And I'm not sure if I should be using seek() or read(). Confused.

var f = new File("D:\\temp\\bin.act");

var w = write_binary(f);
var r = read_binary(w); 

alert(r);

function write_binary(afile)
{
  afile.encoding = "BINARY";
  afile.open ("w");

  for(i = 0; i < 256; i++)
  {
    afile.write(String.fromCharCode (i));
  }

  afile.close();
}


function read_binary(afile)
{
  var f = new File(afile);
  f.open("r");
  f.encoding = "BINARY";

  //var data = f.read();
  //if(f.exists) alert(afile);
  //alert (data);

  var arr = [];
  for (var i = 0; i < f.length; i+=4)
  {
    f.seek(i, 0);
    var hex = f.readch().charCodeAt(0).toString(16);
    if(hex.length === 1) hex = "0" + hex;
    arr.push(hex);
  }
  return arr;
}
RobC
  • 22,977
  • 20
  • 73
  • 80
Ghoul Fool
  • 6,249
  • 10
  • 67
  • 125
  • Looks like you are not returning the file from your write function, but then still want to save it into the var `w`, that you then in turn want to use in the read function. That won't work, because `w` will just be undefined. – mdomino Jul 21 '20 at 21:19
  • @mdomino I see where I've gone wrong now. Thank you for pointing that out. – Ghoul Fool Jul 22 '20 at 08:37
  • Hi, sorry about a question, but I was wondering, how do you approach somethibg like this? I'm also an artist and I have no idea where even to start with binary reading. Say I want to extract a png of a particular brush from an abr file. How fo I learn how to do that..? – Sergey Kritskiy Aug 01 '20 at 07:53
  • When I found out, I'll let you know. @SergeyKritskiy there used to be several utilities to convert abr to png/tiff abrMate was one of them. – Ghoul Fool Aug 01 '20 at 09:21

1 Answers1

0

You can read it like this:

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

var hexstring=""
while (true){
    var b = f.readch().charCodeAt(0);
    if(f.eof){break;}
    var hex = b.toString(16);
    if(hex.length === 1) hex = "0" + hex;
    hexstring += hex;
}
alert(hexstring);

The corresponding writing part of this answer is here.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432