如何使用 Photoshop JavaScript 将文本写入文本文件?

发布于 2024-11-17 00:53:42 字数 240 浏览 5 评论 0原文

我查看了 Photoshop CS5 脚本指南和 Photoshop CS5 JavaScript 参考,但我找不到将文本写入纯文本文件的方法。有什么办法可以做到这一点吗?

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

有什么提示吗?

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?

I want to record the value of bounds of each layer object in a document.

Any hint?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

傲影 2024-11-24 00:53:42

这对我有用,保存与原始文档同名的文本,但扩展名 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();
}

我不知道它是如何工作的,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);
}

This works for me, saves text with the same name as original document, but with extension 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.

Also, if anyone needs this, here is a script that also saves active document as png image:

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);
}
救赎№ 2024-11-24 00:53:42

我已阅读文档并结合了 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 "";
}

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

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

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

// @include 'common-code.jsx'

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 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 common-code.jsx and I can include it with following line (single line comments are intentional).

// @include 'common-code.jsx'
不忘初心 2024-11-24 00:53:42

文件系统访问记录在 Adob​​e 的 JavaScript 工具指南中(PDF)

下载 PDF 文件并查看“文件系统访问”部分。

File system access is documented in Adobe's JavaScript Tools Guide (PDF).

Download the PDF file and check out the "File System Access" section.

眉黛浅 2024-11-24 00:53:42

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

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();
}

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();
}
梦途 2024-11-24 00:53:42

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文