如何通过Photoshop JavaScript将文本写入文本文件?

tor*_*rus 11 javascript photoshop photoshop-script

我看了一下Photoshop CS5脚本指南和Photoshop CS5 JavaScript参考,但我找不到将文本写入纯文本文件的方法.有没有办法做到这一点?

我想记录bounds文档中每个图层对象的值.

任何提示?

psy*_*brm 16

这适用于我,保存与原始文档同名的文本,但扩展名为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();
}
Run Code Online (Sandbox Code Playgroud)

我不知道它是如何工作的,photoshop脚本是一个巨大的混乱,我只是将我发现的一些脚本混合在一起直到它工作.

此外,如果有人需要这个,这里有一个脚本,也将活动文档保存为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);
}
Run Code Online (Sandbox Code Playgroud)


nob*_*ody 7

Adobe的JavaScript工具指南(PDF)中记录了文件系统访问权限.

下载PDF文件并查看"文件系统访问"部分.

  • 源代码值得成千上万的文档. (5认同)

Gho*_*ool 6

这是您需要的: 这是非常基本的。它将遍历图层(没有图层集!!)并保存每个图层的图层名称和图层边界。

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();
}
Run Code Online (Sandbox Code Playgroud)


cor*_*n_m 5

我发现缺少文档,但想出了这个作为在 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();
Run Code Online (Sandbox Code Playgroud)


Mar*_*jaš 5

我已阅读文档并结合了psycho brm 和corrin_m 的答案的最佳部分。
我的回答还会检查错误。

如果文件存在,则没有必要删除它,因为用“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 "";
}
Run Code Online (Sandbox Code Playgroud)

这就是我在脚本中使用该函数的方式

error = saveAsTextFile(filePath, content);
if (error === "") {
  alert(filePath + " saved OK.");
}
else {
  alert("Error saving " + filePath + "\n" + error);
}
Run Code Online (Sandbox Code Playgroud)

顺便说一句,我将它保存在名为 common-code.jsx 的单独文件中,我可以将其包含在以下行中(单行注释是有意的)。

// @include 'common-code.jsx'
Run Code Online (Sandbox Code Playgroud)