Skip to main content
Participant
February 14, 2020
Question

Scripting question, is it possible to create a slice on a layer with javascript?

  • February 14, 2020
  • 1 reply
  • 326 views

 

Ideally I'd like to write a script that selects a layer by name, which I can do easy enough, but is there a way to create a slice on that layer either via the "New Layer Based Slice" option under the Layer menu or just create a slice with javascript. Also is there a way to name the slice? Ideally I'd like to take the text of a sibling layer text layer and append it to the slice name. Even if this requires an eventlistener that is fine.

 

I'm mostly looking to see if this is possible but any code suggestions are helpful too thanks

This topic has been closed for replies.

1 reply

Legend
February 14, 2020
make_slice("THE SLICE");

function make_slice(name)
    {
    try {
        var d = new ActionDescriptor();
        var r = new ActionReference();
        r.putClass(stringIDToTypeID("slice"));
        d.putReference(stringIDToTypeID("null"), r);
        var d1 = new ActionDescriptor();
        d1.putEnumerated(stringIDToTypeID("type"), stringIDToTypeID("sliceType"), stringIDToTypeID("layer"));
        var r1 = new ActionReference();
        r1.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
        d1.putReference(stringIDToTypeID("layer"), r1);
        d.putObject(stringIDToTypeID("using"), stringIDToTypeID("slice"), d1);
        executeAction(stringIDToTypeID("make"), d, DialogModes.NO);

        var d = new ActionDescriptor();
        var r = new ActionReference();
        r.putEnumerated(stringIDToTypeID("slice"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
        d.putReference(stringIDToTypeID("null"), r);
        var d1 = new ActionDescriptor();
        d1.putString(stringIDToTypeID("name"), name);
        d1.putEnumerated(stringIDToTypeID("sliceImageType"), stringIDToTypeID("sliceImageType"), stringIDToTypeID("image"));
        d.putObject(stringIDToTypeID("to"), stringIDToTypeID("slice"), d1);
        executeAction(stringIDToTypeID("set"), d, DialogModes.NO);
        }
    catch (e) { alert(e); }
    }
Participant
February 27, 2020

That's awesome thank you so much!