Question
Example of Image to Text ExtendScript code (Revised 06 Mar 2020)
The Extendscript code below will take a png file and output it as a txt file for you to copy the contents into your Scriptui code where an image is desired.
This is a revision of https://community.adobe.com/t5/photoshop/example-of-image-to-text-extendscript-code/m-p/10965512?page=1
****** See below for more automated version by geppettol66959005 ******
// extendscript to copy image to text for use in storing info within the jsx file
// RONC 06 Mar 2020
// image2txt2.jsx
/**
* @@@BUILDINFO@@@ image2txt2.jsx !Version! Fri Mar 06 2020 03:31:42 GMT-0600
*/
/* revision of previous post with use of suggestion by r-bin */
/* scriptui options image and iconbutton use image input which is most often from and image
file. It is also possible to use an embedded version of the image data as part of those options
this script is designed to assist in creating the file of text to be embedded. Once the file
is created, this new file can be opened in a text editor like Notepad in Windows and then using
a simple copy/paste operation to put the file information into the source code of the the script
where the embed is desired. Ctrl-A followed by Ctrl-C will copy the information in the text
editor and Ctrl-V will paste into the source code.
*/
// Instead of using file name in image or iconbutton scriptui calls.:
// var iconFile = new File ("C:/Users/RECHM/Dropbox/$$-SYNC/$$-Scripts/__SC-Save/TestKey2.png");
// Use:
// Contents of the output txt file which is var iconFile = "... ....";
// Must use either / or \\ rather than \ in file info !!!
// these eight parameters control the input, changes made, and output
//
var ierr = 0;
var inputFilePath = "C:/Users/RECHM/Dropbox/$$-SYNC/$$-Scripts/__SC-Save/";
var inputFileName = "ui-bg_diagonals-thick_18_b81900_40x40RC8";
var inputFileExt = ".png";
var varText = "var iconFile = "; // set to "" if not desiring the jsx info
var varEnd = ";"; // set to "" if not desiring the jsx info
var outputFilePath = "C:/Users/RECHM/Dropbox/$$-SYNC/$$-Scripts/__SC-Save/";
var outputFileName = "ui-bg_diagonals-thick_18_b81900_40x40RC8_3x";
var outputFileExt = ".txt";
var i;
// read image
var infile = File(inputFilePath + inputFileName + inputFileExt);
infile.open("r");
infile.encoding = "binary";
var temp = infile.read();
infile.close();
// check input if OK create output
var len = temp.length;
if (len === 0)
{
alert("input file error - zero length");
ierr = 1;
}
else
{
var temp2 = "var iconFile = String.fromCharCode(";
for (i = 0; i < len; i++)
{
temp2 += (i ? "," : "") + temp.charCodeAt(i);
}
temp2 += ");\n";
// output converted to txt
var outfile = File(outputFilePath + outputFileName + outputFileExt);
outfile.open("w");
outfile.write(temp2);
outfile.close();
ierr = 0;
}
if (ierr === 0)
{
alert("Input file:\n" + inputFilePath + inputFileName + inputFileExt + "\n\nwritten to Output file:\n" + outputFilePath + outputFileName + outputFileExt);
}
RONC
