• Global community
    • Language:
      • Deutsch
      • English
      • EspaƱol
      • FranƧais
      • PortuguĆŖs
  • ę—„ęœ¬čŖžć‚³ćƒŸćƒ„ćƒ‹ćƒ†ć‚£
    Dedicated community for Japanese speakers
  • ķ•œźµ­ ģ»¤ė®¤ė‹ˆķ‹°
    Dedicated community for Korean speakers
Exit
4

Using a Script to Add a File Name to an Image.

New Here ,
Jan 08, 2020 Jan 08, 2020

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.

Views

18.0K

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
New Here ,
Jul 29, 2023 Jul 29, 2023

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");
  */
}

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
New Here ,
Jul 29, 2023 Jul 29, 2023

Copy link to clipboard

Copied

LATEST

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");
  */
}

 

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