7 Replies Latest reply on Mar 9, 2016 3:12 PM by JJMack

    Selection tool and setting the tool's properties using a JavaScript

    igorm27353216

      How to select for example stamp tool and set its Hardness property using Javascript? Scripting Listener not handle tool's property changes

        • 1. Re: Selection tool and setting the tool's properties using a JavaScript
          JJMack Most Valuable Participant

          The only way I know how to select tools an set the tools settings in a script is to create tools presets. Then slelect those tool preset by preset name in a script. This works in Action also.

          • 2. Re: Selection tool and setting the tool's properties using a JavaScript
            igorm27353216 Level 1

            var idsetd = charIDToTypeID( "setd" );
                var desc61 = new ActionDescriptor();
                var idnull = charIDToTypeID( "null" );
                    var ref30 = new ActionReference();
                    var idBrsh = charIDToTypeID( "Brsh" );
                    var idOrdn = charIDToTypeID( "Ordn" );
                    var idTrgt = charIDToTypeID( "Trgt" );
                    ref30.putEnumerated( idBrsh, idOrdn, idTrgt );
                desc61.putReference( idnull, ref30 );
                var idT = charIDToTypeID( "T  " );
                    var desc62 = new ActionDescriptor();
                    var idmasterDiameter = stringIDToTypeID( "masterDiameter" );
                    var idPxl = charIDToTypeID( "#Pxl" );
                    desc62.putUnitDouble( idmasterDiameter, idPxl, 18.000000 );
                var idBrsh = charIDToTypeID( "Brsh" );
                desc61.putObject( idT, idBrsh, desc62 );
            executeAction( idsetd, desc61, DialogModes.NO );
            
            // =======================================================
            var idmodalStateChanged = stringIDToTypeID( "modalStateChanged" );
                var desc63 = new ActionDescriptor();
                var idLvl = charIDToTypeID( "Lvl " );
                desc63.putInteger( idLvl, 1 );
                var idStte = charIDToTypeID( "Stte" );
                var idStte = charIDToTypeID( "Stte" );
                var identer = stringIDToTypeID( "enter" );
                desc63.putEnumerated( idStte, idStte, identer );
            executeAction( idmodalStateChanged, desc63, DialogModes.NO );
            
            // =======================================================
            var idmodalStateChanged = stringIDToTypeID( "modalStateChanged" );
                var desc64 = new ActionDescriptor();
                var idLvl = charIDToTypeID( "Lvl " );
                desc64.putInteger( idLvl, 0 );
                var idStte = charIDToTypeID( "Stte" );
                var idStte = charIDToTypeID( "Stte" );
                var idexit = stringIDToTypeID( "exit" );
                desc64.putEnumerated( idStte, idStte, idexit );
            executeAction( idmodalStateChanged, desc64, DialogModes.NO );
            
            // =======================================================
            var idMk = charIDToTypeID( "Mk  " );
                var desc65 = new ActionDescriptor();
                var idnull = charIDToTypeID( "null" );
                    var ref31 = new ActionReference();
                    var idBrsh = charIDToTypeID( "Brsh" );
                    ref31.putClass( idBrsh );
                desc65.putReference( idnull, ref31 );
                var idNm = charIDToTypeID( "Nm  " );
                desc65.putString( idNm, """test_brush""" );
                var idUsng = charIDToTypeID( "Usng" );
                    var ref32 = new ActionReference();
                    var idPrpr = charIDToTypeID( "Prpr" );
                    var idCrnT = charIDToTypeID( "CrnT" );
                    ref32.putProperty( idPrpr, idCrnT );
                    var idcapp = charIDToTypeID( "capp" );
                    var idOrdn = charIDToTypeID( "Ordn" );
                    var idTrgt = charIDToTypeID( "Trgt" );
                    ref32.putEnumerated( idcapp, idOrdn, idTrgt );
                desc65.putReference( idUsng, ref32 );
            executeAction( idMk, desc65, DialogModes.NO );
            

            • 3. Re: Selection tool and setting the tool's properties using a JavaScript
              JJMack Most Valuable Participant

              As I wrote the only way automate selecting  a tool and set the tools settings  is to create tools presets then select those tools presets in your actions and scripts by preset name.   You can get the code for selecting a preset by name using the scriptlistener plugin.      You can also load the tool presets  the scriptlistener code. If they are not loaded. In theory anyway. I have not tried to do that.  You would create all the tools presets you would like to be able to select.  You would then save out a set of those presets that can  be loaded if they get lost with a reset or are deleted. In you script code you would try to select one of your preset by name.  Should that fail you would use the preset manager to load your set of tool presets then try a second time which should then work.    The tools settings are in the Photoshop Preset.   I just used the preset manager to load one Photoshop set of tool presets.  The I selected a tool preset.   Here is the Scriptlistener code the was generated for my Photoshop's use.

               

               

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

              var idmodalStateChanged = stringIDToTypeID( "modalStateChanged" );

                  var desc254 = new ActionDescriptor();

                  var idLvl = charIDToTypeID( "Lvl " );

                  desc254.putInteger( idLvl, 1 );

                  var idStte = charIDToTypeID( "Stte" );

                  var idStte = charIDToTypeID( "Stte" );

                  var identer = stringIDToTypeID( "enter" );

                  desc254.putEnumerated( idStte, idStte, identer );

              executeAction( idmodalStateChanged, desc254, DialogModes.NO );

               

               

              // ========Preset Manager loading Tools Presets=========================

              var idsetd = charIDToTypeID( "setd" );

                  var desc255 = new ActionDescriptor();

                  var idnull = charIDToTypeID( "null" );

                      var ref80 = new ActionReference();

                      var idPrpr = charIDToTypeID( "Prpr" );

                      var idtoolPreset = stringIDToTypeID( "toolPreset" );

                      ref80.putProperty( idPrpr, idtoolPreset );

                      var idcapp = charIDToTypeID( "capp" );

                      var idOrdn = charIDToTypeID( "Ordn" );

                      var idTrgt = charIDToTypeID( "Trgt" );

                      ref80.putEnumerated( idcapp, idOrdn, idTrgt );

                  desc255.putReference( idnull, ref80 );

                  var idT = charIDToTypeID( "T   " );

                  desc255.putPath( idT, new File( "C:\\Program Files\\Adobe\\Adobe Photoshop CC 2015\\Presets\\Tools\\Airbrushes.tpl" ) );

                  var idAppe = charIDToTypeID( "Appe" );

                  desc255.putBoolean( idAppe, true );

              executeAction( idsetd, desc255, DialogModes.NO );

               

               

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

              var idmodalStateChanged = stringIDToTypeID( "modalStateChanged" );

                  var desc256 = new ActionDescriptor();

                  var idLvl = charIDToTypeID( "Lvl " );

                  desc256.putInteger( idLvl, 0 );

                  var idStte = charIDToTypeID( "Stte" );

                  var idStte = charIDToTypeID( "Stte" );

                  var idexit = stringIDToTypeID( "exit" );

                  desc256.putEnumerated( idStte, idStte, idexit );

              executeAction( idmodalStateChanged, desc256, DialogModes.NO );

               

               

              // =========Selecting a tool preset ==============================================

              var idslct = charIDToTypeID( "slct" );

                  var desc257 = new ActionDescriptor();

                  var idnull = charIDToTypeID( "null" );

                      var ref81 = new ActionReference();

                      var idtoolPreset = stringIDToTypeID( "toolPreset" );

                      ref81.putName( idtoolPreset, "Airbrush Noisy" );

                  desc257.putReference( idnull, ref81 );

              executeAction( idslct, desc257, DialogModes.NO );

              • 4. Re: Selection tool and setting the tool's properties using a JavaScript
                JJMack Most Valuable Participant

                Here I set a new brush preset. I do not know where to settings are in the script listener code.  I set a red brush and made the setting 10 20 30 40 50 so I would have an easy time seeing them but I could not the in the code setting the preset but the preset works. I include a screen capture before and after clicking on the preset to see how it works.

                First the code

                 

                // ================Select=======================================

                var idslct = charIDToTypeID( "slct" );

                    var desc287 = new ActionDescriptor();

                    var idnull = charIDToTypeID( "null" );

                        var ref100 = new ActionReference();

                        var idPbTl = charIDToTypeID( "PbTl" );

                        ref100.putClass( idPbTl );

                    desc287.putReference( idnull, ref100 );

                executeAction( idslct, desc287, DialogModes.NO );

                 

                 

                // ====================Red===================================

                var idsetd = charIDToTypeID( "setd" );

                    var desc288 = new ActionDescriptor();

                    var idnull = charIDToTypeID( "null" );

                        var ref101 = new ActionReference();

                        var idClr = charIDToTypeID( "Clr " );

                        var idFrgC = charIDToTypeID( "FrgC" );

                        ref101.putProperty( idClr, idFrgC );

                    desc288.putReference( idnull, ref101 );

                    var idT = charIDToTypeID( "T   " );

                        var desc289 = new ActionDescriptor();

                        var idRd = charIDToTypeID( "Rd  " );

                        desc289.putDouble( idRd, 255.000000 );

                        var idGrn = charIDToTypeID( "Grn " );

                        desc289.putDouble( idGrn, 0.000000 );

                        var idBl = charIDToTypeID( "Bl  " );

                        desc289.putDouble( idBl, 0.000000 );

                    var idRGBC = charIDToTypeID( "RGBC" );

                    desc288.putObject( idT, idRGBC, desc289 );

                    var idSrce = charIDToTypeID( "Srce" );

                    desc288.putString( idSrce, """swatchesReplace""" );

                executeAction( idsetd, desc288, DialogModes.NO );

                 

                 

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

                var idmodalStateChanged = stringIDToTypeID( "modalStateChanged" );

                    var desc290 = new ActionDescriptor();

                    var idLvl = charIDToTypeID( "Lvl " );

                    desc290.putInteger( idLvl, 1 );

                    var idStte = charIDToTypeID( "Stte" );

                    var idStte = charIDToTypeID( "Stte" );

                    var identer = stringIDToTypeID( "enter" );

                    desc290.putEnumerated( idStte, idStte, identer );

                executeAction( idmodalStateChanged, desc290, DialogModes.NO );

                 

                 

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

                var idmodalStateChanged = stringIDToTypeID( "modalStateChanged" );

                    var desc291 = new ActionDescriptor();

                    var idLvl = charIDToTypeID( "Lvl " );

                    desc291.putInteger( idLvl, 0 );

                    var idStte = charIDToTypeID( "Stte" );

                    var idStte = charIDToTypeID( "Stte" );

                    var idexit = stringIDToTypeID( "exit" );

                    desc291.putEnumerated( idStte, idStte, idexit );

                executeAction( idmodalStateChanged, desc291, DialogModes.NO );

                 

                 

                // =====new preset=====I let Photoshop default the name as you can see it names it soft =============

                var idMk = charIDToTypeID( "Mk  " );

                    var desc292 = new ActionDescriptor();

                    var idnull = charIDToTypeID( "null" );

                        var ref102 = new ActionReference();

                        var idtoolPreset = stringIDToTypeID( "toolPreset" );

                        ref102.putClass( idtoolPreset );

                    desc292.putReference( idnull, ref102 );

                    var idUsng = charIDToTypeID( "Usng" );

                        var ref103 = new ActionReference();

                        var idPrpr = charIDToTypeID( "Prpr" );

                        var idCrnT = charIDToTypeID( "CrnT" );

                        ref103.putProperty( idPrpr, idCrnT );

                        var idcapp = charIDToTypeID( "capp" );

                        var idOrdn = charIDToTypeID( "Ordn" );

                        var idTrgt = charIDToTypeID( "Trgt" );

                        ref103.putEnumerated( idcapp, idOrdn, idTrgt );

                    desc292.putReference( idUsng, ref103 );

                    var idNm = charIDToTypeID( "Nm  " );

                    desc292.putString( idNm, """Brush Tool Soft Elliptical 10 1""" );

                executeAction( idMk, desc292, DialogModes.NO );

                 

                Not the screen captures. First set a hard small brush with no dynamics checked

                Capture.jpg

                • 5. Re: Selection tool and setting the tool's properties using a JavaScript
                  igorm27353216 Level 1

                  JJMack, your code does not work. It only sets brush color to red, and nothing else happens - the brush size and brush shape are still the same.

                  • 6. Re: Selection tool and setting the tool's properties using a JavaScript
                    JJMack Most Valuable Participant

                    As I wrote the code does not set the values the values are recorded into the preset for the current tool setting that I set 10 20 30 40 50,  I showed you that the setting values are not in the script listener code. I also showed you that the preset worked the when I select it, That the values were set into the brush tool.  I also showed ou that you can load tools presets from *.tpl files, using script listener code.  That you can save sets of tools presets using the preset manager to save *.tpl tool preset files.  The settings are in the presets, not in the Code.

                     

                    I just saved a preset file for the brush preset I created on my desktop.  I named the tool preset file BrushToolSoftElliptical.  That preset file is 19KB in size it is not human readable but I opened it into notepad++. there is some ascii text you can read in it.  The editor formats the file as 5 very long(wide) records.  I think you can see there is Brush related information in there.

                     

                     

                    So if you create the presets when an Action or Script select the preset by name  the Tool is selected becomes the current tool and the tools setting are set how the were stored in the preset that you created for the tool.   I theorized that  if you try to select a preset by name if that throws an error you can catch the error a load a set of presets that saved out after creating the presets.

                     

                    As I wrote the only way I know to automate  selecting a tool and setting its options is by selecting a Tool preset.  It is all in the preset selected.  Tool and Setting is in the preset.

                    Capture.jpg

                    • 7. Re: Selection tool and setting the tool's properties using a JavaScript
                      JJMack Most Valuable Participant

                      My theory seems to check out as correct. But this script only works if "current tool only" is not checked in Photoshop's  "Tool Options Bar" Presets pull-down list dialog on the bottom left or, the correct tool is currently selected.

                       

                      var ToolPresetName = "JJMack soft oval red brush" ; // The Photoshop Tool preset name that you created and saved into a set
                      var ToolPresetSet  = "JJMackToolsPresetsSet";       // The SetName.tpl file need to be same folder as this Photoshop script.
                      
                      try {SelectToolPreset(ToolPresetName);}
                      catch(e) {
                        if (LoadToolPresetSet(ToolPresetSet)) {
                        try {SelectToolPreset(ToolPresetName);}
                        catch(e) {alert("Was unable to Select the Tools Preset " + ToolPresetName);}
                        }
                        else {alert("Was unable to load Tools Presets Set " + ToolPresetSet);}
                      }
                      
                      // =================================================== Helper functions ===================================================== //
                      function SelectToolPreset(PresetName) {
                        // =========Selecting a tool preset ==============================================
                        var desc = new ActionDescriptor();
                        var ref = new ActionReference();
                        ref.putName( stringIDToTypeID( "toolPreset" ), PresetName );
                        desc.putReference( charIDToTypeID( "null" ), ref );
                        executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
                      }
                      
                      
                      function LoadToolPresetSet(SetName) {
                        returncode = true;
                        var scriptLocation = String(findScript());
                        var path = scriptLocation.substr(0, scriptLocation.lastIndexOf("/") + 1 ) ;
                        var SetFile = new File(path + SetName + ".tpl");   // Requited to be in the script's folder
                        if (!SetFile.exists) { returncode = false; }
                        else {
                        try {LoadToolsSet(SetFile);}
                        catch(e) { returncode = false; }
                        }
                        return returncode ;
                      }
                      
                      function LoadToolsSet(tplFile) {
                        // ========load a set of Tools Presets=========================
                        var desc = new ActionDescriptor();
                        var ref = new ActionReference();
                        ref.putProperty( charIDToTypeID( "Prpr" ), stringIDToTypeID( "toolPreset"  ));
                        ref.putEnumerated( charIDToTypeID( "capp" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt"  ) );
                        desc.putReference( charIDToTypeID( "null" ), ref );
                        desc.putPath( charIDToTypeID( "T   " ), new File( tplFile ) );
                        desc.putBoolean( charIDToTypeID( "Appe" ), true );
                        executeAction( charIDToTypeID( "setd" ), desc, DialogModes.NO );
                      }
                      
                      // Find the location where this script resides
                      function findScript() {
                        var where = "";
                        try { FORCEERROR = FORCERRROR;}
                        catch(err) { where = File(err.fileName);}
                        return where ;
                      }