Import a txt file into the note tool?

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

zee333
Posts: 1
Joined: Tue Jul 12, 2022 5:23 pm

Import a txt file into the note tool?

Post by zee333 »

I have a folder with multiple files in it:
File A.png
File A Color Key.txt
File B.png
File B Color Key.txt
File C.png
File C Color Key.txt
What I want to do is open the png files in photoshop, annotate then with the note tool, pulling the text from the matching txt file and then save the file as File A.pdf in the same folder. My main issue so far has been trying to access the note tool through scripting but I am also not sure about the best way to pull the text from the txt file. I've seen functions that take a txt file and turn the text into an array breaking up the string at line breaks and that seems like it would work for me if I could enter the array as the text in the note.
any help is appreciated
User avatar
Dormeur74
Posts: 36
Joined: Mon Oct 03, 2016 4:56 am

Re: Import a txt file into the note tool?

Post by Dormeur74 »

Hi all, I wish you a happy New Year.

I had not your A.png and created one (1000 px x 1000 px) on my D:\ drive.

Code: Select all

#target photoshop

var docPath = Folder("/D/Scripting-PS");

main();

function main(){
	var docRef = open(File(docPath+"/A.png"));
	
    //declare listFile variable for text file
    var listFile = File(docPath + "/A color Key.txt");
    //if no file selected, return
    if(listFile == null) return;
    //read the selected text file (listFile) to a string("note")
    listFile.open('r') ;
	var note = "";
	while(!listFile.eof) {
		var line = listFile.readln() + "\r";
		note += line;
	}
	listFile.close();
	// Paste the note
	var layers = app.activeDocument.artLayers;
	var layer = layers.add();
	layer.kind = LayerKind.TEXT;
	layer.name = "Annotation";
	var textItem = layer.textItem;
	textItem.kind = TextType.PARAGRAPHTEXT;
	textItem.size = 30;
	textItem.position = [10, 10];
	textItem.contents = note;
	textItem.width = new UnitValue("1000 pixels");
	textItem.height = new UnitValue("1000 pixels");
}
User avatar
Dormeur74
Posts: 36
Joined: Mon Oct 03, 2016 4:56 am

Re: Import a txt file into the note tool?

Post by Dormeur74 »

The whole script with creation of a PDF file (A4 size).

Code: Select all

#target photoshop 					// MAC Finder ou WINDOWS Explorer for double click
app.bringToFront(); 					// Photoshop in foreground
app.displayDialogs = DialogModes.NO;    	// No dialog boxes

// Set the user's grafical rulers preference in a variable  
var userRulerUnits = app.preferences.rulerUnits;
// ...now we can work in inches
app.preferences.rulerUnits = Units.INCHES;

// Const values
var black = new SolidColor();
black.rgb.hexValue = '000000';

var pdfName = "NewPDF";
var pdfOptions = new PDFSaveOptions();
	pdfOptions.PDFCompatibility = PDFCompatibility.PDF15;
	pdfOptions.encoding = PDFEncoding.PDFZIP;
	pdfOptions.preserveEditability = true;
	pdfOptions.embedAllFonts = true;
	pdfOptions.compressArt = true;

main(); 
// End of script, we restore the user's grafical rulers 
app.preferences.rulerUnits = userRulerUnits;
// ***************** END *******************

function main(){ 
	// Script folder location
	var scriptName = $.fileName.replace(/^.*[\\\/]/, '');
	var curDir = Folder($.fileName.substr(0,$.fileName.length-scriptName.length - 1));

    //declare listFile variable, prompt to browse for text file
	var listFile = File.openDialog("Select the text file to import.","*.txt");
    //if no file selected, return
    if(listFile == null) return;
    //read the selected text file (listFile) to a string("note")
    listFile.open('r') ;
	var note = "";
	while(!listFile.eof) {
		var line = listFile.readln() + "\r";
		note += line;
	}
	listFile.close();
	// Paste the note in a new A4 text layer
	var docRef = app.documents.add(8.3, 11.7,300, pdfName,NewDocumentMode.RGB);
	var txtLayer = docRef.artLayers.add();
	txtLayer.kind = LayerKind.TEXT;
	txtLayer.name = "Annotation";
	var textItem = txtLayer.textItem;
	textItem.kind = TextType.PARAGRAPHTEXT;
	textItem.font = app.fonts.getByName("ArialMT");
	textItem.size = 15;
	textItem.color = black;
	textItem.position = [0.5, 0.5];
	textItem.contents = note;
	textItem.width = 597;
	textItem.height = 843;
	
	// Saves document with PDF format
	docRef.flatten();

	// Save PDF format
	docRef.saveAs(File(curDir + '/' + pdfName + ".pdf"),pdfOptions, true );
	docRef.close(SaveOptions.DONOTSAVECHANGES);	
	alert("Your file were save as " + curDir.path + "/" + curDir.name + "/" + pdfName + ".pdf");
}