Skip to main content
Participant
February 10, 2016
Answered

Photoshop Script Add Filename to Image as Text, But Remove '-' character

  • February 10, 2016
  • 2 replies
  • 3970 views

I found a script that will take the filename of my image and put it on the image, minus the file extension. I have added this to an action, which works exactly how I want it, however my filenames have '-' characters in them.

Is it possible to remove the '-' characters in the script? I haven't been able to get the replace function to work. (which I need).

This is the code that is working right now:

// 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.substring(2);


  
// myTextRef.contents = fileNameNoExtension.replace(/^-/i, '');


  
// off set the text to be in the middle
  myTextRef
.position = new Array( docRef.width / 2, docRef.height / 2 );
  myTextRef
.size = 28;
}

This topic has been closed for replies.
Correct answer Tom Winkelmann

myTextRef.contents = fileNameNoExtension.replace(/-/g, '');

2 replies

Participant
January 28, 2023

I'm looking for this sort of thing to place the filename on 40,000 scans I did from vintage magazines.  Unfortunately I have no idea how to use a script.  I tried saving it as a .js file and when trying to load it got an error.

Thanks for holding my hands on this.

Stephen Marsh
Community Expert
Community Expert
January 28, 2023

@nono28143406l59y 

 

Note, the original code has not survived formatting differences between forum software versions, so it isn't valid at the moment.

 

Edit: I have fixed up the lines, however, the code still has issues and doesn't work... I had to add a missing variable. Other modifications will be needed to set the size/colour/position of the text relative to your scans. It would be helpful if you can provide an example before/after files as a guide.

 

// Now create a text layer at the front
var docRef = activeDocument;
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.substring(2);

// myTextRef.contents = fileNameNoExtension.replace(/^-/i, '');

// off set the text to be in the middle
myTextRef.position = new Array( docRef.width / 2, docRef.height / 2 );
myTextRef.size = 28;

 

In a quick test, it mangled the filename. Something like the following would be better as a replacement:

 

// Now create a text layer at the front
var docRef = activeDocument;
var myLayerRef = docRef.artLayers.add();
myLayerRef.kind = LayerKind.TEXT;
myLayerRef.name = "Filename";

var myTextRef = myLayerRef.textItem;
myTextRef.size = 28;

// strip the extension off
var fileNameNoExtension = docRef.name.split(".")[0];
myTextRef.contents = fileNameNoExtension;

// offset the text to be in the middle
myTextRef.position = new Array( docRef.width / 2, docRef.height / 2 );

 

Full instructions for saving/running here:

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

 

Let me know if you have any questions/issues.

 

Stephen Marsh
Community Expert
Community Expert
January 28, 2023

Another similar script, however, it will still need adjusting to work with your specific images:

 

/*
Utility PS Scripts created by David M. Converse ©2018-21

This script is a demo of using adding filename to an image

Last modified 6/2/2021

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#target photoshop

testText();

function testText(){
    if(documents.length > 0){
        var originalDialogMode = app.displayDialogs;
        app.displayDialogs = DialogModes.ERROR;
        var originalRulerUnits = preferences.rulerUnits;
        preferences.rulerUnits = Units.PIXELS;
        try{
            var docRef = activeDocument;
            var LayerRef = docRef.artLayers.add();
            LayerRef.kind = LayerKind.TEXT;
            var TextRef = LayerRef.textItem;
            var fileNameNoExtension = docRef.name;
            fileNameNoExtension = fileNameNoExtension.split('-');
            if(fileNameNoExtension.length > 1){
                fileNameNoExtension.length--;
                }
            fileNameNoExtension = fileNameNoExtension.join('-');
            fileNameNoExtension = fileNameNoExtension.split('.');
            if(fileNameNoExtension.length > 1){
                fileNameNoExtension.length--;
                }
            fileNameNoExtension = fileNameNoExtension.join('.');
            TextRef.contents = fileNameNoExtension;
            TextRef.position = new Array(425, 1025);
            preferences.rulerUnits = Units.POINTS;
            TextRef.size = 96;
            TextRef.useAutoLeading = false;
            TextRef.leading = 42;
            TextRef.font = 'Calibri-Bold';
            TextRef.justification = Justification.CENTER;
            TextRef.autoKerning = AutoKernType.METRICS;
            }
        catch(e){
            preferences.rulerUnits = originalRulerUnits;
            app.displayDialogs = originalDialogMode;
            return;
            }
        preferences.rulerUnits = originalRulerUnits;
        app.displayDialogs = originalDialogMode;
        }
    else{
        alert('You must have a document open to run this script.');
        return;
        }
    }

 

Tom Winkelmann
Tom WinkelmannCorrect answer
Inspiring
February 10, 2016

myTextRef.contents = fileNameNoExtension.replace(/-/g, '');

catvlekAuthor
Participant
February 11, 2016

Thankyou!