Skip to main content
Inspiring
September 28, 2020
Answered

Get Active Tool (Tool Palette)

  • September 28, 2020
  • 2 replies
  • 2092 views

Does Photoshop has a function to get the active tool from the tool palette?

This topic has been closed for replies.
Correct answer jazz-y

Thanks again for your kindness.  My programming skills are rudimentary but I enjoy making photoshop scripts. I was able to make it work by creating another variable for the patchSelection with the selectionMode key. In order to make it work, I had to repeat the code block for the patchSelection tool. How does the code look?

 

#target photoshop;
 
//alert(app.currentTool)
var s2t = stringIDToTypeID,
t2s = typeIDToStringID;

(r = new ActionReference()).putProperty(s2t('property'), p = s2t('selection'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));

if (executeActionGet(r).hasKey(p)) {
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('tool'));
r.putEnumerated(s2t('application'), s2t('ordinal'), s2t('targetEnum'));
var t = t2s(executeActionGet(r).getEnumerationType(s2t('tool')));
var u = t2s(executeActionGet(r).getEnumerationType(s2t('tool')));


if (t == 'marqueeRectTool' ||
t == 'marqueeEllipTool' ||
t == 'lassoTool' ||
t == 'polySelTool' ||
t == 'magicLassoTool' ||
t == 'magneticLassoTool' ||
//u == 'patchSelection' ||
t == 'magicWandTool') {
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('tool'));
r.putEnumerated(s2t('application'), s2t('ordinal'), s2t('targetEnum'));
d = executeActionGet(r).getObjectValue(s2t('currentToolOptions'))
 
if (d.getInteger(s2t('selectionEnum')) == 1) {
(d = new ActionDescriptor()).putInteger(s2t('selectionEnum'), 2); // 0 - normal, 1 - add, 2 - subtract, 3 - intersect
(r = new ActionReference()).putClass(s2t(t));
(d1 = new ActionDescriptor()).putReference(s2t('target'), r);
d1.putObject(s2t('to'), s2t('target'), d);
executeAction(s2t('set'), d1, DialogModes.NO)
}
else if (d.getInteger(s2t('selectionEnum')) == 2) {
(d = new ActionDescriptor()).putInteger(s2t('selectionEnum'), 1); // 0 - normal, 1 - add, 2 - subtract, 3 - intersect
(r = new ActionReference()).putClass(s2t(t));
(d1 = new ActionDescriptor()).putReference(s2t('target'), r);
d1.putObject(s2t('to'), s2t('target'), d);
executeAction(s2t('set'), d1, DialogModes.NO)
}

}

// patch tool snippet
if (u == 'patchSelection') {
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('tool'));
r.putEnumerated(s2t('application'), s2t('ordinal'), s2t('targetEnum'));
d = executeActionGet(r).getObjectValue(s2t('currentToolOptions'))
 
if (d.getInteger(s2t('selectionMode')) == 1) {
(d = new ActionDescriptor()).putInteger(s2t('selectionMode'), 2); // 0 - normal, 1 - add, 2 - subtract, 3 - intersect
(r = new ActionReference()).putClass(s2t(u));
(d1 = new ActionDescriptor()).putReference(s2t('target'), r);
d1.putObject(s2t('to'), s2t('target'), d);
executeAction(s2t('set'), d1, DialogModes.NO)
}
else if (d.getInteger(s2t('selectionMode')) == 2) {
(d = new ActionDescriptor()).putInteger(s2t('selectionMode'), 1); // 0 - normal, 1 - add, 2 - subtract, 3 - intersect
(r = new ActionReference()).putClass(s2t(u));
(d1 = new ActionDescriptor()).putReference(s2t('target'), r);
d1.putObject(s2t('to'), s2t('target'), d);
executeAction(s2t('set'), d1, DialogModes.NO)
}
}

}

#target photoshop;
var s2t = stringIDToTypeID,
    t2s = typeIDToStringID;

(r = new ActionReference()).putProperty(s2t('property'), p = s2t('selection'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));

if (executeActionGet(r).hasKey(p)) {
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('tool'));
    r.putEnumerated(s2t('application'), s2t('ordinal'), s2t('targetEnum'));
    var t = t2s(executeActionGet(r).getEnumerationType(s2t('tool'))),
        selMode = s2t(t == 'patchSelection' ? 'selectionMode' : 'selectionEnum');
    if (t == 'marqueeRectTool' ||
        t == 'marqueeEllipTool' ||
        t == 'lassoTool' ||
        t == 'polySelTool' ||
        t == 'magicLassoTool' ||
        t == 'magneticLassoTool' ||
        t == 'patchSelection' ||
        t == 'magicWandTool') {
        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('tool'));
        r.putEnumerated(s2t('application'), s2t('ordinal'), s2t('targetEnum'));
        d = executeActionGet(r).getObjectValue(s2t('currentToolOptions'))
        if (d.getInteger(selMode) == 1) {
            (d = new ActionDescriptor()).putInteger(selMode, 2);
            (r = new ActionReference()).putClass(s2t(t));
            (d1 = new ActionDescriptor()).putReference(s2t('target'), r);
            d1.putObject(s2t('to'), s2t('target'), d);
            executeAction(s2t('set'), d1, DialogModes.NO);
        }
    }
}
#target photoshop;

var s2t = stringIDToTypeID,
    t2s = typeIDToStringID,
    tools = {
        marqueeRectTool: 'selectionEnum',
        marqueeEllipTool: 'selectionEnum',
        lassoTool: 'selectionEnum',
        polySelTool: 'selectionEnum',
        magicLassoTool: 'selectionEnum',
        magneticLassoTool: 'selectionEnum',
        patchSelection: 'selectionMode',
        magicWandTool: 'selectionEnum',
    };

(r = new ActionReference()).putProperty(s2t('property'), p = s2t('selection'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));

if (executeActionGet(r).hasKey(p)) {
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('tool'));
    r.putEnumerated(s2t('application'), s2t('ordinal'), s2t('targetEnum'));
    var t = t2s(executeActionGet(r).getEnumerationType(s2t('tool')));
    if (tools[t]) {
        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('tool'));
        r.putEnumerated(s2t('application'), s2t('ordinal'), s2t('targetEnum'));
        d = executeActionGet(r).getObjectValue(s2t('currentToolOptions'))
        if (d.getInteger(s2t(tools[t])) == 1) {
            (d = new ActionDescriptor()).putInteger(s2t(tools[t]), 2);
            (r = new ActionReference()).putClass(s2t(t));
            (d1 = new ActionDescriptor()).putReference(s2t('target'), r);
            d1.putObject(s2t('to'), s2t('target'), d);
            executeAction(s2t('set'), d1, DialogModes.NO);
        }
    }
}

or

 

2 replies

Legend
September 29, 2020

alert(app.currentTool);

 

Inspiring
September 29, 2020

Thank you bth solutuons work.

Is the Lasso selection module scriptable in Photoshop 2020? 

For example, if the active document has a selection and the Add to Selection option is active in the module, can a script switch to the Remove from Selection option?

 

Alternatively, this can be done via modifier keys in the keyboard.

Is there a way to toggle a keyboard modifier key via Photoshop script?

 

Lasso Selection Module 

Legend
September 29, 2020
#target photoshop;
var s2t = stringIDToTypeID,
    t2s = typeIDToStringID;

(r = new ActionReference()).putProperty(s2t('property'), p = s2t('selection'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));

if (executeActionGet(r).hasKey(p)) {
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('tool'));
    r.putEnumerated(s2t('application'), s2t('ordinal'), s2t('targetEnum'));
    if (t2s(executeActionGet(r).getEnumerationType(s2t('tool'))) == 'lassoTool') {

        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('tool'));
        r.putEnumerated(s2t('application'), s2t('ordinal'), s2t('targetEnum'));
        d = executeActionGet(r).getObjectValue(s2t('currentToolOptions'))

        d.putInteger(s2t('selectionEnum'), 2); // 0 - normal, 1 - add, 2 - subtract, 3 - intersect

        (r = new ActionReference()).putClass(s2t('lassoTool'));
        (d1 = new ActionDescriptor()).putReference(s2t('target'), r);
        d1.putObject(s2t('to'), s2t('target'), d);

        executeAction(s2t('set'), d1, DialogModes.NO)


    }
}
JJMack
Community Expert
Community Expert
September 29, 2020

A photoshop script can

function currentToolName() {
	var ref = new ActionReference();
	ref.putProperty(stringIDToTypeID("property"), stringIDToTypeID("tool"));
	ref.putEnumerated( charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
	var applicationDesc = executeActionGet(ref);
	//alert(typeIDToStringID(applicationDesc.getEnumerationType(stringIDToTypeID("tool"))));
	return(typeIDToStringID(applicationDesc.getEnumerationType(stringIDToTypeID("tool"))));
}
alert(currentToolName());
JJMack