Here is a script that switches (toggles) Sample Mode option for tools that support it
- September 23, 2021
- 4 replies
- 2663 views
Finally i found out how to set tool options, so here is a script that switches tools Sample Mode.
And finally we can assign a shortcut to that.
For some tools it toggles "Sample All Layers" on/off,
for others it switches Sample Mode between Current / Current & Below / All Layers / etc.
In the start of the script there is a variable called 'includeAllOptions' that controls whether to cycle all options (set to true) or only Current / Current & Below (set to false).
The script will appear in the top of the File / Scripts menu.
It was kinda hard to figure some stuff out, so i decided not to encrypt it's contents, so that anyone could investigate it.
Happy switching ☺ And if you have any comments, questions or suggestions, i'll be happy to hear them.
The script:
/*
<javascriptresource>
<name>Switch Sample Mode</name>
<enableinfo>true</enableinfo>
<category>Hirlin Scripts</category>
</javascriptresource>
// Oleksii Hirlin 2021
*/
var includeAllOptions = true; // should be either true or false
// if set to false will switch between 'Current' and 'Current &below' or just switch on / off
// if seet to true will switch between all avaliable variants as well as on / off
// getting currentToolOptions property
function checkCTOoption(charString, isIdTypeString){
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID("property"), stringIDToTypeID("currentToolOptions"));
ref.putEnumerated(stringIDToTypeID("application"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
if (charString=="eyeDropperSampleSheet"){
return executeActionGet(ref).getObjectValue(stringIDToTypeID("currentToolOptions")).getInteger(stringIDToTypeID(charString));
}
if (isIdTypeString==true){
return executeActionGet(ref).getObjectValue(stringIDToTypeID("currentToolOptions")).getBoolean(stringIDToTypeID(charString));
} else {
return executeActionGet(ref).getObjectValue(stringIDToTypeID("currentToolOptions")).getBoolean(charIDToTypeID(charString));
}
}
// get current tool options object
function getCTO(){
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID("property"), stringIDToTypeID("currentToolOptions"));
ref.putEnumerated(stringIDToTypeID("application"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
return executeActionGet(ref).getObjectValue(stringIDToTypeID("currentToolOptions"));
}
// setting currentToolOptions with options array
function setCTO( options, isIdTypeString ){
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putClass( stringIDToTypeID( app.currentTool ) );
desc.putReference( stringIDToTypeID( "target" ), ref );
// getting current tool options so that they do not get reset
var ctoObj = getCTO();
// check if we are setting the eyedropper tool
if ( app.currentTool=="eyedropperTool" ){
ctoObj.putInteger( stringIDToTypeID( options[0][0] ), options[0][1] );
} else {
// iteration through options array and putting booleans into cto descriptor
for (var i=0; i < options.length; i++){
if (isIdTypeString==true){
ctoObj.putBoolean( stringIDToTypeID( options[i][0] ), options[i][1] );
} else {
ctoObj.putBoolean( charIDToTypeID( options[i][0] ), options[i][1] );
}
}
}
desc.putObject( stringIDToTypeID( "to" ), stringIDToTypeID( "currentToolOptions" ), ctoObj );
executeAction( stringIDToTypeID( "set" ), desc, DialogModes.NO );
}
// switching currentToolOptions if it's one of the tools, ignoring otherwise
function switchCTO (){
if ( app.currentTool=="magicStampTool" || app.currentTool=="cloneStampTool" ){
if ( checkCTOoption("StmA")==true && checkCTOoption("StmB")==false && checkCTOoption("StmS")==false ){
// if 'Current' set to 'Current and below'
setCTO( [ ["StmA",true], ["StmB",true], ["StmS",true] ] );
}
else if ( checkCTOoption("StmA")==true && checkCTOoption("StmB")==true && checkCTOoption("StmS")==true && includeAllOptions==true ){
// if 'current & below' set to 'all layers'
setCTO( [ ["StmA",true], ["StmB",false], ["StmS",true] ] );
}
else {
// otherwise set it to 'Current'
setCTO( [ ["StmA",true], ["StmB",false], ["StmS",false] ] );
}
}
else if ( app.currentTool=="magicLassoTool" || app.currentTool=="wetBrushTool" || app.currentTool=="recomposeSelection" ){
if ( checkCTOoption("sampleAllLayers", true)==false ){
setCTO( [ ["sampleAllLayers", true] ], true );
} else {
setCTO( [ ["sampleAllLayers", false] ], true );
}
}
else if ( app.currentTool=="quickSelectTool" ){
if ( checkCTOoption("quickSelectSampleAllLayers", true)==false ){
setCTO( [ ["quickSelectSampleAllLayers", true] ], true );
} else {
setCTO( [ ["quickSelectSampleAllLayers", false] ], true );
}
}
else if ( app.currentTool=="magicWandTool" ){
if ( checkCTOoption("windowsSystem", true)==false ){
setCTO( [ ["windowsSystem", true] ], true );
} else {
setCTO( [ ["windowsSystem", false] ], true );
}
}
else if ( app.currentTool=="spotHealingBrushTool" ){
if (checkCTOoption("StmS")==true){
setCTO( [ ["StmS",false] ] );
} else {
setCTO( [ ["StmS",true] ] );
}
}
else if ( app.currentTool=="patchSelection" && checkCTOoption("contentAware", true)==true ){
if ( checkCTOoption("sampleAllLayers", true)==false ){
setCTO( [ ["sampleAllLayers", true] ], true );
} else {
setCTO( [ ["sampleAllLayers", false] ], true );
}
}
else if ( app.currentTool=="eyedropperTool" ){
/*
current layer = 1
cur & below = 3
all layers = 0
all layers no adj = 6
cur & below no adj = 8
*/
if ( checkCTOoption("eyeDropperSampleSheet")==1 ) {
setCTO( [ ["eyeDropperSampleSheet", 3] ] );
} else if ( checkCTOoption("eyeDropperSampleSheet")==3 ) {
if ( includeAllOptions==true ){
setCTO( [ ["eyeDropperSampleSheet", 0] ] );
} else {
setCTO( [ ["eyeDropperSampleSheet", 1] ] );
}
} else if ( checkCTOoption("eyeDropperSampleSheet")==0 ) {
if ( includeAllOptions==true ){
setCTO( [ ["eyeDropperSampleSheet", 6] ] );
} else {
setCTO( [ ["eyeDropperSampleSheet", 1] ] );
}
} else if ( checkCTOoption("eyeDropperSampleSheet")==6 ) {
if ( includeAllOptions==true ){
setCTO( [ ["eyeDropperSampleSheet", 8] ] );
} else {
setCTO( [ ["eyeDropperSampleSheet", 1] ] );
}
} else if ( checkCTOoption("eyeDropperSampleSheet")==8 ) {
setCTO( [ ["eyeDropperSampleSheet", 1] ] );
} else {
setCTO( [ ["eyeDropperSampleSheet", 1] ] );
}
}
else if ( app.currentTool=="bucketTool" || app.currentTool=="magicEraserTool"){
if (checkCTOoption("BckS")==true){
setCTO( [ ["BckS",false] ] );
} else {
setCTO( [ ["BckS",true] ] );
}
}
else if ( app.currentTool=="blurTool" || app.currentTool=="sharpenTool" ){
if (checkCTOoption("BlrS")==true){
setCTO( [ ["BlrS",false] ] );
} else {
setCTO( [ ["BlrS",true] ] );
}
}
else if ( app.currentTool=="smudgeTool" ){
if ( checkCTOoption("smudgeStick", true)==false ){
setCTO( [ ["smudgeStick", true] ], true );
} else {
setCTO( [ ["smudgeStick", false] ], true );
}
}
else {
// if other tools, do nothing
}
}
switchCTO();
