Skip to main content
Participant
March 5, 2016
Question

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

  • March 5, 2016
  • 2 replies
  • 1127 views

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

This topic has been closed for replies.

2 replies

JJMack
Community Expert
Community Expert
March 9, 2016

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 ;

}

JJMack
JJMack
Community Expert
Community Expert
March 5, 2016

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.

JJMack
Participant
March 6, 2016

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 );

JJMack
Community Expert
Community Expert
March 6, 2016

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 );

JJMack