Skip to main content
Inspiring
March 18, 2023
Answered

Script to get brush group when getting brush name

  • March 18, 2023
  • 3 replies
  • 2457 views

Hi, firstly I'm only tagging because that was recommended in a previous post and it got a hit almost immediately after a couple of days lingering. If it's considered pestering then let me know...and I won't do it so often 😉 

Tags : @jazz-y , @c.pfaffenbichler , @r-bin , @Stephen Marsh
I use the script below to get brush names in Photoshop. I've just come across a case of duplicate brush names and have two questions:
1. Having done some testing it seems Photoshop will create a new brush group and append a suffix when there are duplicates. However the brush name remains the same. Is this correct?
2. If the above is correct how does Photoshop know which brush to load when a brush is selected? ScriptListener only seems to note the brush name. There must be a unique ID somewhere?
2. Is there any way of getting the brush group name along with the brush name in the script below (or any other script)?

 

//script start
var names = getPresetList(0);

function getPresetList(presetIndex)
{
// presetIndex: 0 to 7
// 0: Brushes
// 1: Swatches
// 2: Gradients
// 3: Styles
// 4: Patterns
// 5: Contours
// 6: Custom Shapes
// 7: Tools

var presetNames = [];
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID("property"), stringIDToTypeID("presetManager"));
ref.putEnumerated(stringIDToTypeID("application"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
var desc = executeActionGet(ref);
var list = desc.getList(stringIDToTypeID("presetManager"));
var nameList = list.getObjectValue(presetIndex).getList(stringIDToTypeID("name"));
for (var nameIndex = 0; nameIndex < nameList.count; nameIndex++)
{
var n = nameList.getString(nameIndex);
presetNames.push(n);
}
return presetNames;
};
//script end

 

 

This topic has been closed for replies.
Correct answer VahxLar

All of the already installed Brushes on other machines or only your own one?

Because for computers »out of your control« that would seem a gamble. 


Yes, trying to support it beyond a local environment would be ruinous! It's a few machines within control, but users can do what they choose with their instance of Photoshop. And some like to do goodness knows what with their brushes 😉
That code which was referred to by yourself and @Stephen Marsh seems to be the answer. I haven't had a chance to understand it fully, but if I isolate the function select_brush_idx() and use some arbitrary indices it seems to select a brush by id rather than name without a problem.
On the surface this seems to be exactly what's needed. Many thanks.

function select_brush_idx(idx)

    {

    try

        {

        if (idx)

            {

            var d = new ActionDescriptor();

            var r = new ActionReference();

            r.putIndex(stringIDToTypeID("brush"), idx);

            d.putReference(stringIDToTypeID("null"), r);

            executeAction(stringIDToTypeID("select"), d, DialogModes.NO);

            return true;

            }

        return false;

        }

    catch (e) { return false; }

    }

 

3 replies

c.pfaffenbichler
Community Expert
Community Expert
March 20, 2023

Any Photoshop user could remove, rename, change, … the default Brushes so I suspect the reliable way to make sure the selected Brush is the one you want would be 

• to load an abr each time (and then remove it again) for sampled Brushes or

• create the Brush anew in the case of the basic Brushes. 

c.pfaffenbichler
Community Expert
Community Expert
March 20, 2023
quote

1. Having done some testing it seems Photoshop will create a new brush group and append a suffix when there are duplicates. However the brush name remains the same. Is this correct?

 

I don’t quite understand what you mean. 

Why are you duplicating the Groups? 

It is perefctly possible to have two Brushes of the same name in one Group. 

 

quote

Ideally there should be some sort of unique ID to identify the brush.

You can get the indices from r-bin’s code. 

VahxLarAuthor
Inspiring
March 20, 2023
quote

Why are you duplicating the Groups? 

I'm not, Photoshop is. Or at least that is the behaviour on my instance of version 2022 (23.5.4) when I tested this. If I import a brush (ie double click the abr file) then Photoshop creates a new group (the name of which seems to be based on the abr filename). If that group was already in Photoshop then it uses the name and appends an index suffix. But it will use the same brush name as the originally exported brush.

 

Also, as you've pointed out, Photoshop allows duplicate brush names in the same group. So internally Photoshop must be using a unique identifier. Why they don't expose this for use in scripts is is odd. I can only surmise that duplicate brush names are perceived as poor management and hence a user issue. The problem though is if you are someone that likes to purchase 3rd party packages (actions, brushes etc) it can be easy to end up with duplicate brush names.

 

I was just checking the behaviour with patterns, and pattern selection seems to use a GUID (unless I've misinterpreted the code below).

var idMk = charIDToTypeID( "Mk  " );
    var desc238 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref1 = new ActionReference();
        var idcontentLayer = stringIDToTypeID( "contentLayer" );
        ref1.putClass( idcontentLayer );
    desc238.putReference( idnull, ref1 );
    var idUsng = charIDToTypeID( "Usng" );
        var desc239 = new ActionDescriptor();
        var idType = charIDToTypeID( "Type" );
            var desc240 = new ActionDescriptor();
            var idPtrn = charIDToTypeID( "Ptrn" );
                var desc241 = new ActionDescriptor();
                var idNm = charIDToTypeID( "Nm  " );
                desc241.putString( idNm, """Grass-Autumn""" );
                var idIdnt = charIDToTypeID( "Idnt" );
                desc241.putString( idIdnt, """b25bc481-1c2d-ae45-8e7a-5cafcf44db94""" );
            var idPtrn = charIDToTypeID( "Ptrn" );
            desc240.putObject( idPtrn, idPtrn, desc241 );
        var idpatternLayer = stringIDToTypeID( "patternLayer" );
        desc239.putObject( idType, idpatternLayer, desc240 );
    var idcontentLayer = stringIDToTypeID( "contentLayer" );
    desc238.putObject( idUsng, idcontentLayer, desc239 );
executeAction( idMk, desc238, DialogModes.NO );

 

Thanks for that code - I will experiment with the indices.

c.pfaffenbichler
Community Expert
Community Expert
March 20, 2023

I think a global unique numbering of brushes could be problematic – considering the number of Photoshop users and how many Brushes they might create and share. 

 

What kind of brushes do you intend to use – plain round Brushes, Bristle and Erodible Brushes, Sampled Brushes? 

Stephen Marsh
Community Expert
Community Expert
March 19, 2023

Thank you for including me in such a prestigious list of scripters, however, this is above my pay grade! :]

 

Edit: Part of the answer may be found here from @r-bin:

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/select-refer-to-brushes-by-name-within-brush-sets-by-name/td-p/9979111

c.pfaffenbichler
Community Expert
Community Expert
March 19, 2023

Above mine, too. 

 

I suspect one could deduce the indices from the result of the function show_brushes_object() – starting at 1, with every group counting as well. 

 

@VahxLar , what is the ultimate point here? What are you trying to achieve with the information you are trying to collect? 

VahxLarAuthor
Inspiring
March 19, 2023

@c.pfaffenbichler"What are you trying to achieve with the information you are trying to collect?"

I've got a C#.Net application that calls javascript files to apply random brush strokes on a Photoshop document.
In the C# app, the user is presented with a list of brushes that have come from Photoshop. The script will then use the brush/brushes the user has selected by passing the brush names from the C# app to the script.
If there are duplicate brushes in Photoshop then Photoshop will eventually use the wrong brush because the only current means of identifying the brush that I'm aware of in script is the brush name. Ideally there should be some sort of unique ID to identify the brush.
If not, the next step would be to inform the user which group is holding the duplicate brushes so the user can then maybe clone the group/brush and rename it or maybe I can script this cloning process (haven't tried) and then ignore the duplicates. But that's starting to get messy if there are potentially thousands of brushes in Photoshop. Dunno, I'm up for any ideas.