Copy link to clipboard
Copied
I have a script like the one below, which is used to copy all text layers from all open PSD files and then export them to TXT files.
The problem is that each PSD file will be exported to a different TXT file, but I want them all to be exported to just 1 TXT file. The only separation between each file is a blank line/a symbol/ or the file name. Is that possible?
var useDialog = true;
var openFile = true;
var separator = "*************************************";
function initTextExport() {
if (app.documents.length < 1) {
alert("Please open at least one file", "TextExport Error", true);
return;
}
var docs, runMultiple;
if (app.documents.length > 1) {
runMultiple = confirm("TextExport has detected Multiple Files.\nDo you wish to run TextExport on all opened files?", true, "TextExport");
if (runMultiple === true) {
docs = app.documents;
} else {
docs = [app.activeDocument];
}
} else {
runMultiple = false;
docs = [app.activeDocument];
}
for (var i = 0; i < docs.length; i++) {
var filePath;
if (runMultiple !== true && useDialog === true) {
var saveFile = File.saveDialog("Please select a file to export the text to:");
if (saveFile == null) {
alert("User Cancelled");
return;
}
filePath = saveFile.path + "/" + saveFile.name;
} else {
filePath = Folder.myDocuments + '/' + docs[i].name + '.txt';
}
var fileOut = new File(filePath);
var fileLineFeed = ($.os.search(/windows/i) != -1) ? "windows" : "macintosh";
fileOut.linefeed = fileLineFeed;
fileOut.open("w", "TEXT", "????");
app.activeDocument = docs[i];
goTextExport2(app.activeDocument, fileOut);
fileOut.close();
if (runMultiple === false) {
if (openFile === true)
fileOut.execute();
else
alert("File was saved to:\n" + Folder.decode(filePath), "TextExport");
}
}
if (runMultiple === true) {
alert("Parsed " + documents.length + " files;\nFiles were saved in your documents folder", "TextExport");
}
}
function goTextExport2(el, fileOut) {
var layers = el.layers;
for (var layerIndex = layers.length; layerIndex > 0; layerIndex--) {
var currentLayer = layers[layerIndex - 1];
if (currentLayer.kind === LayerKind.TEXT) {
fileOut.writeln(currentLayer.textItem.contents);
} else if (currentLayer.typename === "LayerSet") {
goTextExport2(currentLayer, fileOut);
}
}
}
initTextExport();
Copy link to clipboard
Copied
I haven't tested your code, but I can see that you're using write mode, not append:
fileOut.open("w", "TEXT", "????");
To:
fileOut.open("a", "TEXT", "????");
Copy link to clipboard
Copied
Copy link to clipboard
Copied
😄
Copy link to clipboard
Copied
I have a couple of similar scripts, you should either open the text file and leave it open or use append mode.
Copy link to clipboard
Copied
I found the following, which is a bit simpler, but it creates separate files:
// [Photoshop] Text Contents Collector
// https://gist.github.com/moluapple/1148519
(function () {
function layersText(oDoc) {
var oLayers = oDoc.layers, text = [], i, j, file;
function getTextLayers(oLayers, i) {
for (i = 0; i < oLayers.length; i++) {
oLayers[i].layers ? getTextLayers(oLayers[i].layers, j) : oLayers[i].kind == LayerKind.TEXT && text.push(oLayers[i].textItem.contents + "\n");
}
}
getTextLayers(oLayers, i);
file = new File(String('~/Desktop/text-content-log.txt'));
file.open('a');
file.write(text.join('\n'));
file.close();
}
var oDocs = app.documents, len = oDocs.length, d = 0;
for (; d < len; d++) layersText(oDocs[d]);
})()