Skip to content

Instantly share code, notes, and snippets.

@NimaiMalle
Created June 26, 2018 03:50
Show Gist options
  • Save NimaiMalle/4e793044357b796814534d8ddb8a7683 to your computer and use it in GitHub Desktop.
Save NimaiMalle/4e793044357b796814534d8ddb8a7683 to your computer and use it in GitHub Desktop.
Add filename as text in a Photoshop document
// this script is a variation of the script addTimeStamp.js that is installed with PH7
if ( documents.length > 0 )
{
var originalDialogMode = app.displayDialogs;
app.displayDialogs = DialogModes.ERROR;
var originalRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;
try
{
var docRef = activeDocument;
// Now create a text layer at the front
var myLayerRef = docRef.artLayers.add();
myLayerRef.kind = LayerKind.TEXT;
myLayerRef.name = "Filename";
var myTextRef = myLayerRef.textItem;
// strip the extension off
var fileNameNoExtension = docRef.name;
fileNameNoExtension = fileNameNoExtension.split( "." );
if ( fileNameNoExtension.length > 1 ) {
fileNameNoExtension.length--;
}
fileNameNoExtension = fileNameNoExtension.join(".");
myTextRef.contents = fileNameNoExtension;
// off set the text to be in the middle
myTextRef.position = new Array( docRef.width / 2, docRef.height / 2 );
myTextRef.size = 20;
}
catch( e )
{
// An error occurred. Restore ruler units, then propagate the error back
// to the user
preferences.rulerUnits = originalRulerUnits;
app.displayDialogs = originalDialogMode;
throw e;
}
// Everything went Ok. Restore ruler units
preferences.rulerUnits = originalRulerUnits;
app.displayDialogs = originalDialogMode;
}
else
{
alert( "You must have a document open to add the filename!" );
}
@ghp1968
Copy link

ghp1968 commented Jun 16, 2022

I came across your file regarding add a file name as I needed this script for my photography editing.

Looking at the file, how can I change the default colour from black to white and realign the code to top right.

Kind regards

Gary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment