Skip to main content
Inspiring
March 23, 2023
Answered

Script to move each selected area to new layer

  • March 23, 2023
  • 2 replies
  • 1196 views

It is possible to  to move each manually selected area to a new layer ?

(similar to layer via cut)

 

 

This topic has been closed for replies.
Correct answer jazz-y

find line

 

executeAction(s2t('cutToLayer'), undefined, DialogModes.NO);

 

and replace cutToLayer by copyToLayer:

 

executeAction(s2t('copyToLayer'), undefined, DialogModes.NO)

 

2 replies

Legend
March 25, 2023

try this:

 

#target photoshop
activeDocument.suspendHistory('Divide by selection', 'main()')

function main() {
    var doc = new AM('document'),
        lr = new AM('layer'),
        pth = new AM('path'),
        targetLayer = lr.getProperty('layerID');

    if (doc.hasProperty('selection')) lr.makePathFromSelection(2);

    if (doc.getProperty('numberOfPaths')) {
        var pathContents = pth.getProperty('pathContents').value.getList(stringIDToTypeID('pathComponents'))
        for (var i = 0; i < pathContents.count; i++) {
            pth.makePathFromSubpath(pathContents.getObjectValue(i))
            pth.makeSelectionFromPath()
            lr.select(targetLayer);
            lr.cutToLayer();
        }
        lr.select(targetLayer)
        lr.clearSelection()
        pth.delete()
    }
}

function AM(target, order) {
    var s2t = stringIDToTypeID,
        t2s = typeIDToStringID;
    target = target ? s2t(target) : null;
    this.getProperty = function (property, id, idxMode) {
        property = s2t(property);
        (r = new ActionReference()).putProperty(s2t('property'), property);
        id != undefined ? (idxMode ? r.putIndex(target, id) : r.putIdentifier(target, id)) :
            r.putEnumerated(target, s2t('ordinal'), order ? s2t(order) : s2t('targetEnum'));
        return getDescValue(executeActionGet(r), property)
    }
    this.hasProperty = function (property, id, idxMode) {
        property = s2t(property);
        (r = new ActionReference()).putProperty(s2t('property'), property);
        id ? (idxMode ? r.putIndex(target, id) : r.putIdentifier(target, id))
            : r.putEnumerated(target, s2t('ordinal'), order ? s2t(order) : s2t('targetEnum'));
        return executeActionGet(r).hasKey(property)
    }
    this.select = function (id) {
        var r = new ActionReference();
        if (typeof id == 'number') r.putIdentifier(target, id) else r.putName(target, id);
        (d = new ActionDescriptor()).putReference(s2t('null'), r);
        executeAction(s2t('select'), d, DialogModes.NO);
    }
    this.makeSelectionFromPath = function () {
        (r = new ActionReference()).putProperty(s2t('channel'), s2t('selection'));
        (d = new ActionDescriptor()).putReference(s2t('null'), r);
        (r1 = new ActionReference()).putProperty(s2t('path'), s2t('workPath'));
        d.putReference(s2t('to'), r1);
        d.putBoolean(s2t('vectorMaskParams'), true);
        executeAction(s2t('set'), d, DialogModes.NO);
    }
    this.deleteCurrentPath = function () {
        (r = new ActionReference()).putProperty(s2t('path'), s2t('workPath'));
        (d = new ActionDescriptor()).putReference(s2t('null'), r);
        executeAction(s2t('delete'), d, DialogModes.NO);
    }
    this.makePathFromSelection = function (tolerance) {
        tolerance = tolerance ? tolerance : 10;
        (r = new ActionReference()).putClass(s2t('path'));
        (d = new ActionDescriptor()).putReference(s2t('null'), r);
        (r1 = new ActionReference()).putProperty(s2t('selectionClass'), s2t('selection'));
        d.putReference(s2t('from'), r1);
        d.putUnitDouble(s2t('tolerance'), s2t('pixelsUnit'), tolerance);
        executeAction(s2t('make'), d, DialogModes.NO);
    }
    this.makePathFromSubpath = function (pth) {
        (r = new ActionReference()).putProperty(stringIDToTypeID("path"), stringIDToTypeID("workPath"));
        (d = new ActionDescriptor()).putReference(stringIDToTypeID("null"), r);
        (l = new ActionList()).putObject(stringIDToTypeID("pathComponent"), pth);
        d.putList(stringIDToTypeID("to"), l);
        executeAction(stringIDToTypeID("set"), d, DialogModes.NO);
    }
    this.clearSelection = function () {
        (r = new ActionReference()).putProperty(s2t('channel'), s2t('selection'));
        (d = new ActionDescriptor()).putReference(s2t('null'), r);
        d.putEnumerated(s2t('to'), s2t('ordinal'), s2t('none'));
        executeAction(s2t('set'), d, DialogModes.NO);
    }
    this.delete = function (name) {
        var r = new ActionReference();
        if (name) r.putName(target, name) else r.putEnumerated(target, s2t('ordinal'), s2t('targetEnum'));
        (d = new ActionDescriptor()).putReference(s2t('null'), r);
        executeAction(s2t('delete'), d, DialogModes.NO);
    }
    this.cutToLayer = function () {
        executeAction(s2t('cutToLayer'), undefined, DialogModes.NO);
    }
    function getDescValue(d, p) {
        switch (d.getType(p)) {
            case DescValueType.OBJECTTYPE: return { type: t2s(d.getObjectType(p)), value: d.getObjectValue(p) };
            case DescValueType.LISTTYPE: return d.getList(p);
            case DescValueType.REFERENCETYPE: return d.getReference(p);
            case DescValueType.BOOLEANTYPE: return d.getBoolean(p);
            case DescValueType.STRINGTYPE: return d.getString(p);
            case DescValueType.INTEGERTYPE: return d.getInteger(p);
            case DescValueType.LARGEINTEGERTYPE: return d.getLargeInteger(p);
            case DescValueType.DOUBLETYPE: return d.getDouble(p);
            case DescValueType.ALIASTYPE: return d.getPath(p);
            case DescValueType.CLASSTYPE: return d.getClass(p);
            case DescValueType.UNITDOUBLE: return (d.getUnitDoubleValue(p));
            case DescValueType.ENUMERATEDTYPE: return { type: t2s(d.getEnumerationType(p)), value: t2s(d.getEnumerationValue(p)) };
            default: break;
        };
    }
}

 

 * intsead of selection, you can draw a crop zone with the pen tool - every closed path will be copied.

 
 

 

 

Andrew Bold
Known Participant
August 29, 2023

Hi!
Would it take much time to modify the script a bit?
All the mechanics are the same, except that the selected areas are not cut to new layers, but copied?
And the original layer would remain untouched.

jazz-yCorrect answer
Legend
August 29, 2023

find line

 

executeAction(s2t('cutToLayer'), undefined, DialogModes.NO);

 

and replace cutToLayer by copyToLayer:

 

executeAction(s2t('copyToLayer'), undefined, DialogModes.NO)

 

Stephen Marsh
Community Expert
Community Expert
March 23, 2023

There is a script that uses transparency to isolate elements to individual layers:

 

https://photoshopscripts.wordpress.com/2012/12/09/split-to-layers/

 

siomospAuthor
Inspiring
March 23, 2023

Thank you, I know this script, I want to use manually selected areas!

At many cases it generates hundreds of layers

 

 

Stephen Marsh
Community Expert
Community Expert
March 23, 2023

You have to adjust the tolerance/threshold value, but I understand.

 

I am not aware of an existing script or method to easily do this.