Skip to main content
Carson Jones
Known Participant
April 7, 2025
Question

CUSTOM SCRIPT - Filename To Selected Layer...

  • April 7, 2025
  • 2 replies
  • 160 views

Hi Everyone - sharing a custom script I made that will change the selected layer to the document's filename. Handy if you need this in your layer setup.

 

if (app.documents.length > 0 && app.activeDocument.activeLayer) {
var docName = app.activeDocument.name;
var nameNoExt = docName.replace(/\.[^\.]+$/, ''); // remove extension
app.activeDocument.activeLayer.name = nameNoExt;
} else {
alert("No document or active layer selected.");
}

2 replies

Stephen Marsh
Community Expert
Community Expert
June 4, 2025

@Carson Jones 

 

Thanks for sharing!

 

Unfortunately, one needs a more robust check for selected layers, as there's a quirk/bug with app.activeDocument.activeLayer in ExtendScript DOM, it will return the top layer even if no layers are selected.

 

Here is an example of your script updated using AM code, which does work as expected to check if no layer is selected:

 

if (app.documents.length > 0) {

    // selected layer check by jazz-y
    s2t = stringIDToTypeID;
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayers'));
    r.putEnumerated(s2t("document"), s2t("ordinal"), s2t("targetEnum"));
    //

    if (executeActionGet(r).getList(p).count) {
        var docName = app.activeDocument.name;
        var nameNoExt = docName.replace(/\.[^\.]+$/, ''); // remove extension
        app.activeDocument.activeLayer.name = nameNoExt;

    } else {
        alert('No layer selected.');
    }
    
} else {
alert("No document open.");
}

 

creative explorer
Community Expert
Community Expert
June 4, 2025

@Carson Jones that's pretty slick. Thanks! 

m