I took a look at Photoshop CS5 Scripting Guide and Photoshop CS5 JavaScript Reference, but I couldn't find out a method to write text to a plain text file. Is there any way to do that?
我查看了Photoshop CS5脚本指南和Photoshop CS5 JavaScript参考,但是我找不到一个方法将文本写入纯文本文件。有什么办法吗?
I want to record the value of bounds
of each layer object in a document.
我想记录文档中每个层对象的界限值。
Any hint?
有提示吗?
5 个解决方案
#1
7
File system access is documented in Adobe's JavaScript Tools Guide (PDF).
文件系统访问被记录在Adobe的JavaScript工具指南(PDF)中。
Download the PDF file and check out the "File System Access" section.
下载PDF文件并查看“文件系统访问”部分。
#2
14
This works for me, saves text with the same name as original document, but with extension txt
:
这对我来说是可行的,保存与原始文档同名的文本,但是扩展名txt:
function saveTxt(txt)
{
var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');
var Ext = decodeURI(app.activeDocument.name).replace(/^.*\./,'');
if (Ext.toLowerCase() != 'psd')
return;
var Path = 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.
我不知道它是如何工作的,photoshop脚本是一个巨大的混乱,我只是不断地混合一些我发现的脚本,直到它起作用。
Also, if anyone needs this, here is a script that also saves active document as png
image:
另外,如果有人需要这个,这里有一个脚本,它也可以将活动文档保存为png图像:
function savePng()
{
var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');
var Ext = decodeURI(app.activeDocument.name).replace(/^.*\./,'');
if (Ext.toLowerCase() != 'psd')
return;
var Path = app.activeDocument.path;
var saveFile = File(Path + "/" + Name +".png");
if(saveFile.exists)
saveFile.remove();
var o = new ExportOptionsSaveForWeb();
o.format = SaveDocumentType.PNG;
o.PNG8 = false;
o.transparency = true;
o.interlaced = false;
o.includeProfile = false;
activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, o);
}
#3
4
I found the docs lacking but came up with this as a method to create and write to a new file in CS6:
我发现文档中缺少文档,但我将它作为创建和写入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();
#4
3
I have read the docs and combined best parts of psycho brm's and corrin_m's answer.
MY ANSWER ALSO CHECKS FOR ERRORS.
我读过《精神病人brm》和《corrin_m的答案》的文章,并将它们的精华部分结合在一起。我的回答也检查错误。
It is not necessary to delete file if it exists because opening with "w" will overwrite existing file it.
如果文件存在,则不需要删除文件,因为打开“w”会覆盖现有的文件。
/* =======================================================
* Saves file as text. Overwrites old file if exists.
* Returns empty string if no errors, otherwise error message.
* =======================================================*/
function saveAsTextFile(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 commom-code.jsx and I can include it with following line (single line comments are intentional).
顺便说一句,我把它保存在一个名为commom-code的单独文件中。jsx和我可以将它包含在以下行中(单行注释是有意的)。
// @include 'common-code.jsx'
#5
2
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;
var srcDoc = app.activeDocument;
var numOfLayers = srcDoc.layers.length;
var results = "";
var fileName = srcDoc.name;
var docName = fileName.substring(0,fileName.length -4)
var theFile = srcDoc.path + "/" + docName + ".txt";
for (var i = 0; i < numOfLayers ; i++)
{
var theLayer = srcDoc.layers[i];
var lb = getLayerBounds(theLayer).toString();
results += theLayer.name + ": " + lb + "\n";
}
writeTextFile(theFile, results)
alert(results);
function getLayerBounds(alayer)
{
var x1 = parseFloat(alayer.bounds[0])
var y1 = parseFloat(alayer.bounds[1])
var x2 = parseFloat(alayer.bounds[2])
var y2 = parseFloat(alayer.bounds[3])
return [x1,y1,x2,y2]
}
function writeTextFile(afilename, output)
{
var txtFile = new File(afilename);
txtFile.open("w"); //
txtFile.writeln(output);
txtFile.close();
}
#1
7
File system access is documented in Adobe's JavaScript Tools Guide (PDF).
文件系统访问被记录在Adobe的JavaScript工具指南(PDF)中。
Download the PDF file and check out the "File System Access" section.
下载PDF文件并查看“文件系统访问”部分。
#2
14
This works for me, saves text with the same name as original document, but with extension txt
:
这对我来说是可行的,保存与原始文档同名的文本,但是扩展名txt:
function saveTxt(txt)
{
var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');
var Ext = decodeURI(app.activeDocument.name).replace(/^.*\./,'');
if (Ext.toLowerCase() != 'psd')
return;
var Path = 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.
我不知道它是如何工作的,photoshop脚本是一个巨大的混乱,我只是不断地混合一些我发现的脚本,直到它起作用。
Also, if anyone needs this, here is a script that also saves active document as png
image:
另外,如果有人需要这个,这里有一个脚本,它也可以将活动文档保存为png图像:
function savePng()
{
var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');
var Ext = decodeURI(app.activeDocument.name).replace(/^.*\./,'');
if (Ext.toLowerCase() != 'psd')
return;
var Path = app.activeDocument.path;
var saveFile = File(Path + "/" + Name +".png");
if(saveFile.exists)
saveFile.remove();
var o = new ExportOptionsSaveForWeb();
o.format = SaveDocumentType.PNG;
o.PNG8 = false;
o.transparency = true;
o.interlaced = false;
o.includeProfile = false;
activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, o);
}
#3
4
I found the docs lacking but came up with this as a method to create and write to a new file in CS6:
我发现文档中缺少文档,但我将它作为创建和写入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();
#4
3
I have read the docs and combined best parts of psycho brm's and corrin_m's answer.
MY ANSWER ALSO CHECKS FOR ERRORS.
我读过《精神病人brm》和《corrin_m的答案》的文章,并将它们的精华部分结合在一起。我的回答也检查错误。
It is not necessary to delete file if it exists because opening with "w" will overwrite existing file it.
如果文件存在,则不需要删除文件,因为打开“w”会覆盖现有的文件。
/* =======================================================
* Saves file as text. Overwrites old file if exists.
* Returns empty string if no errors, otherwise error message.
* =======================================================*/
function saveAsTextFile(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 commom-code.jsx and I can include it with following line (single line comments are intentional).
顺便说一句,我把它保存在一个名为commom-code的单独文件中。jsx和我可以将它包含在以下行中(单行注释是有意的)。
// @include 'common-code.jsx'
#5
2
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;
var srcDoc = app.activeDocument;
var numOfLayers = srcDoc.layers.length;
var results = "";
var fileName = srcDoc.name;
var docName = fileName.substring(0,fileName.length -4)
var theFile = srcDoc.path + "/" + docName + ".txt";
for (var i = 0; i < numOfLayers ; i++)
{
var theLayer = srcDoc.layers[i];
var lb = getLayerBounds(theLayer).toString();
results += theLayer.name + ": " + lb + "\n";
}
writeTextFile(theFile, results)
alert(results);
function getLayerBounds(alayer)
{
var x1 = parseFloat(alayer.bounds[0])
var y1 = parseFloat(alayer.bounds[1])
var x2 = parseFloat(alayer.bounds[2])
var y2 = parseFloat(alayer.bounds[3])
return [x1,y1,x2,y2]
}
function writeTextFile(afilename, output)
{
var txtFile = new File(afilename);
txtFile.open("w"); //
txtFile.writeln(output);
txtFile.close();
}