Skip to main content
Known Participant
March 1, 2022
Question

Javascript - get current brush shape name

  • March 1, 2022
  • 4 replies
  • 1736 views

Hello,

 

I'm look for a way to get the current tool name, like "Hard Round 30 1"

I find how to get the preset name, like "paintbrushTool" but not the paintbrushTool's name.

 

getTool();
function getTool(){  
    var descc = new ActionDescriptor();
    var ref = new ActionReference();  
    ref.putEnumerated( charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );  
    var cTool = typeIDToStringID(executeActionGet(ref).getEnumerationType(stringIDToTypeID('tool'))); 
    alert(cTool);
}

Have you any idea ?

Thanks

 

 

4 replies

aallazAuthor
Known Participant
March 3, 2022
var w = new Window ("dialog", "Super Panel", undefined, {closeButton: true});
w.preferredSize = [150, 50];
w.alignChildren = [ "left", "fill"];
w.orientation = "row";

var btnGet = w.add ("button", undefined, "Get");
btnGet.onClick = function () { try{
    var ref = new ActionReference();
    var BrshPnlMode;
    ref.putEnumerated( charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
    var applicationDesc = executeActionGet(ref);
    var panelList = applicationDesc.getList(stringIDToTypeID('panelList'));
    for (var m = 0; m < panelList.count; m++) {
    var thisPanelDesc = panelList.getObjectValue(m);
    if (thisPanelDesc.getString(stringIDToTypeID("name")) == "Brush Settings") {
        if (thisPanelDesc.getBoolean(stringIDToTypeID("visible")) == false) { 
                var desc1 = new ActionDescriptor();
                var ref1 = new ActionReference();
                ref1.putEnumerated(charIDToTypeID('Mn  '), charIDToTypeID('MnIt'), charIDToTypeID('TglB'));
                desc1.putReference(stringIDToTypeID('null'), ref1);
                executeAction(charIDToTypeID('slct'), desc1, DialogModes.NO);
                BrshPnlMode = false;
            }
            else {
                BrshPnlMode = true;
            }
        };
    };

    sTT = stringIDToTypeID; 
    brsh = sTT('brush');
    (ref1 = new ActionReference()).putClass(sTT('brush'));
    (dsc = new ActionDescriptor()).putReference(sTT('null'), ref1), dsc.putString(sTT('name'), 'brsh');
    (ref2 = new ActionReference()).putProperty(sTT('property'), sTT('currentToolOptions'))
    ref2.putClass(sTT('application')), dsc.putReference(sTT('using'), ref2)
    executeAction(sTT('make'), dsc); (ref = new ActionReference())
    .putProperty(sTT('property'), sTT('brush'))
    ref.putClass(sTT('application'));

    brshs = eval('('+executeActionGet(ref).getString(brsh)+')').brushes;

    while(brshs.length) {
        if (brshs.pop().name == 'brsh') {
            dsc = new ActionDescriptor(); (ref = new ActionReference())
            .putEnumerated(sTT('brush'), sTT('ordinal'), sTT('targetEnum'))
            dsc.putReference(sTT('null'), ref), executeAction(sTT('delete'), dsc);
            (ref = new ActionReference()).putName(sTT('brush'), nme = brshs.pop().name);
            (dsc = new ActionDescriptor()).putReference(sTT('null'), ref)
            executeAction(sTT('select'), dsc), activeDocument.activeLayer.name = nme; break
        }
    }

    if (BrshPnlMode == false) { 
        executeAction(charIDToTypeID('slct'), desc1, DialogModes.NO);
    }
}catch(e){alert("No brush found");}finally{w.close ();}}

var btnSet = w.add ("button", undefined, "Set");
    btnSet.onClick = function () {try{
        var V1 = new ActionDescriptor();
        var V2 = new ActionReference();
        V2.putName(charIDToTypeID("Brsh"), activeDocument.activeLayer.name);
        V1.putReference(charIDToTypeID("null"), V2);
        executeAction(charIDToTypeID("slct"), V1, DialogModes.NO)
    }catch(e){alert("No brush found");}finally{w.close ();}}

w.show();
aallazAuthor
Known Participant
March 3, 2022

Amazing, Thank you so much !!

It's working well, excepted when the brush is on a folder. Do you know the reason why ?

 

Legend
March 2, 2022

As I understand it, you want to make the name of the current layer contain the name of the brush that was used when drawing it. In this case, you may want to follow the brush selection event:

 

/** Script renames current layer to brush preset's name
 * Usage:
 * - save script in file
 * - first run: event listener enabled
 * - next run: event listener disabled
 */

#target photoshop
var s2t = stringIDToTypeID,
    t2s = typeIDToStringID;
try {
    var target = t2s(arguments[0].getReference(s2t('null')).getDesiredClass());
    if (target == 'brush') {
        (r = new ActionReference()).putEnumerated(s2t('layer'), s2t('ordinal'), s2t('targetEnum'));
        (d = new ActionDescriptor()).putReference(s2t('target'), r);
        (d1 = new ActionDescriptor()).putString(s2t('name'), arguments[0].getReference(s2t('null')).getName());
        d.putObject(s2t('to'), s2t('layer'), d1);
        executeAction(s2t('set'), d, DialogModes.NO);
    }
} catch (e) {}
if (!target) {
    app.notifiersEnabled = true
    var f = File($.fileName),
        deleted;
    for (var i = 0; i < app.notifiers.length; i++) {
        var ntf = app.notifiers[i]
        if (ntf.eventFile.name == f.name) { ntf.remove(); i--; deleted = true }
    }
    if (deleted) {
        alert('event listening disabled!')
    } else {
        app.notifiers.add('slct', f)
        alert('event listening enabled!')
    }
}

 

Kukurykus
Legend
March 2, 2022

I wrote similar script, but since author did not say originally the script has to rename active layer I didn't link him to appropriate thread (Getting the name of the current brush preset). Now he may use yours script and mine on the condition he'll choose regular Brushes panel, so not one from flyout menu.

Kukurykus
Legend
March 1, 2022
sTT = stringIDToTypeID, brsh = sTT('brush');
(ref = new ActionReference()).putProperty(sTT('property'), brsh)
ref.putClass(sTT('application')); eval('(' + executeActionGet(ref)
.getString(brsh) + ')').brushes[0].children[0].name
aallazAuthor
Known Participant
March 1, 2022

Thank you for your help !

I got "1px Line"  whatever the brush I choose

Kukurykus
Legend
March 2, 2022

No sorry

I copy it again, may be I do wrong ? First time it give me the name of the last brush "RCligneFeuilleC" and then "brsh". It add each time a new brush preset "brsh" without deleting the others.

sTT = stringIDToTypeID; 
brsh = sTT('brush');
(ref1 = new ActionReference()).putClass(sTT('brush'));
(dsc = new ActionDescriptor()).putReference(sTT('null'), ref1), dsc.putString(sTT('name'), 'brsh');
(ref2 = new ActionReference()).putProperty(sTT('property'), sTT('currentToolOptions'))
ref2.putClass(sTT('application')), dsc.putReference(sTT('using'), ref2)
executeAction(sTT('make'), dsc); (ref = new ActionReference())
.putProperty(sTT('property'), sTT('brush'))
ref.putClass(sTT('application'));

//brshs = eval('(' + executeActionGet(ref).getString(sTT('brush')) + ')').brushes;
brshs = eval('('+executeActionGet(ref).getString(brsh)+')').brushes;
while(brshs.length) {
	if (brshs.pop().name == 'brsh') {
		dsc = new ActionDescriptor(); (ref = new ActionReference())
		.putEnumerated(sTT('brush'), sTT('ordinal'), sTT('targetEnum'))
		dsc.putReference(sTT('null'), ref), executeAction(sTT('delete'), dsc);
		(ref = new ActionReference()).putName(sTT('brush'), nme = brshs.pop().name);
		(dsc = new ActionDescriptor()).putReference(sTT('null'), ref)
		executeAction(sTT('select'), dsc), alert(nme); break
	}
}

  


This code is correct and works for me as intended in Photoshop 23.2.1 on Windows. Make a test removing all your containers and leave only about 10 different brushes. Also the above code put in try...catch statement to see if there's some error on your side:

 

 

try{
	/*replace this line to the code*/
}
catch(err){alert(err +'\n\n' + err.line)}