Converting a binary img string to a file, then saving it?
Copy link to clipboard
Copied
Hey!
Does anyone happen to know if it's possible to save a decoded binary string into a working jpg file?
I am trying to store some icons as binary in my script in binary, but want to save them somewhere on the system and only then load them (in order to use @2X icons, which as much as I know don't work when using a binary string).
Thank you!
Copy link to clipboard
Copied
People have used hex encoding, which is supported AFAIK, but native binary data may be a stretch.
Mylenium
Copy link to clipboard
Copied
Thanks
Copy link to clipboard
Copied
I do this kind of thing using base64 encoding. For example, I googled 'base64 encoding javascript' and found this page:
https://muthu.co/code-for-base64-encoding-and-decoding-of-string-javascript/
I copied the code from that page into a new script then added the following:
var encodeFile = File.openDialog("Select the file to encode. Cancel if you want to test the encode.");
if (encodeFile != null) {
encodeFile.encoding = "BINARY";
encodeFile.open("r");
binString = encodeFile.read();
var base64string = Base64.encode(binString);
var saveName = encodeFile.name;
saveName += "_Base64.txt";
var saveFile = new File(encodeFile.parent.absoluteURI + "/" + saveName);
saveFile.open("w");
saveFile.write(base64string);
saveFile.close();
} else {
// paste the contents of the exported text file into the base64String between the quotation marks
var base64String = "";
if (base64String == "") {
alert("You need to export a base64 encoded text file first then copy the contents into the base64string in the script first");
} else {
var theImage = Base64.decode(base64String);
function buildUI() {
var pal = new Window("palette", "test", undefined, {resizeable:true});
if (pal != null) {
var res =
"group { orientation: 'column' ,alignment:['fill','fill'], \
ImageBut: IconButton { text:'', alignment:['left','center'] }, \
}, \
}";
pal.grp = pal.add(res);
pal.grp.ImageBut.image = theImage;
return pal;
}
}
var testPal = buildUI();
if (testPal != null) {
testPal.center();
testPal.show();
}
var saveFile = File.saveDialog("Save the image file?");
if (saveFile != null) {
saveFile.encoding = "BINARY";
saveFile.open("w");
saveFile.write(theImage);
saveFile.close;
}
}
}
So this example script will first ask you to locate an image file (png or jpg I believe will work) and will base64 encode it and save that to the same location as a "base64.txt" file.
Then if you copy the contents of that file into the base64String variable in the script and run it again, hitting Cancel on that first open dialog, it will create a script palette with an IconButton displaying the image and give you the option to save the image. So it's both displaying and saving the image using the encoded string that is now in the script.
I don't know if there's any limit on image file size but probably worth trying it on something small at least to start with!
Copy link to clipboard
Copied
I fiddled around with it and it seems to be working!
I'm soon diving into the UI of a script I am working on so I will have more time to look into it. if I manage to use it to create a nice workflow that imports the image at @2x properly I will post back the code so it can help more people, but so far that's a very good start.
Thank you so much for this @Paul Tuersley !

