How To Write Text To A Text File By Photoshop Javascript?
Solution 1:
This works for me, saves text with the same name as original document, but with extension txt
:
functionsaveTxt(txt)
{
varName = app.activeDocument.name.replace(/\.[^\.]+$/, '');
varExt = decodeURI(app.activeDocument.name).replace(/^.*\./,'');
if (Ext.toLowerCase() != 'psd')
return;
varPath = app.activeDocument.path;
var saveFile = File(Path + "/" + Name +".txt");
if(saveFile.exists)
saveFile.remove();
saveFile.encoding = "UTF8";
saveFile.open("e", "TEXT", "????");
saveFile.writeln(txt);
saveFile.close();
}
I don't know how it works, photoshop scripting is a huge mess, I just kept mixing together a few scripts that I found until it worked.
Also, if anyone needs this, here is a script that also saves active document as png
image:
functionsavePng()
{
varName = app.activeDocument.name.replace(/\.[^\.]+$/, '');
varExt = decodeURI(app.activeDocument.name).replace(/^.*\./,'');
if (Ext.toLowerCase() != 'psd')
return;
varPath = app.activeDocument.path;
var saveFile = File(Path + "/" + Name +".png");
if(saveFile.exists)
saveFile.remove();
var o = newExportOptionsSaveForWeb();
o.format = SaveDocumentType.PNG;
o.PNG8 = false;
o.transparency = true;
o.interlaced = false;
o.includeProfile = false;
activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, o);
}
Solution 2:
File system access is documented in Adobe's JavaScript Tools Guide (PDF).
Download the PDF file and check out the "File System Access" section.
Solution 3:
Here's what you need: It's pretty basic. It'll loop over the layers (no layersets!!) and save out the layer name and the layer bounds for each layer.
app.preferences.rulerUnits = Units.PIXELS;
varsrcDoc= app.activeDocument;
varnumOfLayers= srcDoc.layers.length;
varresults="";
varfileName= srcDoc.name;
vardocName= fileName.substring(0,fileName.length -4)
vartheFile= srcDoc.path + "/" + docName + ".txt";
for (vari=0; i < numOfLayers ; i++)
{
vartheLayer= srcDoc.layers[i];
varlb= getLayerBounds(theLayer).toString();
results += theLayer.name + ": " + lb + "\n";
}
writeTextFile(theFile, results)
alert(results);
function getLayerBounds(alayer)
{
varx1= parseFloat(alayer.bounds[0])
vary1= parseFloat(alayer.bounds[1])
varx2= parseFloat(alayer.bounds[2])
vary2= parseFloat(alayer.bounds[3])
return [x1,y1,x2,y2]
}
function writeTextFile(afilename, output)
{
vartxtFile=newFile(afilename);
txtFile.open("w"); //
txtFile.writeln(output);
txtFile.close();
}
Solution 4:
I have read the docs and combined best parts of psycho brm's and corrin_m's answer. MY ANSWER ALSO CHECKS FOR ERRORS.
It is not necessary to delete file if it exists because opening with "w" will overwrite existing file it.
/* =======================================================
* Saves file as text. Overwrites old file if exists.
* Returns empty stringif no errors, otherwise error message.
* =======================================================*/
functionsaveAsTextFile(filePath, content) {
var saveFile = new File(filePath);
saveFile.encoding = "UTF8";
saveFile.open("w");
if (saveFile.error != "")
return saveFile.error;
saveFile.write(content);
if (saveFile.error != "")
return saveFile.error;
saveFile.close();
if (saveFile.error != "")
return saveFile.error;
return"";
}
This is how I am using the function in my scripts
error = saveAsTextFile(filePath, content);
if (error === "") {
alert(filePath + " saved OK.");
}
else {
alert("Error saving " + filePath + "\n" + error);
}
BTW I am keeping this in separate file called common-code.jsx and I can include it with following line (single line comments are intentional).
// @include'common-code.jsx'
Solution 5:
I found the docs lacking but came up with this as a method to create and write to a new file in CS6:
var s = "My string data here";
var file = new File();
var fileNew = file.saveDlg("Save new file");
fileNew.open("w");
fileNew.write(s);
fileNew.close();
Post a Comment for "How To Write Text To A Text File By Photoshop Javascript?"