Skip to main content
Participating Frequently
April 12, 2024
Question

Export all text in several opened PSD files to a TXT file.

  • April 12, 2024
  • 3 replies
  • 893 views

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

This topic has been closed for replies.

3 replies

Stephen Marsh
Community Expert
Community Expert
April 12, 2024

I found the following, which is a bit simpler, but it creates separate files:

 

 
However, after a couple of tweaks, it now writes to a single file. Each separate doc is separated by a new line:
 
// [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]);
})()
Legend
April 12, 2024

I have a couple of similar scripts, you should either open the text file and leave it open or use append mode.

Stephen Marsh
Community Expert
Community Expert
April 12, 2024

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", "????");
KathyyyyAuthor
Participating Frequently
April 12, 2024
I collected it from a website, I'm not write it by myself