Skip to main content
Known Participant
November 21, 2025
Answered

Script to create and rename slices for selected layers

  • November 21, 2025
  • 4 replies
  • 700 views

Hi everyone,
I’m looking a script that can create one slice for each selected layer and then rename each slice, ideally using the layer’s name.

My goal is:

  1. Loop only through the selected layers
  2. Create a slice from each layer’s exact bounds
  3. Name each slice automatically (e.g., with the layer’s name)

Since slices can only be created through Action Manager, I’m not sure about the correct approach.

If anyone has an example script or guidance, I would really appreciate it.
Thanks!

 

Correct answer Stephen Marsh

@Delta2487 

 

Here is a script based on my previously suggested "A" approach... EDIT: Updated 24th November to vA1.1 to include a check for selected layers.

 

/*
Create Slices From Selected Layers Bounds vA1-1.jsx
Stephen Marsh
vA1.0 - 22nd November 2025: Initial release
vA1.1 - 24th November 2025: Added a check for selected layers
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-create-and-rename-slices-for-selected-layers/td-p/15603067
*/

#target photoshop

// Single history stage undo
activeDocument.suspendHistory("Create Slices From Selected Layers Bounds", "main()");

function main() {

    // Check for active layer/s
    if (!hasActiveLayer()) {
        alert("No active layer/s selected!");
        return;
    }

    // Get the selected layers: courtesy of jazz-y
    var s2t = stringIDToTypeID;
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    var lrs = executeActionGet(r).getList(p),
        sel = new ActionReference();

    // Loop over the selected layers: courtesy of jazz-y
    for (var l = 0; l < lrs.count; l++) {
        sel.putIdentifier(s2t('layer'), p = lrs.getReference(l).getIdentifier(s2t('layerID')));
        (r = new ActionReference()).putIdentifier(s2t('layer'), p);
        (d = new ActionDescriptor()).putReference(s2t("target"), r);
        //d.putBoolean(s2t("makeVisible"), false);
        executeAction(s2t('select'), d, DialogModes.NO);

        ///// Start doing stuff to the selected layers /////
        //alert(app.activeDocument.activeLayer.name);
        createSliceFromActiveLayerBounds();
        renameActiveSliceToLayerName();
        ///// Stop doing stuff to the selected layers /////

    }
}


///// Functions /////

function createSliceFromActiveLayerBounds() {

    var doc = app.activeDocument;
    var layer = doc.activeLayer;

    // Ensure pixel units
    var originalRuler = app.preferences.rulerUnits;
    app.preferences.rulerUnits = Units.PIXELS;

    // Get layer bounds in pixels (redundant, but it can't hurt)
    var b = layer.bounds;
    var left = b[0].as("px");
    var top = b[1].as("px");
    var right = b[2].as("px");
    var bottom = b[3].as("px");

    // Build the slices using AM code
    var idMk = charIDToTypeID("Mk  ");
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putClass(stringIDToTypeID("slice"));
    desc.putReference(charIDToTypeID("null"), ref);
    var sliceDesc = new ActionDescriptor();
    sliceDesc.putEnumerated(
        charIDToTypeID("Type"),
        stringIDToTypeID("sliceType"),
        stringIDToTypeID("user")
    );
    var rect = new ActionDescriptor();
    rect.putUnitDouble(charIDToTypeID("Top "), charIDToTypeID("#Pxl"), top);
    rect.putUnitDouble(charIDToTypeID("Left"), charIDToTypeID("#Pxl"), left);
    rect.putUnitDouble(charIDToTypeID("Btom"), charIDToTypeID("#Pxl"), bottom);
    rect.putUnitDouble(charIDToTypeID("Rght"), charIDToTypeID("#Pxl"), right);
    sliceDesc.putObject(charIDToTypeID("At  "), charIDToTypeID("Rctn"), rect);
    desc.putObject(charIDToTypeID("Usng"), stringIDToTypeID("slice"), sliceDesc);
    executeAction(idMk, desc, DialogModes.NO);

    // Restore the original ruler units
    app.preferences.rulerUnits = originalRuler;
}

function renameActiveSliceToLayerName() {
    var layerName = app.activeDocument.activeLayer.name;
    var idsetd = charIDToTypeID("setd");
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putEnumerated(
        stringIDToTypeID("slice"),
        charIDToTypeID("Ordn"),
        charIDToTypeID("Trgt")
    );
    desc.putReference(charIDToTypeID("null"), ref);
    var inner = new ActionDescriptor();
    inner.putString(charIDToTypeID("Nm  "), layerName);
    desc.putObject(
        charIDToTypeID("T   "),
        stringIDToTypeID("slice"),
        inner
    );
    executeAction(idsetd, desc, DialogModes.NO);
}

function hasActiveLayer() {
    try {
        // selected layer check by jazz-y
        var s2t = stringIDToTypeID;
        var r = new ActionReference();
        r.putProperty(s2t('property'), s2t('targetLayers'));
        r.putEnumerated(s2t("document"), s2t("ordinal"), s2t("targetEnum"));
        var desc = executeActionGet(r);
        var targetLayers = desc.getList(s2t('targetLayers'));
        return targetLayers.count > 0;
    } catch (e) {
        return false;
    }
}

 

4 replies

Stephen Marsh
Community Expert
Community Expert
November 21, 2025

@Delta2487 - This alternative script uses my "B" version suggestion... EDIT: Updated 24th November to vB1.1 to include a check for selected layers.

 

/*
Create Slices From Selected Layers vB1-1.jsx
Stephen Marsh
vB1.0 - 22nd November 2025: Inititial release
vB1.1 - 24th November 2025: Added a check for selected layers
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-create-and-rename-slices-for-selected-layers/td-p/15603067
*/

#target photoshop

// Single history stage undo
activeDocument.suspendHistory("Create Slices From Selected Layers", "main()");

function main() {

    // Check for active layer/s
    if (!hasActiveLayer()) {
        alert("No active layer/s selected!");
        return;
    }

    // Get the selected layers: courtesy of jazz-y
    var s2t = stringIDToTypeID;
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    var lrs = executeActionGet(r).getList(p),
        sel = new ActionReference();

    // Loop over the selected layers: courtesy of jazz-y
    for (var l = 0; l < lrs.count; l++) {
        sel.putIdentifier(s2t('layer'), p = lrs.getReference(l).getIdentifier(s2t('layerID')));
        (r = new ActionReference()).putIdentifier(s2t('layer'), p);
        (d = new ActionDescriptor()).putReference(s2t("target"), r);
        //d.putBoolean(s2t("makeVisible"), false);
        executeAction(s2t('select'), d, DialogModes.NO);

        ///// Start doing stuff to the selected layers /////
        //alert(app.activeDocument.activeLayer.name);
        newLayerBasedSlice();
        renameActiveSliceToLayerName();
        ///// Stop doing stuff to the selected layers /////

    }
}


///// Functions /////

function newLayerBasedSlice() {
    var idMk = charIDToTypeID("Mk  ");
    var desc1305 = new ActionDescriptor();
    var idnull = charIDToTypeID("null");
    var ref126 = new ActionReference();
    var idslice = stringIDToTypeID("slice");
    ref126.putClass(idslice);
    desc1305.putReference(idnull, ref126);
    var idUsng = charIDToTypeID("Usng");
    var desc1306 = new ActionDescriptor();
    var idType = charIDToTypeID("Type");
    var idsliceType = stringIDToTypeID("sliceType");
    var idLyr = charIDToTypeID("Lyr ");
    desc1306.putEnumerated(idType, idsliceType, idLyr);
    var idLyr = charIDToTypeID("Lyr ");
    var ref127 = new ActionReference();
    var idLyr = charIDToTypeID("Lyr ");
    var idOrdn = charIDToTypeID("Ordn");
    var idTrgt = charIDToTypeID("Trgt");
    ref127.putEnumerated(idLyr, idOrdn, idTrgt);
    desc1306.putReference(idLyr, ref127);
    var idslice = stringIDToTypeID("slice");
    desc1305.putObject(idUsng, idslice, desc1306);
    executeAction(idMk, desc1305, DialogModes.NO);
}

function renameActiveSliceToLayerName() {
    var layerName = app.activeDocument.activeLayer.name;
    var idsetd = charIDToTypeID("setd");
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putEnumerated(
        stringIDToTypeID("slice"),
        charIDToTypeID("Ordn"),
        charIDToTypeID("Trgt")
    );
    desc.putReference(charIDToTypeID("null"), ref);
    var inner = new ActionDescriptor();
    inner.putString(charIDToTypeID("Nm  "), layerName);
    desc.putObject(
        charIDToTypeID("T   "),
        stringIDToTypeID("slice"),
        inner
    );
    executeAction(idsetd, desc, DialogModes.NO);
}

function hasActiveLayer() {
    try {
        // selected layer check by jazz-y
        var s2t = stringIDToTypeID;
        var r = new ActionReference();
        r.putProperty(s2t('property'), s2t('targetLayers'));
        r.putEnumerated(s2t("document"), s2t("ordinal"), s2t("targetEnum"));
        var desc = executeActionGet(r);
        var targetLayers = desc.getList(s2t('targetLayers'));
        return targetLayers.count > 0;
    } catch (e) {
        return false;
    }
}

 

Delta2487Author
Known Participant
November 22, 2025

@Stephen Marsh This script also works well, but it gave me error 8800 when selecting a layer with a previously created slice, but if it works I will use both, thank you very much.

Stephen Marsh
Community Expert
Community Expert
November 22, 2025
quote

@Stephen Marsh This script also works well, but it gave me error 8800 when selecting a layer with a previously created slice, but if it works I will use both, thank you very much.


By @Delta2487

 

It creates a new slice from a layer, so that's probably why it errors if a slice already exists. Extra code would be required to check for an existing slice to avoid the error. The expectation is obviously that no slices for the layer should exist.

 

P.S. I have not been able to reproduce this error.

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
November 21, 2025

@Delta2487 

 

Here is a script based on my previously suggested "A" approach... EDIT: Updated 24th November to vA1.1 to include a check for selected layers.

 

/*
Create Slices From Selected Layers Bounds vA1-1.jsx
Stephen Marsh
vA1.0 - 22nd November 2025: Initial release
vA1.1 - 24th November 2025: Added a check for selected layers
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-create-and-rename-slices-for-selected-layers/td-p/15603067
*/

#target photoshop

// Single history stage undo
activeDocument.suspendHistory("Create Slices From Selected Layers Bounds", "main()");

function main() {

    // Check for active layer/s
    if (!hasActiveLayer()) {
        alert("No active layer/s selected!");
        return;
    }

    // Get the selected layers: courtesy of jazz-y
    var s2t = stringIDToTypeID;
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    var lrs = executeActionGet(r).getList(p),
        sel = new ActionReference();

    // Loop over the selected layers: courtesy of jazz-y
    for (var l = 0; l < lrs.count; l++) {
        sel.putIdentifier(s2t('layer'), p = lrs.getReference(l).getIdentifier(s2t('layerID')));
        (r = new ActionReference()).putIdentifier(s2t('layer'), p);
        (d = new ActionDescriptor()).putReference(s2t("target"), r);
        //d.putBoolean(s2t("makeVisible"), false);
        executeAction(s2t('select'), d, DialogModes.NO);

        ///// Start doing stuff to the selected layers /////
        //alert(app.activeDocument.activeLayer.name);
        createSliceFromActiveLayerBounds();
        renameActiveSliceToLayerName();
        ///// Stop doing stuff to the selected layers /////

    }
}


///// Functions /////

function createSliceFromActiveLayerBounds() {

    var doc = app.activeDocument;
    var layer = doc.activeLayer;

    // Ensure pixel units
    var originalRuler = app.preferences.rulerUnits;
    app.preferences.rulerUnits = Units.PIXELS;

    // Get layer bounds in pixels (redundant, but it can't hurt)
    var b = layer.bounds;
    var left = b[0].as("px");
    var top = b[1].as("px");
    var right = b[2].as("px");
    var bottom = b[3].as("px");

    // Build the slices using AM code
    var idMk = charIDToTypeID("Mk  ");
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putClass(stringIDToTypeID("slice"));
    desc.putReference(charIDToTypeID("null"), ref);
    var sliceDesc = new ActionDescriptor();
    sliceDesc.putEnumerated(
        charIDToTypeID("Type"),
        stringIDToTypeID("sliceType"),
        stringIDToTypeID("user")
    );
    var rect = new ActionDescriptor();
    rect.putUnitDouble(charIDToTypeID("Top "), charIDToTypeID("#Pxl"), top);
    rect.putUnitDouble(charIDToTypeID("Left"), charIDToTypeID("#Pxl"), left);
    rect.putUnitDouble(charIDToTypeID("Btom"), charIDToTypeID("#Pxl"), bottom);
    rect.putUnitDouble(charIDToTypeID("Rght"), charIDToTypeID("#Pxl"), right);
    sliceDesc.putObject(charIDToTypeID("At  "), charIDToTypeID("Rctn"), rect);
    desc.putObject(charIDToTypeID("Usng"), stringIDToTypeID("slice"), sliceDesc);
    executeAction(idMk, desc, DialogModes.NO);

    // Restore the original ruler units
    app.preferences.rulerUnits = originalRuler;
}

function renameActiveSliceToLayerName() {
    var layerName = app.activeDocument.activeLayer.name;
    var idsetd = charIDToTypeID("setd");
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putEnumerated(
        stringIDToTypeID("slice"),
        charIDToTypeID("Ordn"),
        charIDToTypeID("Trgt")
    );
    desc.putReference(charIDToTypeID("null"), ref);
    var inner = new ActionDescriptor();
    inner.putString(charIDToTypeID("Nm  "), layerName);
    desc.putObject(
        charIDToTypeID("T   "),
        stringIDToTypeID("slice"),
        inner
    );
    executeAction(idsetd, desc, DialogModes.NO);
}

function hasActiveLayer() {
    try {
        // selected layer check by jazz-y
        var s2t = stringIDToTypeID;
        var r = new ActionReference();
        r.putProperty(s2t('property'), s2t('targetLayers'));
        r.putEnumerated(s2t("document"), s2t("ordinal"), s2t("targetEnum"));
        var desc = executeActionGet(r);
        var targetLayers = desc.getList(s2t('targetLayers'));
        return targetLayers.count > 0;
    } catch (e) {
        return false;
    }
}

 

Delta2487Author
Known Participant
November 22, 2025

@Stephen Marsh This script, option A, works very well. Even when I manually created a slice, it captured and renamed each selected layer perfectly. I didn't know how to manipulate multiple layers with Action Manager, so thank you very much for the script.

Stephen Marsh
Community Expert
Community Expert
November 22, 2025
quote

@Stephen Marsh This script, option A, works very well. Even when I manually created a slice, it captured and renamed each selected layer perfectly. I didn't know how to manipulate multiple layers with Action Manager, so thank you very much for the script.


By @Delta2487

 

You're welcome. My preference would be the "B" script as it's simpler/faster, however, both the "A" and "B" version should provide idential results when it comes to the slice bounds (just be careful if using styles/FX such as strokes, glows or shadows as these are not taken into account and extra steps would be required in ExtendScript for the full bounds).

 

I have reused this basic script code framework for multiple selected layers (thanks to @jazz-y) for many different script requests on the forum, it's just a matter of swapping out the referenced functions in the section clearly commented to "do stuff to selected layers". As a bonus this script also offers a single undo history step.

Stephen Marsh
Community Expert
Community Expert
November 21, 2025

@Delta2487 

 

You would use the ScriptingListener plugin to record the AM code as a start point.

 

A couple of ideas:

 

A) A script to manually create a slice to get the base code, then swap out the recorded placeholder coordinate values with the layer bounds values. This would be run over all selected layers.

 

or

 

B) A script to process each selected layer and run the Layer > New Layer Based Slice command

Myra Ferguson
Community Expert
Community Expert
November 21, 2025

There's a feature in Photoshop that lets you generate images from your layers when they are named in a particular way and you go to File > Automate > Generator Plugins. The naming convention for the layers also lets you set certain parameters such as quality, size, format, and scaling. 

 

Here's a page that details the naming and the process:

https://helpx.adobe.com/photoshop/using/generate-assets-layers.html

 

Delta2487Author
Known Participant
November 22, 2025

@Myra Ferguson  Thanks for the help, but what I wanted was to create slices, not export the images. Anyway, thank you very much.