Export all text in several opened PSD files to a TXT file.
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();
