Using a Script to Add a File Name to an Image.
Copy link to clipboard
Copied
How to put file Name in file top left corner in file Using a Script to Add a File Name to an Image.
let me explain anybody experts.
Explore related tutorials & articles
Copy link to clipboard
Copied
Here it is, just as you asked for it, and then some! It's the GOLDENGUN of Photoshop name scripts; it will surely blow all others out of the water. Variables allow complete customization, and comments are plentiful for ease of editing.
Overall, the script will automatically center the filename and maximize the text size based on the limits you set, until it clips. It will also attempt to resize outputs automatically if you clip horizontally, as long as you keep reasonable values. The script offers three output options: filename, filename.extension, and path name. Additionally, you can select your font of choice from the comments, apply custom offsets from "auto center," and choose colors based on RGB values. Feel free to edit the Scaling Factors and other variables to achieve the exact output you desire, and verbose output is available as well. The variable for fonts has been improved with helpful comments, and I've added horizontal variable offsets that scale accordingly.
A special thanks to JJMack for the original script and the powerful tool, ChatGPT. Hopefully this helps someone, if you find it useful, please give me a thumbs up! If anyone wants to suggest additional features or finds any bugs, please let me know, and I will fix/add them ASAP.
#target photoshop // this command only works in Photoshop CS2 and higher
app.bringToFront();
if (!documents.length) {
alert('There are no documents open.', 'No Document');
} else {
// Call the main function wrapped in Document.suspendHistory()
app.activeDocument.suspendHistory("Script Name", "main()");
}
function main() {
// Common default Photoshop fonts with abbreviations
var fontList = [
{ number: "1", abbr: "ari", name: "ArialMT" }, { number: "2", abbr: "cal", name: "Calibri" },
{ number: "3", abbr: "cmb", name: "Cambria" }, { number: "4", abbr: "cdn", name: "Candara" },
{ number: "5", abbr: "con", name: "Consolas" }, { number: "6", abbr: "cor", name: "Corbel" },
{ number: "7", abbr: "cms", name: "ComicSansMS" }, { number: "8", abbr: "crn", name: "CourierNewPSMT" },
{ number: "9", abbr: "geo", name: "Georgia" }, { number: "10", abbr: "imp", name: "Impact" },
{ number: "11", abbr: "lcn", name: "LucidaConsole" }, { number: "12", abbr: "mss", name: "MicrosoftSansSerif" },
{ number: "13", abbr: "plt", name: "PalatinoLinotype" }, { number: "14", abbr: "seg", name: "SegoeUI" },
{ number: "15", abbr: "tah", name: "Tahoma" }, { number: "16", abbr: "myp", name: "Myriad Pro" },
{ number: "17", abbr: "tnr", name: "TimesNewRomanPSMT" },
{ number: "18", abbr: "trb", name: "TrebuchetMS" }, { number: "19", abbr: "ver", name: "Verdana" }
];
// Set the desired font name here (e.g., "ArialMT", "Calibri", etc.)
var selectedFontName = "TimesNewRomanPSMT";
// User-defined variables
var fontName = selectedFontName;
var textColor = new SolidColor;
textColor.rgb.red = 255;
textColor.rgb.green = 255;
textColor.rgb.blue = 255;
var minFontSize = 10; // Minimum font size in pixels
var baseScalingFactor = 2.0; // Base scaling factor (0.01 = 1%) .1 = May force truncation
// Additional scaling factor (modify this value as needed)
var additionalScalingFactor = 1.0; // (1.0 = 100%) Over 100% may force truncation
// Set the vertical offset percentage (0.0 means centered vertically, 0.25 is 25% up, -0.25 is 25% down)
var verticalOffsetPercentage = -0.80; // Default value: 0.0 (centered vertically)
// Set the horizontal offset percentage (0.0 means centered horizontally, 0.25 is 25% left, -0.25 is 25% right)
var horizontalOffsetPercentage = -.70; // Default value: 0.0 (centered horizontally)
// Option 1: Full Path Name (Uncomment to use)
//var fileNameWithoutExtension = decodeURI(app.activeDocument.fullName);
// Option 2: File Name (Uncomment to use)
var fileNameWithoutExtension = decodeURI-app.activeDocument.name.split(".")[0]);
// Option 3: File Name with Extension (Uncomment to use)
//var fileNameWithoutExtension = decodeURI(app.activeDocument.name);
// Automatically adjust baseScalingFactor based on the document's resolution
var resolutionScalingFactor = app.activeDocument.resolution / 72; // 72 PPI as the reference resolution
baseScalingFactor = baseScalingFactor * resolutionScalingFactor;
// If Option 1 (Full Path Name) is enabled, set the maximum baseScalingFactor to 0.05 (5%)
if (fileNameWithoutExtension) {
baseScalingFactor = Math.min(baseScalingFactor, 0.042);
}
// If Option 3 (File Name with Extension) is enabled, adjust the additionalScalingFactor based on the resolution
if (fileNameWithoutExtension) {
var minAdditionalScalingFactor = 1.5; // Minimum additional scaling factor (0.5 = 50%) to avoid excessively small text
var maxAdditionalScalingFactor = 6.0; // Maximum additional scaling factor (2.0 = 200%) to control text enlargement
var resolutionScalingFactor = app.activeDocument.resolution / 72; // 72 PPI as the reference resolution
additionalScalingFactor = Math.max(minAdditionalScalingFactor, additionalScalingFactor * resolutionScalingFactor);
additionalScalingFactor = Math.min(maxAdditionalScalingFactor, additionalScalingFactor);
}
// Rest of the code...
var invertOffset = true; // Set to true to invert the offset effect
// Invert the vertical offset if needed
if (invertOffset) {
verticalOffsetPercentage *= -1;
horizontalOffsetPercentage *= -1;
}
var doc = app.activeDocument;
var docWidth = doc.width.value;
var docHeight = doc.height.value;
var availableWidth = docWidth - Math.abs(docWidth * horizontalOffsetPercentage); // Calculate the available width for the text based on the horizontal offset percentage
var centerPosX = docWidth / 2 + (docWidth / 2) * horizontalOffsetPercentage; // Apply the horizontal offset
var centerPosY = docHeight / 2 + (docHeight / 2) * verticalOffsetPercentage; // Apply the vertical offset
// Text properties
var fontSize = Math.max(minFontSize, availableWidth * baseScalingFactor * additionalScalingFactor); // Scale the font size based on the available width, base scaling factor, and additional scaling factor
// Get the file name without the extension
var textContent = fileNameWithoutExtension;
// Save the current ruler and type
var strtRulerUnits = app.preferences.rulerUnits;
var strtTypeUnits = app.preferences.typeUnits;
app.preferences.rulerUnits = Units.PIXELS;
app.preferences.typeUnits = TypeUnits.PIXELS;
// Resize the image to a fixed resolution (72 PPI) temporarily for consistent results
var testRes = 72;
var currentRes = doc.resolution;
if (currentRes != testRes) {
doc.resizeImage(doc.width.value, doc.height.value, testRes);
}
// Create a new text layer
var textLayer = doc.artLayers.add();
textLayer.name = doc.name;
textLayer.kind = LayerKind.TEXT;
textLayer.textItem.color = textColor;
textLayer.textItem.font = fontName;
textLayer.textItem.size = fontSize;
textLayer.textItem.position = [centerPosX, centerPosY]; // Center-align the text with the offsets
// Set text layer options
textLayer.textItem.justification = Justification.CENTER; // Center-align the text horizontally
textLayer.textItem.antiAliasMethod = AntiAlias.SHARP;
// Set the text content
try {
textLayer.textItem.contents = textContent;
} catch (error) {
textLayer.textItem.contents = activeDocument.name;
}
// Restore the original resolution and preferences
if (currentRes != testRes) {
doc.resizeImage(doc.width.value, doc.height.value, currentRes);
}
app.preferences.rulerUnits = strtRulerUnits;
app.preferences.typeUnits = strtTypeUnits;
// Commenting out the final output
/*
var outputString = "Script executed successfully!\n\n";
outputString += "Selected Font: " + fontName + "\n";
outputString += "Font Size: " + fontSize + " pixels\n";
outputString += "Text Color: RGB(" + textColor.rgb.red + ", " + textColor.rgb.green + ", " + textColor.rgb.blue + ")\n";
outputString += "Horizontal Offset Percentage: " + (horizontalOffsetPercentage * 100) + "%\n";
outputString += "Vertical Offset Percentage: " + (verticalOffsetPercentage * 100) + "%\n";
outputString += "Text Content: " + textContent + "\n";
alert(outputString, "Script Output");
*/
}
Copy link to clipboard
Copied
Here it is, just as you asked for it, and then some! It's the GOLDENGUN of Photoshop name scripts; it will surely blow all others out of the water. Variables allow complete customization, and comments are plentiful for ease of editing.
Overall, the script will automatically center the filename and maximize the text size based on the limits you set, until it clips. It will also attempt to resize outputs automatically if you clip horizontally, as long as you keep reasonable values. The script offers three output options: filename, filename.extension, and path name. Additionally, you can select your font of choice from the comments, apply custom offsets from "auto center," and choose colors based on RGB values. Feel free to edit the Scaling Factors and other variables to achieve the exact output you desire, and verbose output is available as well. The variable for fonts has been improved with helpful comments, and I've added horizontal variable offsets that scale accordingly.
A special thanks to JJMack for the original script and the powerful tool, ChatGPT. Hopefully this helps someone, if you find it useful, please give me a thumbs up! If anyone wants to suggest additional features or finds any bugs, please let me know, and I will fix/add them ASAP.
#target photoshop // this command only works in Photoshop CS2 and higher
app.bringToFront();
if (!documents.length) {
alert('There are no documents open.', 'No Document');
} else {
// Call the main function wrapped in Document.suspendHistory()
app.activeDocument.suspendHistory("Script Name", "main()");
}
function main() {
// Common default Photoshop fonts with abbreviations
var fontList = [
{ number: "1", abbr: "ari", name: "ArialMT" }, { number: "2", abbr: "cal", name: "Calibri" },
{ number: "3", abbr: "cmb", name: "Cambria" }, { number: "4", abbr: "cdn", name: "Candara" },
{ number: "5", abbr: "con", name: "Consolas" }, { number: "6", abbr: "cor", name: "Corbel" },
{ number: "7", abbr: "cms", name: "ComicSansMS" }, { number: "8", abbr: "crn", name: "CourierNewPSMT" },
{ number: "9", abbr: "geo", name: "Georgia" }, { number: "10", abbr: "imp", name: "Impact" },
{ number: "11", abbr: "lcn", name: "LucidaConsole" }, { number: "12", abbr: "mss", name: "MicrosoftSansSerif" },
{ number: "13", abbr: "plt", name: "PalatinoLinotype" }, { number: "14", abbr: "seg", name: "SegoeUI" },
{ number: "15", abbr: "tah", name: "Tahoma" }, { number: "16", abbr: "myp", name: "Myriad Pro" },
{ number: "17", abbr: "tnr", name: "TimesNewRomanPSMT" },
{ number: "18", abbr: "trb", name: "TrebuchetMS" }, { number: "19", abbr: "ver", name: "Verdana" }
];
// Set the desired font name here (e.g., "ArialMT", "Calibri", etc.)
var selectedFontName = "TimesNewRomanPSMT";
// User-defined variables
var fontName = selectedFontName;
var textColor = new SolidColor;
textColor.rgb.red = 255;
textColor.rgb.green = 255;
textColor.rgb.blue = 255;
var minFontSize = 10; // Minimum font size in pixels
var baseScalingFactor = 2.0; // Base scaling factor (0.01 = 1%) .1 = May force truncation
// Additional scaling factor (modify this value as needed)
var additionalScalingFactor = 1.0; // (1.0 = 100%) Over 100% may force truncation
// Set the vertical offset percentage (0.0 means centered vertically, 0.25 is 25% up, -0.25 is 25% down)
var verticalOffsetPercentage = 0.0; // Default value: 0.0 (centered vertically)
// Set the horizontal offset percentage (0.0 means centered horizontally, 0.25 is 25% left, -0.25 is 25% right)
var horizontalOffsetPercentage = 0.0; // Default value: 0.0 (centered horizontally)
// Option 1: Full Path Name (Uncomment to use)
//var fileNameWithoutExtension = decodeURI(app.activeDocument.fullName);
// Option 2: File Name (Uncomment to use)
var fileNameWithoutExtension = decodeURI(app.activeDocument.name.split(".")[0]);
// Option 3: File Name with Extension (Uncomment to use)
//var fileNameWithoutExtension = decodeURI(app.activeDocument.name);
// Automatically adjust baseScalingFactor based on the document's resolution
var resolutionScalingFactor = app.activeDocument.resolution / 72; // 72 PPI as the reference resolution
baseScalingFactor = baseScalingFactor * resolutionScalingFactor;
// If Option 1 (Full Path Name) is enabled, set the maximum baseScalingFactor to 0.05 (5%)
if (fileNameWithoutExtension) {
baseScalingFactor = Math.min(baseScalingFactor, 0.022);
}
// If Option 2 (File Name) or Option 3 (File Name with Extension) is enabled, adjust the additionalScalingFactor based on the resolution
if (fileNameWithoutExtension) {
var minAdditionalScalingFactor = 3.5; // Minimum additional scaling factor (0.5 = 50%) to avoid excessively small text
var maxAdditionalScalingFactor = 6.0; // Maximum additional scaling factor (2.0 = 200%) to control text enlargement
var resolutionScalingFactor = app.activeDocument.resolution / 72; // 72 PPI as the reference resolution
additionalScalingFactor = Math.max(minAdditionalScalingFactor, additionalScalingFactor * resolutionScalingFactor);
additionalScalingFactor = Math.min(maxAdditionalScalingFactor, additionalScalingFactor);
}
// Rest of the code...
var invertOffset = true; // Set to true to invert the offset effect
// Invert the vertical offset if needed
if (invertOffset) {
verticalOffsetPercentage *= -1;
horizontalOffsetPercentage *= -1;
}
var doc = app.activeDocument;
var docWidth = doc.width.value;
var docHeight = doc.height.value;
var availableWidth = docWidth - Math.abs(docWidth * horizontalOffsetPercentage); // Calculate the available width for the text based on the horizontal offset percentage
var centerPosX = docWidth / 2 + (docWidth / 2) * horizontalOffsetPercentage; // Apply the horizontal offset
var centerPosY = docHeight / 2 + (docHeight / 2) * verticalOffsetPercentage; // Apply the vertical offset
// Text properties
var fontSize = Math.max(minFontSize, availableWidth * baseScalingFactor * additionalScalingFactor); // Scale the font size based on the available width, base scaling factor, and additional scaling factor
// Get the file name without the extension
var textContent = fileNameWithoutExtension;
// Save the current ruler and type
var strtRulerUnits = app.preferences.rulerUnits;
var strtTypeUnits = app.preferences.typeUnits;
app.preferences.rulerUnits = Units.PIXELS;
app.preferences.typeUnits = TypeUnits.PIXELS;
// Resize the image to a fixed resolution (72 PPI) temporarily for consistent results
var testRes = 72;
var currentRes = doc.resolution;
if (currentRes != testRes) {
doc.resizeImage(doc.width.value, doc.height.value, testRes);
}
// Create a new text layer
var textLayer = doc.artLayers.add();
textLayer.name = doc.name;
textLayer.kind = LayerKind.TEXT;
textLayer.textItem.color = textColor;
textLayer.textItem.font = fontName;
textLayer.textItem.size = fontSize;
textLayer.textItem.position = [centerPosX, centerPosY]; // Center-align the text with the offsets
// Set text layer options
textLayer.textItem.justification = Justification.CENTER; // Center-align the text horizontally
textLayer.textItem.antiAliasMethod = AntiAlias.SHARP;
// Set the text content
try {
textLayer.textItem.contents = textContent;
} catch (error) {
textLayer.textItem.contents = activeDocument.name;
}
// Restore the original resolution and preferences
if (currentRes != testRes) {
doc.resizeImage(doc.width.value, doc.height.value, currentRes);
}
app.preferences.rulerUnits = strtRulerUnits;
app.preferences.typeUnits = strtTypeUnits;
// Commenting out the final output for smooth operation
/*
var outputString = "Script executed successfully!\n\n";
outputString += "Selected Font: " + fontName + "\n";
outputString += "Font Size: " + fontSize + " pixels\n";
outputString += "Text Color: RGB(" + textColor.rgb.red + ", " + textColor.rgb.green + ", " + textColor.rgb.blue + ")\n";
outputString += "Horizontal Offset Percentage: " + (horizontalOffsetPercentage * 100) + "%\n";
outputString += "Vertical Offset Percentage: " + (verticalOffsetPercentage * 100) + "%\n";
outputString += "Text Content: " + textContent + "\n";
alert(outputString, "Script Output");
*/
}
Copy link to clipboard
Copied
I'm new and know nothing about scripting or coding. This is exactly what I want to do - write the filename centered about 15% from the bottom margin. This way I can watch them on my screen saver and remember what I'm seeing.
I am obviously doing something wrong. I have copied, pasted, and tried each and every one of the scripts posted here. They all show something is happening in the History tab and one or more new layers show up, but I can see nothing in any of those layers, even when the rest are turned off. Yet, if I click on Type and enter text, it shows up and works normally.
I'm trying to work on the .psd files, because .jpgs revert back to Bridge as a raw file and end up getting converted to .psd anyway.
Is there some preference or switch I need to fix?
Copy link to clipboard
Copied
Is there some preference or switch I need to fix?
By Ken287629603pzl
It's not possible for me to comment on the issues that you have had.
Please attach a sample PSD with a text layer with the desired font, size, position etc.
P.S. It might be easier for you to use alternative software for this task, where the feature is built-in and readily accessible:
https://www.xnview.com/en/xnconvert/
This example uses metadata, however, the text can also be the filename or other variables.
Copy link to clipboard
Copied
The file "BB - Fort DeRussy. . .psd" is a reduction from the original file to fit on the upload here. The Screenshot 1.psd is that open image BEFORE applying Sanders final script above showing no history and only the primary layer. The Screenshot 2.psd shows the same information with the resultant image after running that script. If you open up the "BB - Fort DeRussy. . .psd" it will have the added layers, but no visible text.
Copy link to clipboard
Copied
I'll take a look, but that wasn't what I asked for. I wanted a working example of the final result, not the faulty results.
Copy link to clipboard
Copied
If you use the move tool with "show transform controls" activated, you will find the text half on/half off the upper left canvas in 1.3pt white text.
Copy link to clipboard
Copied
Thank you so much.
I had tried so many of these scripts with the same hidden results that I was beginning to think my computer had developed a grudge against me. I knew there had to be some simple preference or switch I had missed.
Now I can start fine tuning it.
Copy link to clipboard
Copied
Try specifying the type size and location directly in the script. Some of the examples posted above have this code. You can also run an action after the script to set the location if that's easier.
Copy link to clipboard
Copied
You're welcome.
The script code has //comments with explanations which would be the first place to start.
Copy link to clipboard
Copied
I'm still having problems with this script. I can now see the filename, except it's the full path+filename+extension. Not only that, but I'm using it exactly copied and pasted.
ONLY ONE modification to change the font from TimesNewRomanPSMT to ArialMT and the script crashes with
Error 25: Expected:;.
Line 7
-> Call the main function wrapped in Document.suspendHistory()
What's wrong?
Copy link to clipboard
Copied
I have no idea which script you are using, there were multiples posted in this topic. Perhaps it's time to try a different script, there are many on the forum to be found via a search.
Copy link to clipboard
Copied
I think I agree with you. I've found four different scripts so far. The problem is I don't know enough to cut and splice pieces together to end up with what I want - White bolded and shadowed Tahoma or Arial file name only near the bottom and centered on the image. I would love to be able to bulk apply it, but even if I have to do it individually to set font size and position, I'd do it.
Is there a specific site that explains the Adobe coding used in scripts, so a novice could try to figure out how to do the above?
Thank you.
Copy link to clipboard
Copied
I'll post some code later on... Or you could just use xnconvert as previously suggested.
Copy link to clipboard
Copied
Rather than cut and paste scripts, you might record an action that sets styling the way you want. Run a script to create the title text and then the action. A script can run an action so the two work together.
Copy link to clipboard
Copied


-
- 1
- 2