Skip to main content
Morthezoo
Participant
July 21, 2019
Question

Picking random layers from each group.

  • July 21, 2019
  • 4 replies
  • 749 views

So i am making a document and I need to pick single random layer from each group to show. how do I do that? Explain like I'm 5.

    This topic has been closed for replies.

    4 replies

    Chuck Uebele
    Community Expert
    Community Expert
    July 21, 2019

    This script will pick one layer from each layer set, but if there are nested layer sets, it might select one of those.

    var doc = activeDocument;

    var layA = new Array();

    var selLayA = new Array();

    findSets (doc)

    function findSets(gp){

        for(var i=0;i<gp.layers.length;i++){

            if(gp.layers.typename == 'LayerSet'){

                layA.push(gp.layers);

                findSets (gp.layers);

                }

            }

        }

    for(var i=0;i<layA.length;i++){

        var curLay = doc.activeLayer = layA;

        var ranNum = Math.floor(Math.random()*curLay.layers.length)

        selLayA.push(curLay.layers[ranNum].name);

        }

    alert(selLayA)

    jane-e
    Community Expert
    Community Expert
    July 21, 2019

    Hi Mort,

    How random are the layers you need to show? What is your purpose?

    If you just need to show and hide certain layers, you can assign a color by right-clicking and filter to Color at the top of the Layers panel. Or they may meet other criteria, such as you have multiple-selected them or they share attributes or modes or effects, or they have some of the same letters or numbers in the names etc., and you can filter to those criteria.

    If you are always hiding and showing the same layers, use Layer Comps. Create a new layer comp (Window menu) for each state and switch between them in the Layer Comps panel.

    Details here: Photoshop layer comps

    When we know what you are trying to do, we can better advise.

    ~ Jane

    Chuck Uebele
    Community Expert
    Community Expert
    July 21, 2019

    This can be done with Photoshop's extendscript. What do you want done with each selected layer?

    Bob_Hallam
    Legend
    July 21, 2019

    I would do this in XMPie.  This is a straight up programming function, nothing thats easily or commonly done in Photoshop because there are better tools available that lend themselves to this type of work.    

    Multichannel and VDP Marketing Software | XMPie

    ICC programmer and developer, Photographer, artist and color management expert, Print standards and process expert.