Quantcast
Channel: Adobe Community: Message List - Photoshop Scripting
Viewing all articles
Browse latest Browse all 27456

Re: Getting selected layers by script (object refs, not indices!)

$
0
0

This uses the other file’s name ("aaa" in this case), using an index for that, too, may be preferable to avoid problems (in case more than one open file has the same name for example), but maybe it helps.

// thanks to mike hale and paul mr;

// 2013, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

var myDocument = app.activeDocument;

myDocument.suspendHistory("operation", "main(myDocument)");

};

////////////////////////////////////

function main () {

var theLayers = getSelectedLayersIdx();

// do stuff;

// reselect layers;

for (var p = 0; p < theLayers.length; p++) {

  duplicateLayer (theLayers[p], "aaa");

  };

};

// by mike hale, via paul riggott;

// http://forums.adobe.com/message/1944754#1944754

function selectLayerByIndex(index,add){

add = undefined ? add = false:add

var ref = new ActionReference();

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

    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){

alert(e.message);

}

};

////// by paul mr;

function getSelectedLayersIdx(){

      var selectedLayers = new Array;

      var ref = new ActionReference();

      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++){

            try{

               activeDocument.backgroundLayer;

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

            }catch(e){

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

            }

          }

       }else{

         var ref = new ActionReference();

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

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

         try{

            activeDocument.backgroundLayer;

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

         }catch(e){

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

         }

      }

      return selectedLayers;

};

function duplicateLayer (theIndex, theDoc) {

// =======================================================

var idDplc = charIDToTypeID( "Dplc" );

    var desc7 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref3 = new ActionReference();

    ref3.putIndex(charIDToTypeID("Lyr "), theIndex);

    desc7.putReference( idnull, ref3 );

    var idT = charIDToTypeID( "T   " );

        var ref4 = new ActionReference();

        var idDcmn = charIDToTypeID( "Dcmn" );

        ref4.putName( idDcmn, theDoc );

    desc7.putReference( idT, ref4 );

    var idVrsn = charIDToTypeID( "Vrsn" );

    desc7.putInteger( idVrsn, 5 );

executeAction( idDplc, desc7, DialogModes.NO );

};


Viewing all articles
Browse latest Browse all 27456

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>