Photoshop script to randomly show 1 layer per group based on some condition, save file and save layer name shown in text file

I am trying to modify some script given here, allowing to show 1 layer of each photoshop layer group to allow for some conditional formatting for my project. What I mean is, I have groups of layer from which I only 1 to show 1 image, but not necessarily all group at once.

For example, I have groups containing the text with various visual: "All" "Each" "Fruit" "Fruits"

I would like to be able to modify to code for it to randomly pick either the "All" or "Each" group and display one of the image of its group, without displaying the on in the other. After this step, I would need 1 layer of the group "Fruit" to be shown if the initial group picked was "Each, or the group "Fruits" to be used if the initial was "All".

I've honestly no idea how to add such conditional use to the initial script I found:

function Visible() {


var Grps = app.activeDocument.layerSets; // loops through all groups
  for(var i = 0; i < Grps.length; i++){
    var tmp = app.activeDocument.layerSets[i].layers.length;
    app.activeDocument.layerSets[i].visible=true;
    var groupChildArr = app.activeDocument.layerSets[i].layers;
    var randLays = Math.floor(Math.random() * tmp);
    groupChildArr[randLays].visible = true;
    Save();
  }
  Revert();
}

function Save() {
  var outFolder = app.activeDocument; // psd name
  var outPath = outFolder.path;
  var fName = "PNG";   // define folder name
  var f = new Folder(outPath + "/" + fName);
  if ( ! f.exists ) {
    f.create()
  }
  var saveFile = new File(outPath + "/" + fName +"/" + "Pattern_" +  num + ".png");
  pngSaveOptions = new PNGSaveOptions();
  pngSaveOptions.interlaced = false;
  app.activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);
}


function Revert(){
   var idRvrt = charIDToTypeID( "Rvrt" );
   executeAction( idRvrt, undefined, DialogModes.NO );
}

var count = prompt("How many patterns you want","");
for (var x=0 ; x<count;x++){
  var num = x+1;
  Visible();
}

After each successful image saved, I would like to add which variations were used in a text file, 1 line per file, therefore without overwriting previous text added by the script.

    function saveTxt(txt)
    {
        var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');
        var Ext = decodeURI(app.activeDocument.name).replace(/^.*\./,'');
        if (Ext.toLowerCase() != 'psd')
            return;
    
        var Path = app.activeDocument.path;
        var saveFile = File(Path + "/" "Data.txt");

//  Removed myself to avoid overwriting file?    
//        if(saveFile.exists)
//            saveFile.remove();
    
        saveFile.encoding = "UTF8";
        saveFile.open("e", "TEXT", "????");
        saveFile.writeln(txt);
        saveFile.close();
    }

Data saved could be as simple as including name and version of layer shown such as :

"Background 3","All 5","Fruits 2"

"Background 2","All 9","Fruits 4"

etc.

It could also be written into a .csv instead. To note, I don't mind naming each version of layers in a group accordingly if it would ease writing down which layer was used.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum