15 Replies Latest reply on Mar 14, 2013 11:34 AM by Michael L Hale

    Help with opening levels dialog and saving entered settings.

    Mark Ashton Level 1

      Hey,

       

      I am trying to work out how to save the applied midpoint, whitepoint ect to a variable after the levels dialog has been called from javascript?

       

      I am writing a script that will allow you to set the levels on one image and then I need to repeatedly apply those settings on a batch of specified images.

       

      Is this possible? I figured out how to open and apply the levels but not save the entered settings.

       

      Any help would be much appreciated!

       

      I am using photoshop cs6 on a mac.

       

      Thanks,

       

      Mark

        • 1. Re: Help with opening levels dialog and saving entered settings.
          Paul Riggott Level 6

          I would think you need access to Photoshop CS3 to get all your details as  later versions do not record the details you want.

           

          As an example here a couple of functions that may help...

           

           

          var details = newLevels();
          //example of creating a new level layer with extracted info
          newLevelsWithInts(details[0],details[1]);
          /*****************************
          This function gets the following values
          Adjust Shadow Input Level
          Adjust Highlight Input Level
          *****************************/
          function newLevels() {
          var desc2 = new ActionDescriptor();
          var ref1 = new ActionReference();
          ref1.putClass( charIDToTypeID('AdjL') );
          desc2.putReference( charIDToTypeID('null'), ref1 );
          var desc3 = new ActionDescriptor();
          var desc4 = new ActionDescriptor();
          desc4.putEnumerated( stringIDToTypeID('presetKind'), stringIDToTypeID('presetKindType'), stringIDToTypeID('presetKindDefault') );
          desc3.putObject( charIDToTypeID('Type'), charIDToTypeID('Lvls'), desc4 );
          desc2.putObject( charIDToTypeID('Usng'), charIDToTypeID('AdjL'), desc3 );
          var d = executeAction( charIDToTypeID('Mk  '), desc2, DialogModes.ALL );
          d = d.getObjectValue(stringIDToTypeID('using')).getObjectValue(stringIDToTypeID('type')).getList (stringIDToTypeID('adjustment')).getObjectValue(0).getList (stringIDToTypeID('input'));
          var levelsInput = new Array();
          for(var a =0;a< d.count;a++){levelsInput.push( d.getInteger(a));}
          return levelsInput;
          };
          /*****************************
          This function put the following values
          Adjust Shadow Input Level
          Adjust Highlight Input Level
          and creates a new levels layer
          *****************************/
          function newLevelsWithInts(int1,int2) {
          var desc2 = new ActionDescriptor();
          var ref1 = new ActionReference();
          ref1.putClass( charIDToTypeID('AdjL') );
          desc2.putReference( charIDToTypeID('null'), ref1 );
          var desc3 = new ActionDescriptor();
          var desc4 = new ActionDescriptor();
          var list1 = new ActionList();
          var desc5 = new ActionDescriptor();
          var ref2 = new ActionReference();
          ref2.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), charIDToTypeID('Cmps') );
          desc5.putReference( charIDToTypeID('Chnl'), ref2 );
          var list2 = new ActionList();
          list2.putInteger( Number(int1) );
          list2.putInteger( Number(int2) );
          desc5.putList( charIDToTypeID('Inpt'), list2 );
          list1.putObject( charIDToTypeID('LvlA'), desc5 );
          desc4.putList( charIDToTypeID('Adjs'), list1 );
          desc3.putObject( charIDToTypeID('Type'), charIDToTypeID('Lvls'), desc4 );
          desc2.putObject( charIDToTypeID('Usng'), charIDToTypeID('AdjL'), desc3 );
          executeAction( charIDToTypeID('Mk  '), desc2, DialogModes.NO );
          };
          
          
          1 person found this helpful
          • 2. Re: Help with opening levels dialog and saving entered settings.
            Mark Ashton Level 1

            Ah thats annoying! Thanks for your help though, I could definately use this script to work with what I had planned. Is there a way to get it to set the midpoint too as it only seems to set the white and black point?

             

            Thanks,

             

            Mark

            • 3. Re: Help with opening levels dialog and saving entered settings.
              Paul Riggott Level 6

              Sorry no, it seems that those are the only values that are available.

              1 person found this helpful
              • 4. Re: Help with opening levels dialog and saving entered settings.
                Mark Ashton Level 1

                Ah well, thank you so much for your help, I think I will be able to achieve what I want by duplicating the levels layer to the images that require it.

                 

                Thanks again!

                • 5. Re: Help with opening levels dialog and saving entered settings.
                  Michael L Hale Level 5

                  Sorry Paul, but I think you are wrong. The 'adjustment' list should have descriptors for each channel that was set by the user. Those descriptors should have key for the channel as a referencetype, input b/w points as a list, output b/w points as a list, and gamma as a double. I would think that if those are not in the descriptor it's because the user didn't change the default values.

                   

                  But if all you want to do is reapply the same settings later you don't really need to know what those setting were. You just store the descriptor that contains them for later use.

                   

                  function newLevels() {
                  var desc2 = new ActionDescriptor();
                  var ref1 = new ActionReference();
                  ref1.putClass( charIDToTypeID('AdjL') );
                  desc2.putReference( charIDToTypeID('null'), ref1 );
                  var desc3 = new ActionDescriptor();
                  var desc4 = new ActionDescriptor();
                  desc4.putEnumerated( stringIDToTypeID('presetKind'), stringIDToTypeID('presetKindType'), stringIDToTypeID('presetKindDefault') );
                  desc3.putObject( charIDToTypeID('Type'), charIDToTypeID('Lvls'), desc4 );
                  desc2.putObject( charIDToTypeID('Usng'), charIDToTypeID('AdjL'), desc3 );
                  return executeAction( charIDToTypeID('Mk  '), desc2, DialogModes.ALL );
                  };
                  // store the descriptor holding the users values for later
                  var newLevelsDesc = newLevels();
                  // do something else like open another image
                  // then when needed make a new adjustment layer using those settings
                  executeAction( charIDToTypeID('Mk  '), newLevelsDesc, DialogModes.NO );
                  
                  • 6. Re: Help with opening levels dialog and saving entered settings.
                    Paul Riggott Level 6

                    Thanks for that Mike, but when I explored the descriptor there are only two values even though I did move the center point, as you will see/test with the first function it returns all values. Also the SL output in CS3 only shows two values I.E.

                     

                     

                    function ftn1() {
                    var desc2 = new ActionDescriptor();
                    var ref1 = new ActionReference();
                    ref1.putClass( charIDToTypeID('AdjL') );
                    desc2.putReference( charIDToTypeID('null'), ref1 );
                    var desc3 = new ActionDescriptor();
                    var desc4 = new ActionDescriptor();
                    var list1 = new ActionList();
                    var desc5 = new ActionDescriptor();
                    var ref2 = new ActionReference();
                    ref2.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), charIDToTypeID('Cmps') );
                    desc5.putReference( charIDToTypeID('Chnl'), ref2 );
                    var list2 = new ActionList();
                    list2.putInteger( 14 );
                    list2.putInteger( 241 );
                    desc5.putList( charIDToTypeID('Inpt'), list2 );
                    var list3 = new ActionList();
                    list3.putInteger( 11 );
                    list3.putInteger( 248 );
                    desc5.putList( charIDToTypeID('Otpt'), list3 );
                    list1.putObject( charIDToTypeID('LvlA'), desc5 );
                    desc4.putList( charIDToTypeID('Adjs'), list1 );
                    desc3.putObject( charIDToTypeID('Type'), charIDToTypeID('Lvls'), desc4 );
                    desc2.putObject( charIDToTypeID('Usng'), charIDToTypeID('AdjL'), desc3 );
                    executeAction( charIDToTypeID('Mk  '), desc2, DialogModes.NO );
                    };
                    
                    
                    • 7. Re: Help with opening levels dialog and saving entered settings.
                      Mark Ashton Level 1

                      Thank you Michael this seems to have done the trick!

                       

                      For something so simple to do in photoshop itself it is very complicated in scripting! I guess its always the way!

                       

                      Thanks again.

                      • 8. Re: Help with opening levels dialog and saving entered settings.
                        Michael L Hale Level 5

                        Paul, I think that is because your first function is only searching the input values. This seems to work for me.

                         

                        function newLevels() {
                            try{
                                var desc2 = new ActionDescriptor();
                                var ref1 = new ActionReference();
                                ref1.putClass( charIDToTypeID('AdjL') );
                                desc2.putReference( charIDToTypeID('null'), ref1 );
                                var desc3 = new ActionDescriptor();
                                var desc4 = new ActionDescriptor();
                                desc4.putEnumerated( stringIDToTypeID('presetKind'), stringIDToTypeID('presetKindType'), stringIDToTypeID('presetKindDefault') );
                                desc3.putObject( charIDToTypeID('Type'), charIDToTypeID('Lvls'), desc4 );
                                desc2.putObject( charIDToTypeID('Usng'), charIDToTypeID('AdjL'), desc3 );
                                var desc = executeAction( charIDToTypeID('Mk  '), desc2, DialogModes.ALL );
                                // because this descriptor contains both the new layer settings and the adjustment settings if any, we first get the layer descriptor
                                var layerDesc = desc.getObjectValue(charIDToTypeID('Usng')).getObjectValue(charIDToTypeID('Type'));
                                // now we check to see if the user made any changes to the defalut level adjustments
                                if( layerDesc.hasKey(stringIDToTypeID('adjustment')) ){
                                    var channelList = layerDesc.getList (stringIDToTypeID('adjustment'));
                                    var settings = [];
                                    // for each channel in the list( there may be more than one )
                                    for(var c =0;c<channelList.count;c++){
                                        var channelSettings = {};
                                        // get the descriptor for that channel
                                        var channelDesc = channelList.getObjectValue(c);
                                        // each channelDesc will have at least one key for which channel the setting apply
                                        channelSettings.channel = typeIDToCharID(channelDesc.getReference(charIDToTypeID('Chnl')).getEnumeratedValue());
                                        // now we check to see if there are other user values
                                        channelDesc.hasKey(charIDToTypeID('Gmm ')) ?  channelSettings.gamma = channelDesc.getDouble(charIDToTypeID('Gmm ')):channelSettings.gamma = 1.0;
                                        if(channelDesc.hasKey(charIDToTypeID('Inpt'))){
                                            var inputList = channelDesc.getList(charIDToTypeID('Inpt'));
                                            channelSettings.inputBlack = inputList.getInteger(0);
                                            channelSettings.inputWhite = inputList.getInteger(1);
                                        }else{
                                            channelSettings.inputBlack = 0;
                                            channelSettings.inputWhite = 255;
                                        }
                                        if(channelDesc.hasKey(charIDToTypeID('Otpt'))){
                                            var outputList = channelDesc.getList(charIDToTypeID('Otpt'));
                                            channelSettings.outputBlack = outputList.getInteger(0);
                                            channelSettings.outputWhite = outputList.getInteger(1);
                                        }else{
                                            channelSettings.outputBlack = 0;
                                            channelSettings.outputWhite = 255;
                                        }
                                        settings.push(channelSettings);
                                    }
                                    return settings;
                                }
                            }catch(e){
                                // deal with user clicking cancel here
                            }
                        };
                        var settings = newLevels();
                        alert(settings[0].gamma);
                        
                        • 9. Re: Help with opening levels dialog and saving entered settings.
                          Paul Riggott Level 6

                          Very nice Mike but still no mid point values, I wonder where they are?

                          • 10. Re: Help with opening levels dialog and saving entered settings.
                            Michael L Hale Level 5

                            There are other ways to do this. You could leave the original document open and dupe the adjustment layer to each additional image in the batch. Or if you just want to apply a levels image adjustment without making an adjustment layer the code would be simpler. It's dealing with the current way Photoshop stores settings in an adjustment layer that is adding to the complexity.

                            • 11. Re: Help with opening levels dialog and saving entered settings.
                              Michael L Hale Level 5

                              Paul, are you saying that the last code I posted doesn't alert the gamma for you?

                              • 12. Re: Help with opening levels dialog and saving entered settings.
                                Paul Riggott Level 6

                                No, I am wondering where this value is stored...

                                 

                                Levels.jpg

                                • 13. Re: Help with opening levels dialog and saving entered settings.
                                  Michael L Hale Level 5

                                  That is strange. It is working for me. What OS and Photoshop version are you using?

                                  gammaAlert.jpg

                                  • 14. Re: Help with opening levels dialog and saving entered settings.
                                    Paul Riggott Level 6

                                    I was using CS3 and Windows 7.

                                    I can't see this value being set in your example above only the white and black points.

                                    How did you extract this value and where does it go in a new levels?

                                    • 15. Re: Help with opening levels dialog and saving entered settings.
                                      Michael L Hale Level 5

                                      Maybe that code doesn't work with CS3. I will try check and post an update when I can. But in newer versions that value is stored in the 'Gmm ' key as a double.