4 Replies Latest reply on Nov 12, 2016 10:26 PM by JavierAroche

    Help With Including Curves Changes In Script

    ian-barber Level 1

      I thought I had mastered this but it turns out I haven't.

       

      The ScriptListner does not appear to record the point plotted in a curves adjustment layer.

       

      Any help appreciated

       

      Ian

        • 1. Re: Help With Including Curves Changes In Script
          JavierAroche Level 3

          ian-barber, it did record the curve changes in the ScriptingListener for me.

           

          I made a function to modify an active (selected) curves adjustment layer. It needs a minimum of 2 input points, and it can have as many points as you want. Each point is an array with an x and y coordinate.

           

          // Examples
          var point1 = [28, 0];
          var point2 = [37, 90];
          var point3 = [163, 155];
          var point4 = [253, 190];
          
          // Add points to array
          var points = [point1, point2, point3, point4];
          
          // Execute
          modifySelectedCurvesLayer(points);
          
          /*
           * @param {Array} points
           * --> forEach : [x, y]
           */
          function modifySelectedCurvesLayer(points) {
              if(points.length < 2) { 
                  alert('Curves need a minimum of 2 points');
                  return false;
              }
              
              var desc = new ActionDescriptor();
              var ref = new ActionReference();
                  ref.putEnumerated( charIDToTypeID( "AdjL" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
                  desc.putReference( charIDToTypeID( "null" ), ref );
              
              var curvesLayerDesc = new ActionDescriptor();
                  curvesLayerDesc.putEnumerated( stringIDToTypeID( "presetKind" ), stringIDToTypeID( "presetKindType" ), stringIDToTypeID( "presetKindCustom" ) );
              
              var chnlList = new ActionList();
              var chnlDesc = new ActionDescriptor();
              var channelRef = new ActionReference();
                  channelRef.putEnumerated( charIDToTypeID( "Chnl" ), charIDToTypeID( "Chnl" ), charIDToTypeID( "Cmps" ) );
                  chnlDesc.putReference( charIDToTypeID( "Chnl" ), channelRef );
              
              var pointsList = new ActionList();
              
              for(var i = 0; i < points.length; i++) {
                  var pointDesc = new ActionDescriptor();
                      pointDesc.putDouble( charIDToTypeID( "Hrzn" ), points[i][0] );
                      pointDesc.putDouble( charIDToTypeID( "Vrtc" ), points[i][1] );
                      pointsList.putObject( charIDToTypeID( "Pnt " ), pointDesc );
              }
              
                  chnlDesc.putList( charIDToTypeID( "Crv " ), pointsList );
                  chnlList.putObject( charIDToTypeID( "CrvA" ), chnlDesc );
              
                  curvesLayerDesc.putList( charIDToTypeID( "Adjs" ), chnlList );
                  desc.putObject( charIDToTypeID( "T   " ), charIDToTypeID( "Crvs" ), curvesLayerDesc );
              executeAction( charIDToTypeID( "setd" ), desc, DialogModes.NO );
          }
          

           

          Hope it helps!

          • 2. Re: Help With Including Curves Changes In Script
            ian-barber Level 1

            JavierAroche, many thanks for the reply and the code. I will now study this to see if i can work it out.

            • 3. Re: Help With Including Curves Changes In Script
              ian-barber Level 1

              The above code appears to work fine if the open document is in RGB but does not work if the open document is in grayscale mode.

               

              Can anyone throw any light on as to why that would be ?

               

              ian

              • 4. Re: Help With Including Curves Changes In Script
                JavierAroche Level 3

                Hey ian-barber, you're right. I missed the fact that different document modes require a different charID. Here's the updated function which will work on any document mode. I'm pretty sure I covered all of them, including all possible monotone / duotone / tritone / quadtone options.

                 

                // Examples  
                var point1 = [28, 0];  
                var point2 = [37, 90];  
                var point3 = [163, 155];  
                var point4 = [253, 190];  
                  
                // Add points to array  
                var points = [point1, point2, point3, point4];  
                  
                // Execute  
                modifySelectedCurvesLayer(points);  
                  
                /* 
                 * @param {Array} points 
                 * --> forEach : [x, y] 
                 */  
                function modifySelectedCurvesLayer(points) {
                    // Validate input
                    if(points.length < 2) {   
                        alert('Curves need a minimum of 2 points');  
                        return false;  
                    }
                    
                    // Analyze document mode
                    switch(app.activeDocument.mode.toString()) {
                        case "DocumentMode.RGB":
                        case "DocumentMode.CMYK":
                            var docModeCharID = "Cmps";
                            break;
                    
                        case "DocumentMode.GRAYSCALE":
                            var docModeCharID = "Blck";
                            break;
                            
                        case "DocumentMode.DUOTONE":
                            var channelName = app.activeDocument.channels[0].name;
                        
                            if( channelName == "Monotone" ) {
                                var docModeCharID = "Mntn";
                            } else if( channelName == "Duotone" ) {
                                var docModeCharID = "Dtn ";
                            } else if( channelName == "Tritone" ) {
                                var docModeCharID = "Trtn";
                            } else if( channelName == "Quadtone" ) {
                                var docModeCharID = "Qdtn";
                            }
                            
                            break;
                            
                        case "DocumentMode.LAB":
                            var docModeCharID = "Lght";
                            break;
                            
                        default: 
                            break;
                    }
                      
                    var desc = new ActionDescriptor();  
                    var ref = new ActionReference();  
                        ref.putEnumerated( charIDToTypeID( "AdjL" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );  
                        desc.putReference( charIDToTypeID( "null" ), ref );  
                      
                    var curvesLayerDesc = new ActionDescriptor();  
                        curvesLayerDesc.putEnumerated( stringIDToTypeID( "presetKind" ), stringIDToTypeID( "presetKindType" ), stringIDToTypeID( "presetKindCustom" ) );  
                      
                    var chnlList = new ActionList();  
                    var chnlDesc = new ActionDescriptor();  
                    var channelRef = new ActionReference();  
                        channelRef.putEnumerated( charIDToTypeID( "Chnl" ), charIDToTypeID( "Chnl" ), charIDToTypeID( docModeCharID ) );  
                        chnlDesc.putReference( charIDToTypeID( "Chnl" ), channelRef );  
                      
                    var pointsList = new ActionList();  
                      
                    for(var i = 0; i < points.length; i++) {  
                        var pointDesc = new ActionDescriptor();  
                            pointDesc.putDouble( charIDToTypeID( "Hrzn" ), points[i][0] );  
                            pointDesc.putDouble( charIDToTypeID( "Vrtc" ), points[i][1] );  
                            pointsList.putObject( charIDToTypeID( "Pnt " ), pointDesc );  
                    }  
                      
                        chnlDesc.putList( charIDToTypeID( "Crv " ), pointsList );  
                        chnlList.putObject( charIDToTypeID( "CrvA" ), chnlDesc );  
                      
                        curvesLayerDesc.putList( charIDToTypeID( "Adjs" ), chnlList );  
                        desc.putObject( charIDToTypeID( "T   " ), charIDToTypeID( "Crvs" ), curvesLayerDesc );  
                    executeAction( charIDToTypeID( "setd" ), desc, DialogModes.NO );  
                }