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

Shuffle selected layers only

Community Beginner ,
Aug 10, 2017 Aug 10, 2017

Copy link to clipboard

Copied

Hi.. Can you please tell me the script to shuffle randomly "Selected Layers" only. Which may or may not be in a layerset. I just want to shuffle only my selected layers, not the whole document layers. Thank you very much

c.pfaffenbichlerSuperMerlin

TOPICS
Actions and scripting

Views

4.1K

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

correct answers 1 Correct answer

Guide , Aug 10, 2017 Aug 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(

...

Votes

Translate

Translate
Adobe
Community Expert ,
Aug 10, 2017 Aug 10, 2017

Copy link to clipboard

Copied

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

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 ,
Aug 10, 2017 Aug 10, 2017

Copy link to clipboard

Copied

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.

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 ,
Aug 10, 2017 Aug 10, 2017

Copy link to clipboard

Copied

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

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
Guide ,
Aug 10, 2017 Aug 10, 2017

Copy link to clipboard

Copied

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  " ));;

}

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 ,
Aug 10, 2017 Aug 10, 2017

Copy link to clipboard

Copied

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

???

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 ,
Aug 10, 2017 Aug 10, 2017

Copy link to clipboard

Copied

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.

Capture.jpg

JJMack

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 ,
Aug 10, 2017 Aug 10, 2017

Copy link to clipboard

Copied

JJMack  schrieb

…  Reformatted the function  you will see that else is not for the first if.

OK. I see. It's clear now for me.

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
Guide ,
Aug 10, 2017 Aug 10, 2017

Copy link to clipboard

Copied

Photoshop versions upto and including  CS6 need to do a further test to see if only one layer or none is selected, the CC versions have fixed this bug so further testing for a single layer is not required.

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 ,
Aug 10, 2017 Aug 10, 2017

Copy link to clipboard

Copied

Yes some bugs are fixed new bugs are introduces some by design and other bugs seems to be deemed OK.  Adobe management's  way of supporting Photoshop.

JJMack

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
New Here ,
Mar 08, 2018 Mar 08, 2018

Copy link to clipboard

Copied

Is it possible to keep selected the layers after suffle?

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
Guide ,
Mar 08, 2018 Mar 08, 2018

Copy link to clipboard

Copied

#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))]));

    }

deselectLayers();

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

    selectLayerById(Number(shuffleIDs),true);

    }

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

};

function deselectLayers() {

    var desc01 = new ActionDescriptor();

        var ref01 = new ActionReference();

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

    desc01.putReference( charIDToTypeID('null'), ref01 );

    executeAction( stringIDToTypeID('selectNoLayers'), desc01, DialogModes.NO );

};

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{

         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.putProperty( charIDToTypeID("Prpr") , stringIDToTypeID( "layerID" ));

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  " ));;

}

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
New Here ,
Mar 08, 2018 Mar 08, 2018

Copy link to clipboard

Copied

LATEST

Amazing. Thank you so much SuperMerlin!

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 ,
Aug 10, 2017 Aug 10, 2017

Copy link to clipboard

Copied

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

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