• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
4

Script for merging each main groups/artboard into separate layers, with auto selection.

Community Beginner ,
Nov 28, 2023 Nov 28, 2023

Copy link to clipboard

Copied

Hi

I need to run a script into a save action, and could use some help to rewrite a script.

The action i need the script to do, is merging each main group/artboard into seperate layers.


Is it possible to rewrite the script, so that photoshop selects the top layer (In my case a Artboard group) ?
When using "select --> All layers" i get a error (See attached) But if i manually select the top artboard, the scripts runs perfect!

This is my current script, and i think i just need to add a "select top group" kinda code.


target
photoshop;
while(app.activeDocument.layerSets.length){
activeDocument.activeLayer = app.activeDocument.layerSets[0];
executeAction(stringIDToTypeID("newPlacedLayer"), new ActionDescriptor(), DialogModes.NO);
activeDocument.activeLayer.rasterize(RasterizeType.ENTIRELAYER);
}

 

Screenshot 2023-11-28 at 11.07.09.png

 

 

TOPICS
Actions and scripting , macOS

Views

471

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Nov 28, 2023 Nov 28, 2023

Copy link to clipboard

Copied

Can you provide before/after screenshots of the layers panel? A sample PSD would also be helpful too.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 28, 2023 Nov 28, 2023

Copy link to clipboard

Copied

Hi Stephen

Attached is 2 screenshots. The first is how my files would be build in the future, and it could spand from 2 to approx 10 artboards. The other screenshot is the intended end goal. This is possible with the script, only if i manually select the top group, and i cant record that in a action in a good way. So is it possible to write the script, so that the top group is always selected, not taking account of the name. ?

I've also attached a sample PSD.

Thanks 🙂

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 28, 2023 Nov 28, 2023

Copy link to clipboard

Copied

Instead of the original script, here are two alternative scripts that you may wish to try, which don't require any layers to be selected. I have added the extra code to rasterize the SO layer:

 

/* convert all top level layers and layer sets to smart objects AM CODE.jsx

by jazz-y
Smart Objects in separate layers
https://community.adobe.com/t5/photoshop/smart-objects-in-separate-layers/td-p/11137197
*/

#target photoshop

s2t = stringIDToTypeID;
t2s = typeIDToStringID;

(r = new ActionReference()).putProperty(s2t('property'), p = s2t('hasBackgroundLayer'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'))
var indexFrom = executeActionGet(r).getBoolean(p) ? 0 : 1;

(r = new ActionReference()).putProperty(s2t('property'), p = s2t('numberOfLayers'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'))
var indexTo = executeActionGet(r).getInteger(p);

var layers = {}
parseLayerSets(indexFrom, indexTo, layers)

for (o in layers) {
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('layerKind'));
    r.putIdentifier(s2t("layer"), layers[o].id);
    var kind = executeActionGet(r).getInteger(p)
    if (kind != 2 && kind != 5) {
        (r = new ActionReference()).putIdentifier(s2t("layer"), layers[o].id);
        (d = new ActionDescriptor()).putReference(s2t("null"), r);
        executeAction(s2t("select"), d, DialogModes.NO)
        try {
            executeAction(s2t("newPlacedLayer"), undefined, DialogModes.NO);
            activeDocument.activeLayer.rasterize(RasterizeType.ENTIRELAYER);
        } catch (e) {
            alert(e)
        }
    }
}

function parseLayerSets(from, to, parentObj) {
    for (var i = from; i <= to; i++) {
        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('layerSection'));
        r.putIndex(s2t("layer"), i)
        var section = t2s(executeActionGet(r).getEnumerationValue(p))

        if (section == 'layerSectionEnd') {
            i = parseLayerSets(i + 1, to, parentObj[i] = {})
            continue;
        }

        var properties = {};
        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('layerID'));
        r.putIndex(s2t("layer"), i);
        properties.id = executeActionGet(r).getInteger(p);

        if (section == 'layerSectionStart') {
            for (o in properties) {
                parentObj[o] = properties[o]
            }
            return i;
        } else {
            parentObj[i] = properties
        }
    }
}

 

 

Or perhaps this one:

 

/* convert all top level layers and layer sets to smart objects.jsx

Smart Objects in separate layers
https://community.adobe.com/t5/photoshop/smart-objects-in-separate-layers/td-p/11137197
*/

function main() {

    if (!documents.length) {
        alert('There are no documents open!');
    } else {
        processAllLayersAndSets(app.activeDocument);
    }

    function processAllLayersAndSets(obj) {

        // Process all layers and layer sets
        // Change the following 2 entries of "obj.layers" to "obj.artLayers" to exclude layer sets
        for (var al = obj.layers.length - 1; 0 <= al; al--) {
            app.activeDocument.activeLayer = obj.layers[al];

            newPlacedLayer();
            activeDocument.activeLayer.rasterize(RasterizeType.ENTIRELAYER);

        }

        // Process Layer Set Layers 
        for (var ls = obj.layerSets.length - 1; 0 <= ls; ls--) {
            processAllLayersAndSets(obj.layerSets[ls]);

            newPlacedLayer();
            activeDocument.activeLayer.rasterize(RasterizeType.ENTIRELAYER);

        }
    }

    // Convert to smart object (Cleaned AM code)
    function newPlacedLayer() {
        var s2t = function (s) {
            return app.stringIDToTypeID(s);
        };
        executeAction(s2t("newPlacedLayer"), undefined, DialogModes.NO);
    }
}

activeDocument.suspendHistory('Smart Objects from Layers & Sets', 'main()');

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 28, 2023 Nov 28, 2023

Copy link to clipboard

Copied

LATEST
quote

Is it possible to rewrite the script, so that photoshop selects the top layer (In my case a Artboard group) ?

 

 


By @Simon3344797511r3

 

Is the problem that no artboards or layers are currently selected before running the script (your sample PSD had a layer selected, so I wasn't sure)?

 

If so, you can add the following code to ensure that the very top layer is selected:

 

activeDocument.activeLayer = activeDocument.layerSets[0].layers[0];
activeDocument.activeLayer = activeDocument.activeLayer.parent;

 

I'm guessing that you only need this once, before the while loop to ensure that the top artboard is selected, before the script loops over and rasterizes the artboards.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines