Skip to main content
Participant
February 14, 2014
Question

Move a layer/layerset into a layerset using ID only

  • February 14, 2014
  • 1 reply
  • 1656 views

I'm trying to move a layer into a layerset using it's ID (not name or index).

This function below works if I use putName instead of putIdentifier, does anyone have some code that will move a layer into a layerset using only ID?

I keep getting General photoshop error occurred.

     moveLayer : function( fromLayer, toLayer )

     {

                  

        var desc5 = new ActionDescriptor();

        var ref4 = new ActionReference();

        ref4.putIdentifier( charIDToTypeID('Lyr '), Number(fromLayer) );

        desc5.putReference( charIDToTypeID('null'), ref4 );

        var ref5 = new ActionReference();

        ref5.putIndex( charIDToTypeID('Lyr '), this.getLayerIndex(toLayer) );

        desc5.putReference( charIDToTypeID('T   '), ref5 );

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

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

       

        try

        {

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

        }

        catch(e)

        {

            alert(e);

        }

       

    },

    // Get layer index but return 0 if there is an error.

    getLayerIndex: function(id)

    {

        var ref = new ActionReference();

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

       

        try

        {

            activeDocument.backgroundLayer;

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

           

        }

        catch(e)

        {

            try

            {

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

            }

             catch(e)

             {

                 return 0;

             }

        }

    },

This topic has been closed for replies.

1 reply

Pedro Cortez Marques
Legend
February 14, 2014

It is simpler using this?

var thisLayer = activeDocument.activeLayer;

// create new layerset if doesn't exist

if (activeDocument.layerSets.length == 0) activeDocument.layerSets.add();

// return to previous selected layer

activeDocument.activeLayer = thisLayer;

// move layer to the referenced layerSets[0] but the id of this element could be any number

activeDocument.activeLayer.move (activeDocument.layerSets[0], ElementPlacement.PLACEATEND);

So the solution is to create first a script for(var a in activeDocument.layerSets) to reference your chosen layerSet by id and if exists it gives you the index.

oddgamesAuthor
Participant
February 17, 2014

My extension has it's own "layer list" that represents only a hand ful of layers that have meta data that has been set by the extension.

I need to be able to move exisiting layers into existing layer sets using their ID.

Does this line of code place it within the layerset? Or after it?

activeDocument.activeLayer.move (activeDocument.layerSets[0], ElementPlacement.PLACEATEND);

oddgamesAuthor
Participant
February 17, 2014

This seems to work:

     moveInside : function( fromLayerID, toLayerID )

     {

        var desc = new ActionDescriptor();

        var sourceRef = new ActionReference();

        sourceRef.putIdentifier( charIDToTypeID( "Lyr " ), fromLayerID );

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

        var layerIndex = this.getLayerIndex(toLayerID);

        var destinationRef = new ActionReference();

        destinationRef.putIndex( charIDToTypeID( "Lyr " ), layerIndex-1 );

        desc.putReference( charIDToTypeID( "T   " ), destinationRef );

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

        desc.putInteger( charIDToTypeID( "Vrsn" ), 5 );

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

    },

    // Get layer index but return 0 if there is an error.

    getLayerIndex: function(id)

    {

        var ref = new ActionReference();

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

       

        try

        {

            activeDocument.backgroundLayer;

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

           

        }

        catch(e)

        {

            try

            {

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

            }

             catch(e)

             {

                 return 0;

             }

        }

    },