Skip to main content
This topic has been closed for replies.
Correct answer SuperMerlin

Please try this...

#target photoshop;

app.bringToFront();

if(documents.length) app.activeDocument.suspendHistory('Shuffle Layers', 'main()');

function main(){

try{

var list = getSelectedLayersIdx();

if(list.length < 2){

    alert("You need to select two or more layers!");

    return;

    }

var IDs = [];

for (var a in list){IDs.push(getLayerIDbyIndex(Number(list)));}

var shuffleIDs = shuffle(IDs);

for(var k = 0;k<shuffleIDs.length;k++){

    selectLayerById(Number(shuffleIDs));

    moveActiveLayerToIndex (Number(shuffleIDs[(shuffleIDs.length - (k + 1))]));

    }

}catch(e){$.writeln(e + "\n" + e.line);}

};

function moveActiveLayerToIndex (ID){

var desc2 = new ActionDescriptor();

        var ref3 = new ActionReference();

        ref3.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );

    desc2.putReference( charIDToTypeID('null'), ref3 );

        var ref4 = new ActionReference();

        ref4.putIndex( charIDToTypeID('Lyr '), getLayerIndexByID(ID) );

    desc2.putReference( charIDToTypeID('T   '), ref4 );

    desc2.putBoolean( charIDToTypeID('Adjs'), false );

    desc2.putInteger( charIDToTypeID('Vrsn'), 5 );

    executeAction( charIDToTypeID('move'), desc2, DialogModes.NO );

};

function selectLayerById(id,add){

var ref = new ActionReference();

ref.putIdentifier(charIDToTypeID('Lyr '), id);

var desc = new ActionDescriptor();

desc.putReference(charIDToTypeID("null"), ref );

if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );

desc.putBoolean( charIDToTypeID( "MkVs" ), false );

try{

executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );

}catch(e){}

};

function getLayerIndexByID(ID){

var ref = new ActionReference();

ref.putIdentifier( charIDToTypeID('Lyr '), ID );

try{

activeDocument.backgroundLayer;

return executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1;

}catch(e){

return executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ));

}

};

function shuffle(array) {

  var currentIndex = array.length, temporaryValue, randomIndex;

  while (0 !== currentIndex) {

    randomIndex = Math.floor(Math.random() * currentIndex);

    currentIndex -= 1;

    temporaryValue = array[currentIndex];

    array[currentIndex] = array[randomIndex];

    array[randomIndex] = temporaryValue;

  }

  return array;

};

function getSelectedLayersIdx(){

      var selectedLayers = new Array();

      var backGroundCounter = 1;

            if(activeDocument.artLayers.length > 0){

            backGroundCounter = activeDocument.artLayers[activeDocument.artLayers.length - 1].isBackgroundLayer ? 0 : 1;

            }

      var ref = new ActionReference();

      ref.putProperty(charIDToTypeID("Prpr"), stringIDToTypeID("targetLayers"));

      ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

      var desc = executeActionGet(ref);

      if( desc.hasKey( stringIDToTypeID( "targetLayers" ) ) ){

         desc = desc.getList( stringIDToTypeID( "targetLayers" ));

          var c = desc.count;

          var selectedLayers = new Array();

          for(var i=0;i<c;i++){

               selectedLayers.push(  desc.getReference( i ).getIndex() +backGroundCounter );

          }

      if(app.version.match(/^\d+/) > 15) return selectedLayers ;

       }else{

           if(app.version.match(/^\d+/) > 15) return selectedLayers ;

         var ref = new ActionReference();

         ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));

         ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

         if(!backGroundCounter){

            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )) -1);

            }else{

                selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));

                }

     var vis = app.activeDocument.activeLayer.visible;

        if(vis == true) app.activeDocument.activeLayer.visible = false;

        var desc9 = new ActionDescriptor();

    var list9 = new ActionList();

    var ref9 = new ActionReference();

    ref9.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

    list9.putReference( ref9 );

    desc9.putList( charIDToTypeID("null"), list9 );

    executeAction( charIDToTypeID("Shw "), desc9, DialogModes.NO );

    if(app.activeDocument.activeLayer.visible == false) selectedLayers.shift();

        app.activeDocument.activeLayer.visible = vis;

      }

      return selectedLayers;

};

function getLayerIDbyIndex(idx) {

var ref = new ActionReference();

ref.putIndex( charIDToTypeID( "Lyr " ), idx );

return executeActionGet(ref).getInteger(stringIDToTypeID( "layerID" ));

}

function getNameByID(ID) {

    var ref = new ActionReference();

    ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Nm  " ));

    ref.putIdentifier( charIDToTypeID( "Lyr " ), ID );

    return executeActionGet(ref).getString(charIDToTypeID( "Nm  " ));;

}

Mubashar4Author
Known Participant
August 10, 2017

SuperMerlin​ Amazing! Just got what I want.. Thank you so much

SuperMerlin
SuperMerlinCorrect answer
Inspiring
August 10, 2017

Please try this...

#target photoshop;

app.bringToFront();

if(documents.length) app.activeDocument.suspendHistory('Shuffle Layers', 'main()');

function main(){

try{

var list = getSelectedLayersIdx();

if(list.length < 2){

    alert("You need to select two or more layers!");

    return;

    }

var IDs = [];

for (var a in list){IDs.push(getLayerIDbyIndex(Number(list)));}

var shuffleIDs = shuffle(IDs);

for(var k = 0;k<shuffleIDs.length;k++){

    selectLayerById(Number(shuffleIDs));

    moveActiveLayerToIndex (Number(shuffleIDs[(shuffleIDs.length - (k + 1))]));

    }

}catch(e){$.writeln(e + "\n" + e.line);}

};

function moveActiveLayerToIndex (ID){

var desc2 = new ActionDescriptor();

        var ref3 = new ActionReference();

        ref3.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );

    desc2.putReference( charIDToTypeID('null'), ref3 );

        var ref4 = new ActionReference();

        ref4.putIndex( charIDToTypeID('Lyr '), getLayerIndexByID(ID) );

    desc2.putReference( charIDToTypeID('T   '), ref4 );

    desc2.putBoolean( charIDToTypeID('Adjs'), false );

    desc2.putInteger( charIDToTypeID('Vrsn'), 5 );

    executeAction( charIDToTypeID('move'), desc2, DialogModes.NO );

};

function selectLayerById(id,add){

var ref = new ActionReference();

ref.putIdentifier(charIDToTypeID('Lyr '), id);

var desc = new ActionDescriptor();

desc.putReference(charIDToTypeID("null"), ref );

if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );

desc.putBoolean( charIDToTypeID( "MkVs" ), false );

try{

executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );

}catch(e){}

};

function getLayerIndexByID(ID){

var ref = new ActionReference();

ref.putIdentifier( charIDToTypeID('Lyr '), ID );

try{

activeDocument.backgroundLayer;

return executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1;

}catch(e){

return executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ));

}

};

function shuffle(array) {

  var currentIndex = array.length, temporaryValue, randomIndex;

  while (0 !== currentIndex) {

    randomIndex = Math.floor(Math.random() * currentIndex);

    currentIndex -= 1;

    temporaryValue = array[currentIndex];

    array[currentIndex] = array[randomIndex];

    array[randomIndex] = temporaryValue;

  }

  return array;

};

function getSelectedLayersIdx(){

      var selectedLayers = new Array();

      var backGroundCounter = 1;

            if(activeDocument.artLayers.length > 0){

            backGroundCounter = activeDocument.artLayers[activeDocument.artLayers.length - 1].isBackgroundLayer ? 0 : 1;

            }

      var ref = new ActionReference();

      ref.putProperty(charIDToTypeID("Prpr"), stringIDToTypeID("targetLayers"));

      ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

      var desc = executeActionGet(ref);

      if( desc.hasKey( stringIDToTypeID( "targetLayers" ) ) ){

         desc = desc.getList( stringIDToTypeID( "targetLayers" ));

          var c = desc.count;

          var selectedLayers = new Array();

          for(var i=0;i<c;i++){

               selectedLayers.push(  desc.getReference( i ).getIndex() +backGroundCounter );

          }

      if(app.version.match(/^\d+/) > 15) return selectedLayers ;

       }else{

           if(app.version.match(/^\d+/) > 15) return selectedLayers ;

         var ref = new ActionReference();

         ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));

         ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

         if(!backGroundCounter){

            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )) -1);

            }else{

                selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));

                }

     var vis = app.activeDocument.activeLayer.visible;

        if(vis == true) app.activeDocument.activeLayer.visible = false;

        var desc9 = new ActionDescriptor();

    var list9 = new ActionList();

    var ref9 = new ActionReference();

    ref9.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

    list9.putReference( ref9 );

    desc9.putList( charIDToTypeID("null"), list9 );

    executeAction( charIDToTypeID("Shw "), desc9, DialogModes.NO );

    if(app.activeDocument.activeLayer.visible == false) selectedLayers.shift();

        app.activeDocument.activeLayer.visible = vis;

      }

      return selectedLayers;

};

function getLayerIDbyIndex(idx) {

var ref = new ActionReference();

ref.putIndex( charIDToTypeID( "Lyr " ), idx );

return executeActionGet(ref).getInteger(stringIDToTypeID( "layerID" ));

}

function getNameByID(ID) {

    var ref = new ActionReference();

    ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Nm  " ));

    ref.putIdentifier( charIDToTypeID( "Lyr " ), ID );

    return executeActionGet(ref).getString(charIDToTypeID( "Nm  " ));;

}

pixxxelschubser
Community Expert
Community Expert
August 10, 2017

Hi @SuperMerlin,

I didn't try your code yet.

But I don't understand this if-clause

  1.       if(app.version.match(/^\d+/) > 15) return selectedLayers ; 
  2.        }else{  
  3.            if(app.version.match(/^\d+/) > 15) return selectedLayers ; 

if version >15 do something

or

if version >15 do the same

???

JJMack
Community Expert
Community Expert
August 10, 2017

If you look at the else code you will see the code uses a diffent method in not > 15 it does not return it continues on. Also the way JIVE messes up formatting it is difficult to see where the first If is in the scripts structure. IS that else for that fits if you show?

The function always returns selected layers the are three return point in the function.  Reformatted the function  you will see that else is not for the first if.

JJMack
JJMack
Community Expert
Community Expert
August 10, 2017

Adobe Photoshop Scripting | Adobe Developer Connection 

User here try to help you.  We all have different abilities.   You can use script other provide you should not expect that someone will be willing to program a script that will work with the next document structure you come up with and automate what you want to do.   You would need to design a script to implement your what you want to do.  You would need to design some document structure that would work with the script you designed and programmed.  Scripting is programming not a simple recorded and edit action.

Script can be very powerful. If you look at a Plug-in script like Image Processor Pro you will see it contains 10,000+ lines of code that is not something you develop quickly.  Layer Comps to files is over 2,500 line of code and much more flexible then the custom script SuperM programmed for you.   Scripting is powerful programming a script is more work than you can handle. Scripting Photoshop is not simple and easy.  Some thing can be done with little code but thay are usually very simple and specific.

JJMack
Mubashar4Author
Known Participant
August 10, 2017

JJMack​ These two script (this one, and last one you replied to earlier thread) i direly needed it! I use various script, and these are lot more fast even from photoshop actions.. I use them frequently in my work flow to deliver work to various clients.

JJMack
Community Expert
Community Expert
August 10, 2017

If you ask here User will tell you if it can be done with an Action the action will be fasteter than a script. Actions are canned Photoshop steps with the exception of a few conditional codition steps. They  have no plane text logic that  needs to be interpited into machine code and executed.  Action are faster than scripts. However they are not as powerful and flexable as a script can be. So yes you would need a script to loop through your document to do what you want.  If you were asking for help writting a script you will find many willing to help you learn Photoshop scripting better.

IMO you have been very lucky that SuperM has done your work for you for free. You are not actually asking for help here which is what these forums are for. You are  asking for other to work for you for free for you are unable the do what you want to do and do not seem to want to learn.  For me that not what there forums were created for.  You most likely do not even read the code posted here for you.  You just use it and do not undetstans alert messages a script may give you. I hope your luck  will continues.  However if you come up with something that is very complex you will most likely fine you luck has run out.

JJMack