Javascript - get current brush shape name

Community Beginner ,
Mar 01, 2022 Mar 01, 2022

Copy link to clipboard

Copied

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

 

 

TOPICS
Actions and scripting

Views

393

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 01, 2022 Mar 01, 2022

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 01, 2022 Mar 01, 2022

Copy link to clipboard

Copied

Thank you for your help !

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 01, 2022 Mar 01, 2022

Copy link to clipboard

Copied

That was for first brush in first folder. Now play with no folders, just brushes:

 

sTT = stringIDToTypeID; (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;

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
	}
}

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 01, 2022 Mar 01, 2022

Copy link to clipboard

Copied

Ok but how to get the current brush name if I don't know it? This return all the list no ?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 01, 2022 Mar 01, 2022

Copy link to clipboard

Copied

Did you test above code with removed brush containers?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 01, 2022 Mar 01, 2022

Copy link to clipboard

Copied

Yes I guess. Maybe I didn't understand...

I select the first "Hard Round 30 1", start your script that give me an alert message with "RCligneFeuilleC" that is the last one and that creat the "brsh"

Capture d’écran 2022-03-01 182227.jpgCapture d’écran 2022-03-01 182339.jpg

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 01, 2022 Mar 01, 2022

Copy link to clipboard

Copied

I made mistake, so now use edited version of code where I changed wrong line:

 

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

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 02, 2022 Mar 02, 2022

Copy link to clipboard

Copied

Sorry I don't know it doesn't change. This creat a brush "brsh" with the same properties than the current brush and select it.

 

My goal is to make a psd template with some brushes drawing exemples in separate layers. I want to get the current brush name (what I am looking for) and rename the layer with it

BrushName = ?
var document = app.activeDocument;
document.activeLayer.name = BrushName; 

So later when I select the layer, I can select the related brush

var V1 = new ActionDescriptor();
var V2 = new ActionReference();
V2.putName(charIDToTypeID("Brsh"), document.activeLayer.name);
V1.putReference(charIDToTypeID("null"), V2);
executeAction(charIDToTypeID("slct"), V1, DialogModes.NO)

 

Thanks again for your help !

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 02, 2022 Mar 02, 2022

Copy link to clipboard

Copied

The last version of script with changed line from other post should alert you name of selected brush. Use it only on brushes that are not contained in a folder and tell me if that works...

 

Instead of alert(nme) use activeDocument.activeLayer.name = nme

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 02, 2022 Mar 02, 2022

Copy link to clipboard

Copied

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
	}
}

  

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 02, 2022 Mar 02, 2022

Copy link to clipboard

Copied

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

 

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 02, 2022 Mar 02, 2022

Copy link to clipboard

Copied

Strange, I try with 8 brushes, no folder and it do the same. And it catch no error.

I have also the last update 23.2.1, windows 10. I try it on 2 computers (but with same windows and photoshop vr) and same error 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 02, 2022 Mar 02, 2022

Copy link to clipboard

Copied

Let's see if any other ACP willing to try out my script has the same problem as you: jazz-y, Stephen_A_Marshc.pfaffenbichlerr-binLumigraphicspixxxelschubserChuck Uebele.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 02, 2022 Mar 02, 2022

Copy link to clipboard

Copied

Everything works. But with some reservations.

In my 23.2.1 script creates a new temporary brush not after the current one, but at the end of the list of brushes. Accordingly, when you try to delete it, it displays not the one selected at the time the script was launched, but the last one in the list.

In my case script does not delete the temporary brush (however, it switches to the previous one, as if the deletion had been made).

I can't figure out what is causing this behavior.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 02, 2022 Mar 02, 2022

Copy link to clipboard

Copied

Aside scripting when you don't select any brush and creates new brush it's added at the end of all brushes, but when one is selected it's for me created after that selected brush. I have no idea why it doesn't work for you same as for me 😕

 

The script should add the 'brsh', then delete it, to finally select the originally selected brush. In the meantime this tricky method lets get the name of the selected brush to alert it at end.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 02, 2022 Mar 02, 2022

Copy link to clipboard

Copied

I spent some time checking. The behavior depends on whether the brush panel (window -> brushes) has been launched at least once (in the current session) or not.

 

If you open Photoshop and do not open (or expand if it minimized) the brushes panel and use only brush flyout menus, then the behavior of the script will be as I described above.

 

If you open the brushes panel at least once, the script will work correctly until the next start of photoshop.

 

Check it. It looks like this is another bug that will never be fixed 🙂

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 02, 2022 Mar 02, 2022

Copy link to clipboard

Copied

The workaround is to each time make a loop over panels. If Brushes are hidden then toogle them, read some parameter of unfocused brush yet to overset it, then make refreshment, and finally use proper part of code.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 02, 2022 Mar 02, 2022

Copy link to clipboard

Copied

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!')
    }
}

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 02, 2022 Mar 02, 2022

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 03, 2022 Mar 03, 2022

Copy link to clipboard

Copied

Amazing, Thank you so much !!

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

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 03, 2022 Mar 03, 2022

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines