Copy link to clipboard
Copied
I have about 200 image files of various sizes separated by folders, each folder represents a different customer. I need a text layer in each image that matches the name of each folder. It would be like this:
• 1 Select Folder
• 2 Copy the folder name
• 3 Open the image
• 4 Paste into a text layer. "Adding text in the image"
I found this script, it's almost all I need: Only a change: Change the file name to the folder name. Thank you.
// add document name in text layer;
#target photoshop
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
var originalUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// getting the name;
var docName = myDocument.name;
//var basename = docName.match(/(.*)\.[^\.]+$/)[1];
//getting the location;
//var docPath = myDocument.path;
// make the layer;
var myLayerRef = myDocument.artLayers.add();
myLayerRef.kind = LayerKind.TEXT;
myLayerRef.name = "text";
var myTextRef = myLayerRef.textItem;
myTextRef.size = 12;
myTextRef.font = "Arial";
myTextRef.justification = Justification.RIGHT;
//Set text colour in RGB values
var newColor = new SolidColor();
newColor.rgb.red = 192;
newColor.rgb.green = 44;
newColor.rgb.blue = 44;
myTextRef.color = newColor;
// Set the position of the text - percentages from left first, then from top.
myTextRef.position = new Array( myDocument.width -670, myDocument.height -1640);
myLayerRef.blendMode = BlendMode.NORMAL;
myLayerRef.opacity = 100;
myTextRef.contents = docName;
app.preferences.rulerUnits = originalUnits;
};
//that’s it; thanks to xbytor;
Try this on line #11
var docPath = decodeURI(myDocument.path.name);
Copy link to clipboard
Copied
Hi,
give this a try:
// add document name in text layer;
#target photoshop
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
var originalUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// getting the name;
var docName = myDocument.name;
//var basename = docName.match(/(.*)\.[^\.]+$/)[1];
//getting the location;
var docPath = myDocument.path;
// make the layer;
var myLayerRef = myDocument.artLayers.add();
myLayerRef.kind = LayerKind.TEXT;
myLayerRef.name = "text";
var myTextRef = myLayerRef.textItem;
myTextRef.size = 12;
myTextRef.font = "Arial";
myTextRef.justification = Justification.RIGHT;
//Set text colour in RGB values
var newColor = new SolidColor();
newColor.rgb.red = 192;
newColor.rgb.green = 44;
newColor.rgb.blue = 44;
myTextRef.color = newColor;
// Set the position of the text - percentages from left first, then from top.
myTextRef.position = new Array( myDocument.width -670, myDocument.height -1640);
myLayerRef.blendMode = BlendMode.NORMAL;
myLayerRef.opacity = 100;
myTextRef.contents = docPath.toString();
app.preferences.rulerUnits = originalUnits;
};
Davide Barranca
www.davidebarranca.com
Copy link to clipboard
Copied
D.Barranca you're a genius !! Came out perfect ... What To make it even better, would have a way to shorten the path leaving only the last folder? Thank you.
Copy link to clipboard
Copied
Try this
var path_parts = path.split('/')
var last_folder = path_parts[path_parts.length-1]
Two things you need if Windows gives you '\' instead of '/' and if path has a trailing '/' (in which case the last folder is path_parts.length-2
Copy link to clipboard
Copied
You can split the string/path by forward slashes ("/"). This will give you an array of words, and you can simply choose the last one.
var str = '/e/Ultilidades/Imagens/Amostra';
str = str.split('/');
str = str[str.length - 1];
Copy link to clipboard
Copied
Matias.Kiviniemi and Javier roche, in which line I should add the code, I'm not conceguindo. Thank you for your help.
Copy link to clipboard
Copied
Hi
matias.kiviniemi and JavierAroche your code should work. But only one question:
Why not simple replace this line #11
var docPath = myDocument.path;
with this
var docPath = myDocument.path.name;
Regards
pixxxel schubser
Copy link to clipboard
Copied
Ha! I didn't realize matias.kiviniemi and I replied the exact same thing
Yes, this is a cleaner solution since the document object already includes the path.name property. The solution matias and I gave is an alternative for when you have to parse a string.
Matias.Kiviniemi and Javier roche, in which line I should add the code, I'm not conceguindo. Thank you for your help.
Like pixxxel schubser said, this would replace line #11
Copy link to clipboard
Copied
How to eliminate this "20%" in the name of the folder? Unfortunately this has muddled. Maybe subistituindo by "_". Example: Used_Images. Thank you..
Copy link to clipboard
Copied
Try this on line #11
var docPath = decodeURI(myDocument.path.name);
Copy link to clipboard
Copied
It worked perfectly well! Thanks again JavierAroche.