4 Replies Latest reply on Aug 2, 2011 7:06 PM by Michael L Hale

    How to set layer color property via script?

    jrapczak2

      I'm looking for a way to set a layer's UI color property via script (see attached). Any help would be greatly appreciated!

      LayerColor.jpg

        • 1. Re: How to set layer color property via script?
          xbytor2 Level 4

          This sets the current layer's color property to red...

           

          -X

           

          function setCurrentLayerRed() {
            function cTID(s) { return app.charIDToTypeID(s); };
            function sTID(s) { return app.stringIDToTypeID(s); };
          
              var desc5 = new ActionDescriptor();
                  var ref3 = new ActionReference();
                  ref3.putEnumerated( cTID('Lyr '), cTID('Ordn'), cTID('Trgt') );
              desc5.putReference( cTID('null'), ref3 );
                  var desc6 = new ActionDescriptor();
                  desc6.putEnumerated( cTID('Clr '), cTID('Clr '), cTID('Rd  ') );
              desc5.putObject( cTID('T   '), cTID('Lyr '), desc6 );
              executeAction( cTID('setd'), desc5, DialogModes.NO );
          };
          
          setCurrentLayerRed();
          
          1 person found this helpful
          • 2. Re: How to set layer color property via script?
            jrapczak2 Level 1

            Uhm.. wow. That's crazy complicated!

             

            What are the list of possible colors that can be set this way? I tried 'Bl ' and it used the standard blue, but 'Gr ' is a much darker green than can be selected via the UI...

             

            Also this only appears to work on one layer at a time, and not with multiple selected. Sorry, I can't really decipher what is going on there!

            • 3. Re: How to set layer color property via script?
              Michael L Hale Level 5

              Here are the charID that you can use to set a layer's color.

               

              charIDToTypeID('None')
              charIDToTypeID( "Rd  " )
              charIDToTypeID( "Orng" )
              charIDToTypeID( "Ylw " )
              charIDToTypeID( "Grn " )
              charIDToTypeID( "Bl  " )
              charIDToTypeID( "Vlt " )
              charIDToTypeID( "Gry " )
              
              • 4. Re: How to set layer color property via script?
                Michael L Hale Level 5

                jrapczak2 wrote:

                 

                Uhm.. wow. That's crazy complicated

                It is even more complicated to work with multi-selected layers.

                 

                if( app.documents.length > 0 && versionCheck()  ){
                     app.activeDocument.suspendHistory("Set Layer's Color", 'changeLayersColor()');
                }
                function versionCheck()  { return app.version.match(/1[1|2]./) >= 11; };
                function changeLayersColor(){
                     function setActiveLayerColor( color ) {
                          var desc = new ActionDescriptor();
                               var ref = new ActionReference();
                               ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
                          desc.putReference( charIDToTypeID('null'), ref );
                               var colorEnumDesc = new ActionDescriptor();
                               colorEnumDesc.putEnumerated( charIDToTypeID('Clr '), charIDToTypeID('Clr '), color );
                          desc.putObject( charIDToTypeID('T   '), charIDToTypeID('Lyr '), colorEnumDesc );
                          executeAction( charIDToTypeID('setd'), desc, DialogModes.NO );
                     };
                     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 makeActiveByIndex( idx, visible ){
                          if( idx.constructor != Array ) idx = [ idx ];
                          for( var i = 0; i < idx.length; i++ ){
                               var desc = new ActionDescriptor();
                               var ref = new ActionReference();
                               ref.putIndex(charIDToTypeID( "Lyr " ), idx[i] );
                               desc.putReference( charIDToTypeID( "null" ), ref );
                               if( i > 0 ) {
                                    var idselectionModifier = stringIDToTypeID( "selectionModifier" );
                                    var idselectionModifierType = stringIDToTypeID( "selectionModifierType" );
                                    var idaddToSelection = stringIDToTypeID( "addToSelection" );
                                    desc.putEnumerated( idselectionModifier, idselectionModifierType, idaddToSelection );
                               }
                               desc.putBoolean( charIDToTypeID( "MkVs" ), visible );
                               executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
                          }     
                     };
                
                     var colors = ['None','Red','Orange','Yellow','Green','Blue','Violet','Grey'];
                     var colorIDs = [charIDToTypeID('None'),
                                              charIDToTypeID( "Rd  " ),
                                              charIDToTypeID( "Orng" ),
                                              charIDToTypeID( "Ylw " ),
                                              charIDToTypeID( "Grn " ),
                                              charIDToTypeID( "Bl  " ),
                                              charIDToTypeID( "Vlt " ),
                                              charIDToTypeID( "Gry " )];
                     var dlg = new Window( 'dialog', 'Change Layer Color' );
                     dlg.ddColors= dlg.add("dropdownlist", undefined,  colors);
                     dlg.ddColors.preferredSize.width = 100;
                     dlg.ddColors.items[0].selected = true;
                     dlg.ok = dlg.add('button',undefined,'Ok');
                     dlg.cancel = dlg.add('button',undefined,'Cancel');
                     var results = dlg.show();
                     if( results == 1 ){
                          var selectedLayers =  getSelectedLayersIdx();
                          for( var l=0;l<selectedLayers.length;l++ ){
                               makeActiveByIndex( selectedLayers[l], false );
                               setActiveLayerColor( colorIDs[dlg.ddColors.selection.index] );
                          }
                          makeActiveByIndex( selectedLayers,false );
                     }
                };