使用Photoshop脚本(.jsx)附加到现有文本文件

Append to an existing text file using Photoshop Scripting ( .jsx )

本文关键字:文本 文件 脚本 Photoshop jsx 使用      更新时间:2023-09-26

我一直找不到任何类似于我的小众案例的东西。

我有很多.png文件,所有这些文件都有一个矩形或正方形的透明。我已经创建了一个脚本,它可以找到边界,并将这些信息写入一个文本文件。

目前的行为是,脚本为每个图像创建一个单独的文本文件,并将我需要的信息写入该文件。

目前的代码如下:

//Create logfile FOLDER on the desktop
var LogFolder = new Folder(Folder.desktop + "/LogFiles/");
if(!LogFolder.exists) LogFolder.create();
//NOTE TO SELF: Would be optimal if appended to single log file
//Create new LOGFILE in the folder using image name
var Loginfo = new File(Folder.desktop + "/LogFiles/" + activeDocument.name.replace(/'.[^'.]+$/, '') + ".txt");
Loginfo.open("w", "TEXT");
//Write the info to the file
Loginfo.write(activeDocument.name.replace(/'.[^'.]+$/, '') + ", " + selectionWidth + ", " + selectionHeight + ", " + selectionTopLeftXOffset + ", " + selectionTopLeftYOffset);
//Close the log
Loginfo.close();

我已经开始处理它了,但没能把它附加到一个文件:

//Create logfile FOLDER on the desktop
var LogFolder = new Folder(Folder.desktop + "/LogFiles/");
if(!LogFolder.exists) LogFolder.create();
//Append to LOGFILE
var Loginfo = new File(Folder.desktop + "/LogFiles/" + "coords.txt");
Loginfo.open("w", "TEXT");
//Write the info to the file
Loginfo.write(activeDocument.name.replace(/'.[^'.]+$/, '') + ", " + selectionWidth + ", " + selectionHeight + ", " + selectionTopLeftXOffset + ", " + selectionTopLeftYOffset + "'r");
//Close the log
Loginfo.close();

附加到单个文件将使创建文件后的工作变得更加容易。如有任何帮助,我们将不胜感激。

您当前正在"写入"文件,而不是"追加"。

  • //open()方法
  • fileExample.open("w");//写入
  • fileExample.open("e");//编辑
  • fileExample.open("a");//追加

您应该能够更改

Loginfo.open("w", "TEXT");

Loginfo.open("a", "TEXT");