• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Explorer ,
Apr 12, 2024 Apr 12, 2024

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

TOPICS
Actions and scripting

Views

84

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Apr 12, 2024 Apr 12, 2024

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", "????");

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 12, 2024 Apr 12, 2024

Copy link to clipboard

Copied

I collected it from a website, I'm not write it by myself

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 12, 2024 Apr 12, 2024

Copy link to clipboard

Copied

 😄

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 12, 2024 Apr 12, 2024

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 12, 2024 Apr 12, 2024

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines