Skip to main content
Participant
March 22, 2023
Answered

Script to rename color swatch names

  • March 22, 2023
  • 1 reply
  • 793 views

Hey folks,

 

I had a quick question that I was hoping someone could help me out with, as I looked at similar posts and didn't quite see what I was looking for. 

 

I have about 1500+ color swatches that are named in the format of "Color Name (XX 0000)" and I want to rename them to "XX 0000 Color Name". I have already formatted the names in an excel spreadsheet, and was wondering if there is a quick way to rename all of these swatches since I have the new names already batched out? 

 

Thanks to anyone who can help me with this! 

This topic has been closed for replies.
Correct answer r-bin

 

to c.pfaffenbichler

 

rename_color("Light Blue", "Light Blue New");

function rename_color(src, dst)
    {
    try {
        var r = new ActionReference();
        r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("presetManager"));
        r.putEnumerated(stringIDToTypeID("application"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
        var list = executeActionGet(r).getList(stringIDToTypeID("presetManager")).getObjectValue(1).getList(stringIDToTypeID("name"));

        for (var i = 0; i < list.count; i++) 
            {
            if (list.getString(i) == src)
                {
                var d = new ActionDescriptor();
                var r = new ActionReference();
                r.putIndex(stringIDToTypeID("colors"), i+1);
                d.putReference(stringIDToTypeID("null"), r);
                d.putString(stringIDToTypeID("to"), dst);
                executeAction(stringIDToTypeID("rename"), d, DialogModes.NO);
                return;
                }
            }

        alert("Color '" + src + "' not found!");
        } 
    catch(e) { alert(e); }
    }

 

It may not work in new photoshops, because something is wrong with the presetManager.

1 reply

c.pfaffenbichler
Community Expert
Community Expert
March 22, 2023

The question may be »quick« but I don’t see a »quick« solution in Photoshop, renaming color swatches does not seem to produce Scripting Listener code. 

 

Maybe it would be possible to export an aco-file and edit that in another application. 

Otherwise I think there might be possible work-arounds – that might not be efficient … 

@r-bin , @Stephen Marsh  , have you approached this issue in the past?

 

Which threads are you referring to? 

What tasks were they about? 

r-binCorrect answer
Legend
March 22, 2023

 

to c.pfaffenbichler

 

rename_color("Light Blue", "Light Blue New");

function rename_color(src, dst)
    {
    try {
        var r = new ActionReference();
        r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("presetManager"));
        r.putEnumerated(stringIDToTypeID("application"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
        var list = executeActionGet(r).getList(stringIDToTypeID("presetManager")).getObjectValue(1).getList(stringIDToTypeID("name"));

        for (var i = 0; i < list.count; i++) 
            {
            if (list.getString(i) == src)
                {
                var d = new ActionDescriptor();
                var r = new ActionReference();
                r.putIndex(stringIDToTypeID("colors"), i+1);
                d.putReference(stringIDToTypeID("null"), r);
                d.putString(stringIDToTypeID("to"), dst);
                executeAction(stringIDToTypeID("rename"), d, DialogModes.NO);
                return;
                }
            }

        alert("Color '" + src + "' not found!");
        } 
    catch(e) { alert(e); }
    }

 

It may not work in new photoshops, because something is wrong with the presetManager.

c.pfaffenbichler
Community Expert
Community Expert
March 23, 2023

Thank you! 

Seems to work fine in 24.3.0