Question
Example of Image to Text Extendscript code
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.
// extendscript to copy image to text for use in storing info within the jsx file
// RONC 03 Mar 2020
// image2txt.jsx
/**
* @@@BUILDINFO@@@ image2txt.jsx !Version! Tue Mar 03 2020 05:52:11 GMT-0600
*/
// Instead of using:
// 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 / rather than \ in file info !!!
// these eight parameters control the input, changes made, and output
//
var inputFilePath = "C:/Users/RECHM/Dropbox/$$-SYNC/$$-Scripts/__SC-Save/";
var inputFileName = "TestKey2";
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 = "TestKey2";
var outputFileExt = ".txt";
// read image
var infile = File(inputFilePath + inputFileName + inputFileExt);
infile.open("r");
infile.encoding = "binary";
var temp = infile.read();
infile.close();
if (temp.length === 0)
{
alert("input error");
}
// output converted to txt
var outfile = File(inputFilePath + "work" + ".txt");
outfile.open("w");
outfile.write(temp.toSource());
outfile.close();
// read work file
var infile2 = File(inputFilePath + "work" + ".txt");
infile2.open("r");
infile2.encoding = "binary";
temp = infile2.read();
infile2.close();
infile2.remove();
// remove beginning and trailing characters
var temp2 = temp.replace("(new String(", varText);
temp = temp2.replace("))", varEnd);
// output to final txt file ready for copying to jsx code
var outfile2 = File(outputFilePath + outputFileName + outputFileExt);
outfile2.open("w");
outfile2.write(temp);
outfile2.close();
alert("Input file:\n" + inputFilePath + inputFileName + inputFileExt + "\nwritten to Output file:\n" + outputFilePath + outputFileName + outputFileExt);
Please comment.
RONC
