Skip to main content
Participant
Aug 21, 2024 3:32 AM UTC
Answered

Can I set a hotkey for Color Label?

Hello everyone,

 

Is it possible to set a hotkey for adding color labels to layers? I like to categorize my layers using different colors, like red, green, and blue. However, I currently have to right-click on each layer and select the color, which is time-consuming.

Is there a way to configure it so that I can click on a layer and press, for example, F1 for a Red Label, F2 for a Blue Label, and so on?

 

Thank you in advance!

🥹🙏💖

This topic is closed to new replies. Start a new post to keep the conversation going.
Bojan Živković11378569
Community Expert
Aug 22, 2024 4:59 AM UTC

It appears the only method is to record the action for setting the color label on the current layer, then assign a keyboard shortcut to execute that action.

20 replies

Stephen Marsh
Community Expert
Community Expert
Sep 26, 2024 7:59 AM UTC

For version 2023 or earlier

Cycle Through Layer Color Labels - Forward - ALT for Backward.jsx
NOTE: Not intended for use with a keyboard shortcut, only for execution via the script menu or an action. Hold down the alt/opt key to reverse
 
/*
Cycle Through Layer Color Labels - Forward - ALT for Backward.jsx
v1.0 - 26th September 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/can-i-set-a-hotkey-for-color-label/td-p/14812505
NOTE: Not intended for use with a keyboard shortcut, only for execution via the script menu or an action. Hold down the alt/opt key to reverse
*/

#target photoshop

// Ensure that version 2023 or earlier is being used
var versionNumber = app.version.split(".");
var versionCheck = parseInt(versionNumber[0]);

if (versionCheck <= 24) {

    // Main Execution: Get current layer color and determine its index
    try {
        // Get the current layer color
        var activeLayerColor = getLayerColour();
        //alert("Current color is: " + activeLayerColor);  // Log the current color

        if (activeLayerColor !== null) {
            // Proceed to set the next color
            setNextLayerLabelCol(activeLayerColor);
        } else {
            //alert("Error: Couldn't retrieve the layer's label color.");
        }
    } catch (e) {
        //alert("Error in main execution: " + e.message);
    }

    // Function to get the active layer color label
    function getLayerColour() {
        try {
            var ref = new ActionReference();
            ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
            var desc = executeActionGet(ref);

            if (desc.hasKey(stringIDToTypeID('color'))) {
                var colorID = desc.getEnumerationValue(stringIDToTypeID('color'));
                return typeIDToStringID(colorID); // Return color label as a string
            } else {
                return "none"; // If no color label is found
            }
        } catch (e) {
            //alert("Error retrieving layer color: " + e.message);
            return null;
        }
    }

    // Function to determine index of the current color and set the next color
    function setNextLayerLabelCol(currentColor) {
        // Define colorList as an array (original and new colors)
        var colorList = [
            "none",
            "red",
            "orange",
            "yellowColor", // Yellow
            "grain", // Green
            "blue",
            "violet",
            "gray"
        ];

        // Debugging output to check colorList definition
        if (typeof colorList !== "object" || !colorList.length) {
            //alert("Error: colorList is not defined correctly!");
            return; // Exit if colorList is not defined as expected
        }

        try {
            // Find the index of the current color
            var currentIndex = -1;
            for (var i = 0; i < colorList.length; i++) {
                if (colorList[i] === currentColor) {
                    currentIndex = i;
                    break;
                }
            }

            // If the current color is not found, default to the first color
            if (currentIndex === -1) {
                //alert("Error: Current color not found in the list. Setting to 'none'.");
                currentIndex = 0; // Default to 'none' if not found
            } else {
                // Check if the Alt/Opt key is pressed using ScriptUI
                var isAltPressed = ScriptUI.environment.keyboardState.altKey;

                if (isAltPressed) {
                    // Alt/Opt key pressed - reverse loop
                    currentIndex = (currentIndex - 1 + colorList.length) % colorList.length; // Loop back to the end
                } else {
                    // Forward loop
                    currentIndex = (currentIndex + 1) % colorList.length; // Loop back to start
                }
            }

            var nextColor = colorList[currentIndex];
            //alert("Setting new color to: " + nextColor);  // Log the new color

            // Apply the new layer label color
            if (!app.activeDocument.activeLayer.isBackgroundLayer) {
                setLayerLabelCol(nextColor);
            }
        } catch (e) {
            //alert("Error determining color index: " + e.message);
        }
    }

    // Function to set the new layer label color
    function setLayerLabelCol(labelCol) {
        var c2t = function (s) {
            return app.charIDToTypeID(s);
        };
        var s2t = function (s) {
            return app.stringIDToTypeID(s);
        };

        var descriptor = new ActionDescriptor();
        var reference = new ActionReference();
        reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
        descriptor.putReference(c2t("null"), reference);

        // Create a descriptor for the color
        var colorDescriptor = new ActionDescriptor();
        try {
            // Use stringIDToTypeID for the new colors
            var colorID = stringIDToTypeID(labelCol); // Convert label to ID

            // Check if the colorID is valid
            if (colorID === undefined) {
                //alert("Invalid color ID for: " + labelCol);
                return; // Exit if the color ID is not valid
            }

            // Debugging color ID
            //alert("Color ID for '" + labelCol + "': " + colorID);

            colorDescriptor.putEnumerated(s2t("color"), s2t("color"), colorID);

            // Apply the new color label to the layer
            descriptor.putObject(s2t("to"), s2t("layer"), colorDescriptor);

            executeAction(s2t("set"), descriptor, DialogModes.NO);
            //alert("Successfully set layer color to: " + labelCol);
        } catch (e) {
            //alert("Error applying color: " + labelCol + ", " + e.message);
        }
    }

} else {

    alert("You must use Photoshop 2023 or earlier to use this script!");

}

 

Translate
Stephen Marsh
Community Expert
Community Expert
Sep 26, 2024 7:58 AM UTC

For version 2023 or earlier

Cycle Through Layer Color Labels - Backward.jsx
NOTE: Intended for use with a custom keyboard shortcut when the script is installed into the ...Presets/Scripts folder
/*
Cycle Through Layer Color Labels - Backward.jsx
v1.0 - 26th September 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/can-i-set-a-hotkey-for-color-label/td-p/14812505
NOTE: Intended for use with a custom keyboard shortcut when the script is installed into the ...Presets/Scripts folder
*/

#target photoshop

// Ensure that version 2023 or earlier is being used
var versionNumber = app.version.split(".");
var versionCheck = parseInt(versionNumber[0]);

if (versionCheck <= 24) {

    // Main Execution: Get current layer color and determine its index
try {
    // Get the current layer color
    var activeLayerColor = getLayerColour();
    //alert("Current color is: " + activeLayerColor);  // Log the current color

    if (activeLayerColor !== null) {
        // Proceed to set the next color
        setPreviousLayerLabelCol(activeLayerColor);
    } else {
        //alert("Error: Couldn't retrieve the layer's label color.");
    }
} catch (e) {
    //alert("Error in main execution: " + e.message);
}

// Function to get the active layer color label
function getLayerColour() {
    try {
        var ref = new ActionReference();
        ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
        var desc = executeActionGet(ref);

        if (desc.hasKey(stringIDToTypeID('color'))) {
            var colorID = desc.getEnumerationValue(stringIDToTypeID('color'));
            return typeIDToStringID(colorID);  // Return color label as a string
        } else {
            return "none";  // If no color label is found
        }
    } catch (e) {
        //alert("Error retrieving layer color: " + e.message);
        return null;
    }
}

// Function to determine index of the current color and set the next color
function setPreviousLayerLabelCol(currentColor) {
    // Define colorList as an array (original and new colors)
    var colorList = [
        "none",
        "red",
        "orange",
        "yellowColor", // Yellow
        "grain", // Green
        "blue",
        "violet",
        "gray"
    ];

    try {
        // Debugging the current color
        //alert("Current color before lookup: " + currentColor);
        
        // Manually find the index of the current color
        var currentIndex = -1;
        for (var i = 0; i < colorList.length; i++) {
            if (colorList[i] === currentColor) {
                currentIndex = i;
                break;
            }
        }

        // If the current color is not found, set index to the last color
        if (currentIndex === -1) {
            //alert("Error: Current color not found in the list. Setting to 'none'.");
            currentIndex = 0; // Default to 'none' if not found
        } else {
            // Set to the previous color in the list (backward loop)
            currentIndex = (currentIndex - 1 + colorList.length) % colorList.length; // Loop back to the end
        }

        var nextColor = colorList[currentIndex];
        //alert("Setting new color to: " + nextColor);  // Log the new color

        // Apply the new layer label color
        if (!app.activeDocument.activeLayer.isBackgroundLayer) {
            setLayerLabelCol(nextColor);
        }
    } catch (e) {
        //alert("Error determining color index: " + e.message);
    }
}

// Function to set the new layer label color
function setLayerLabelCol(labelCol) {
    var c2t = function (s) {
        return app.charIDToTypeID(s);
    };
    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    };

    var descriptor = new ActionDescriptor();
    var reference = new ActionReference();
    reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
    descriptor.putReference(c2t("null"), reference);

    // Create a descriptor for the color
    var colorDescriptor = new ActionDescriptor();
    try {
        // Use stringIDToTypeID for the new colors
        var colorID = stringIDToTypeID(labelCol); // Convert label to ID
        
        // Check if the colorID is valid
        if (colorID === undefined) {
            //alert("Invalid color ID for: " + labelCol);
            return; // Exit if the color ID is not valid
        }

        // Debugging color ID
        //alert("Color ID for '" + labelCol + "': " + colorID);

        colorDescriptor.putEnumerated(s2t("color"), s2t("color"), colorID);

        // Apply the new color label to the layer
        descriptor.putObject(s2t("to"), s2t("layer"), colorDescriptor);
        
        executeAction(s2t("set"), descriptor, DialogModes.NO);
        //alert("Successfully set layer color to: " + labelCol);
    } catch (e) {
        //alert("Error applying color: " + labelCol + ", " + e.message);
    }
}

} else {

    alert("You must use Photoshop 2023 or earlier to use this script!");

}

 

Translate
Stephen Marsh
Community Expert
Community Expert
Sep 26, 2024 7:57 AM UTC

For version 2023 or earlier

Cycle Through Layer Color Labels - Forward.jsx
NOTE: Intended for use with a custom keyboard shortcut when the script is installed into the ...Presets/Scripts folder
 
/*
Cycle Through Layer Color Labels - Forward.jsx
v1.0 - 26th September 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/can-i-set-a-hotkey-for-color-label/td-p/14812505
NOTE: Intended for use with a custom keyboard shortcut when the script is installed into the ...Presets/Scripts folder
*/

#target photoshop

// Ensure that version 2023 or earlier is being used
var versionNumber = app.version.split(".");
var versionCheck = parseInt(versionNumber[0]);

if (versionCheck <= 24) {

    // Main Execution: Get current layer color and determine its index
    try {
        // Get the current layer color
        var activeLayerColor = getLayerColour();
        //alert("Current color is: " + activeLayerColor);  // Log the current color

        if (activeLayerColor !== null) {
            // Proceed to set the next color
            setNextLayerLabelCol(activeLayerColor);
        } else {
            //alert("Error: Couldn't retrieve the layer's label color.");
        }
    } catch (e) {
        //alert("Error in main execution: " + e.message);
    }

    // Function to get the active layer color label
    function getLayerColour() {
        try {
            var ref = new ActionReference();
            ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
            var desc = executeActionGet(ref);

            if (desc.hasKey(stringIDToTypeID('color'))) {
                var colorID = desc.getEnumerationValue(stringIDToTypeID('color'));
                return typeIDToStringID(colorID); // Return color label as a string
            } else {
                return "none"; // If no color label is found
            }
        } catch (e) {
            //alert("Error retrieving layer color: " + e.message);
            return null;
        }
    }

    // Function to determine index of the current color and set the next color
    function setNextLayerLabelCol(currentColor) {
        // Define colorList as an array (original and new colors)
        var colorList = [
            "none",
            "red",
            "orange",
            "yellowColor", // Yellow
            "grain", // Green
            "blue",
            "violet",
            "gray"
        ];

        try {
            // Debugging the current color
            //alert("Current color before lookup: " + currentColor);

            // Manually find the index of the current color
            var currentIndex = -1;
            for (var i = 0; i < colorList.length; i++) {
                if (colorList[i] === currentColor) {
                    currentIndex = i;
                    break;
                }
            }

            // If the current color is not found, set index to 0
            if (currentIndex === -1) {
                //alert("Error: Current color not found in the list. Setting to 'none'.");
                currentIndex = 0; // Default to 'none' if not found
            } else {
                // Set to next color in the list
                currentIndex = (currentIndex + 1) % colorList.length; // Loop back to start
            }

            var nextColor = colorList[currentIndex];
            //alert("Setting new color to: " + nextColor);  // Log the new color

            // Apply the new layer label color
            if (!app.activeDocument.activeLayer.isBackgroundLayer) {
                setLayerLabelCol(nextColor);
            }
        } catch (e) {
            //alert("Error determining color index: " + e.message);
        }
    }

    // Function to set the new layer label color
    function setLayerLabelCol(labelCol) {
        var c2t = function (s) {
            return app.charIDToTypeID(s);
        };
        var s2t = function (s) {
            return app.stringIDToTypeID(s);
        };

        var descriptor = new ActionDescriptor();
        var reference = new ActionReference();
        reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
        descriptor.putReference(c2t("null"), reference);

        // Create a descriptor for the color
        var colorDescriptor = new ActionDescriptor();
        try {
            // Use stringIDToTypeID for the new colors
            var colorID = stringIDToTypeID(labelCol); // Convert label to ID

            // Check if the colorID is valid
            if (colorID === undefined) {
                //alert("Invalid color ID for: " + labelCol);
                return; // Exit if the color ID is not valid
            }

            // Debugging color ID
            //alert("Color ID for '" + labelCol + "': " + colorID);

            colorDescriptor.putEnumerated(s2t("color"), s2t("color"), colorID);

            // Apply the new color label to the layer
            descriptor.putObject(s2t("to"), s2t("layer"), colorDescriptor);

            executeAction(s2t("set"), descriptor, DialogModes.NO);
            //alert("Successfully set layer color to: " + labelCol);
        } catch (e) {
            //alert("Error applying color: " + labelCol + ", " + e.message);
        }
    }

} else {

    alert("You must use Photoshop 2023 or earlier to use this script!");

}

 

Translate
Stephen Marsh
Community Expert
Community Expert
Sep 26, 2024 7:52 AM UTC

For version 2024 >

Cycle Through Layer Color Labels - Forward - ALT for Backward v2024.jsx
NOTE: Not intended for use with a keyboard shortcut, only for execution via the script menu or an action. Hold down the alt/opt key to reverse
 
/*
Cycle Through Layer Color Labels - Forward - ALT for Backward v2024.jsx
v1.0 - 26th September 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/can-i-set-a-hotkey-for-color-label/td-p/14812505
NOTE: Not intended for use with a keyboard shortcut, only for execution via the script menu or an action. Hold down the alt/opt key to reverse
*/

#target photoshop

// Ensure that version 2024 or later is being used
var versionNumber = app.version.split(".");
var versionCheck = parseInt(versionNumber[0]);

if (versionCheck < 25) {

    alert("You must use Photoshop 2024 or later to use this script!");

} else {

    // Main Execution: Get current layer color and determine its index
    try {
        // Get the current layer color
        var activeLayerColor = getLayerColour();
        //alert("Current color is: " + activeLayerColor);  // Log the current color

        if (activeLayerColor !== null) {
            // Proceed to set the next color
            setNextLayerLabelCol(activeLayerColor);
        } else {
            //alert("Error: Couldn't retrieve the layer's label color.");
        }
    } catch (e) {
        //alert("Error in main execution: " + e.message);
    }

    // Function to get the active layer color label
    function getLayerColour() {
        try {
            var ref = new ActionReference();
            ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
            var desc = executeActionGet(ref);

            if (desc.hasKey(stringIDToTypeID('color'))) {
                var colorID = desc.getEnumerationValue(stringIDToTypeID('color'));
                return typeIDToStringID(colorID); // Return color label as a string
            } else {
                return "none"; // If no color label is found
            }
        } catch (e) {
            //alert("Error retrieving layer color: " + e.message);
            return null;
        }
    }

    // Function to determine index of the current color and set the next color
    function setNextLayerLabelCol(currentColor) {
        // Define colorList as an array (original and new colors)
        var colorList = [
            "none",
            "red",
            "orange",
            "yellowColor",
            "grain", // Green
            "seafoam", // New in 2024
            "blue",
            "indigo", // New in 2024
            "magenta", // New in 2024
            "fuchsia", // New in 2024
            "violet",
            "gray" // Gray
        ];

        // Debugging output to check colorList definition
        if (typeof colorList !== "object" || !colorList.length) {
            //alert("Error: colorList is not defined correctly!");
            return; // Exit if colorList is not defined as expected
        }

        try {
            // Find the index of the current color
            var currentIndex = -1;
            for (var i = 0; i < colorList.length; i++) {
                if (colorList[i] === currentColor) {
                    currentIndex = i;
                    break;
                }
            }

            // If the current color is not found, default to the first color
            if (currentIndex === -1) {
                //alert("Error: Current color not found in the list. Setting to 'none'.");
                currentIndex = 0; // Default to 'none' if not found
            } else {
                // Check if the Alt/Opt key is pressed using ScriptUI
                var isAltPressed = ScriptUI.environment.keyboardState.altKey;

                if (isAltPressed) {
                    // Alt/Opt key pressed - reverse loop
                    currentIndex = (currentIndex - 1 + colorList.length) % colorList.length; // Loop back to the end
                } else {
                    // Forward loop
                    currentIndex = (currentIndex + 1) % colorList.length; // Loop back to start
                }
            }

            var nextColor = colorList[currentIndex];
            //alert("Setting new color to: " + nextColor);  // Log the new color

            // Apply the new layer label color
            if (!app.activeDocument.activeLayer.isBackgroundLayer) {
                setLayerLabelCol(nextColor);
            }
        } catch (e) {
            //alert("Error determining color index: " + e.message);
        }
    }

    // Function to set the new layer label color
    function setLayerLabelCol(labelCol) {
        var c2t = function (s) {
            return app.charIDToTypeID(s);
        };
        var s2t = function (s) {
            return app.stringIDToTypeID(s);
        };

        var descriptor = new ActionDescriptor();
        var reference = new ActionReference();
        reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
        descriptor.putReference(c2t("null"), reference);

        // Create a descriptor for the color
        var colorDescriptor = new ActionDescriptor();
        try {
            // Use stringIDToTypeID for the new colors
            var colorID = stringIDToTypeID(labelCol); // Convert label to ID

            // Check if the colorID is valid
            if (colorID === undefined) {
                //alert("Invalid color ID for: " + labelCol);
                return; // Exit if the color ID is not valid
            }

            // Debugging color ID
            //alert("Color ID for '" + labelCol + "': " + colorID);

            colorDescriptor.putEnumerated(s2t("color"), s2t("color"), colorID);

            // Apply the new color label to the layer
            descriptor.putObject(s2t("to"), s2t("layer"), colorDescriptor);

            executeAction(s2t("set"), descriptor, DialogModes.NO);
            //alert("Successfully set layer color to: " + labelCol);
        } catch (e) {
            //alert("Error applying color: " + labelCol + ", " + e.message);
        }
    }

}

 

Translate
Stephen Marsh
Community Expert
Community Expert
Sep 26, 2024 7:50 AM UTC

For version 2024 >

Cycle Through Layer Color Labels - Backward v2024.jsx
NOTE: Intended for use with a custom keyboard shortcut when the script is installed into the ...Presets/Scripts folder 

 

/*
Cycle Through Layer Color Labels - Backward v2024.jsx
v1.0 - 26th September 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/can-i-set-a-hotkey-for-color-label/td-p/14812505
NOTE: Intended for use with a custom keyboard shortcut when the script is installed into the ...Presets/Scripts folder
*/

#target photoshop

// Ensure that version 2024 or later is being used
var versionNumber = app.version.split(".");
var versionCheck = parseInt(versionNumber[0]);

if (versionCheck < 25) {

    alert("You must use Photoshop 2024 or later to use this script!");

} else {

    // Main Execution: Get current layer color and determine its index
    try {
        // Get the current layer color
        var activeLayerColor = getLayerColour();
        //alert("Current color is: " + activeLayerColor);  // Log the current color

        if (activeLayerColor !== null) {
            // Proceed to set the next color
            setPreviousLayerLabelCol(activeLayerColor);
        } else {
            //alert("Error: Couldn't retrieve the layer's label color.");
        }
    } catch (e) {
        //alert("Error in main execution: " + e.message);
    }

    // Function to get the active layer color label
    function getLayerColour() {
        try {
            var ref = new ActionReference();
            ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
            var desc = executeActionGet(ref);

            if (desc.hasKey(stringIDToTypeID('color'))) {
                var colorID = desc.getEnumerationValue(stringIDToTypeID('color'));
                return typeIDToStringID(colorID); // Return color label as a string
            } else {
                return "none"; // If no color label is found
            }
        } catch (e) {
            //alert("Error retrieving layer color: " + e.message);
            return null;
        }
    }

    // Function to determine index of the current color and set the next color
    function setPreviousLayerLabelCol(currentColor) {
        // Define colorList as an array (original and new colors)
        var colorList = [
            "none",
            "red",
            "orange",
            "yellowColor",
            "grain", // Green
            "seafoam", // New in 2024
            "blue",
            "indigo", // New in 2024
            "magenta", // New in 2024
            "fuchsia", // New in 2024
            "violet",
            "gray" // Gray
        ];

        try {
            // Debugging the current color
            //alert("Current color before lookup: " + currentColor);

            // Manually find the index of the current color
            var currentIndex = -1;
            for (var i = 0; i < colorList.length; i++) {
                if (colorList[i] === currentColor) {
                    currentIndex = i;
                    break;
                }
            }

            // If the current color is not found, set index to the last color
            if (currentIndex === -1) {
                //alert("Error: Current color not found in the list. Setting to 'none'.");
                currentIndex = 0; // Default to 'none' if not found
            } else {
                // Set to the previous color in the list (backward loop)
                currentIndex = (currentIndex - 1 + colorList.length) % colorList.length; // Loop back to the end
            }

            var nextColor = colorList[currentIndex];
            //alert("Setting new color to: " + nextColor);  // Log the new color

            // Apply the new layer label color
            if (!app.activeDocument.activeLayer.isBackgroundLayer) {
                setLayerLabelCol(nextColor);
            }
        } catch (e) {
            //alert("Error determining color index: " + e.message);
        }
    }

    // Function to set the new layer label color
    function setLayerLabelCol(labelCol) {
        var c2t = function (s) {
            return app.charIDToTypeID(s);
        };
        var s2t = function (s) {
            return app.stringIDToTypeID(s);
        };

        var descriptor = new ActionDescriptor();
        var reference = new ActionReference();
        reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
        descriptor.putReference(c2t("null"), reference);

        // Create a descriptor for the color
        var colorDescriptor = new ActionDescriptor();
        try {
            // Use stringIDToTypeID for the new colors
            var colorID = stringIDToTypeID(labelCol); // Convert label to ID

            // Check if the colorID is valid
            if (colorID === undefined) {
                //alert("Invalid color ID for: " + labelCol);
                return; // Exit if the color ID is not valid
            }

            // Debugging color ID
            //alert("Color ID for '" + labelCol + "': " + colorID);

            colorDescriptor.putEnumerated(s2t("color"), s2t("color"), colorID);

            // Apply the new color label to the layer
            descriptor.putObject(s2t("to"), s2t("layer"), colorDescriptor);

            executeAction(s2t("set"), descriptor, DialogModes.NO);
            //alert("Successfully set layer color to: " + labelCol);
        } catch (e) {
            //alert("Error applying color: " + labelCol + ", " + e.message);
        }
    }

}

 

 

Translate
Stephen Marsh
Community Expert
Community Expert
Sep 26, 2024 7:49 AM UTC

For version 2024 >

Cycle Through Layer Color Labels - Forward v2024.jsx
NOTE: Intended for use with a custom keyboard shortcut when the script is installed into the ...Presets/Scripts folder 

 

/*
Cycle Through Layer Color Labels - Forward v2024.jsx
v1.0 - 26th September 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/can-i-set-a-hotkey-for-color-label/td-p/14812505
NOTE: Intended for use with a custom keyboard shortcut when the script is installed into the ...Presets/Scripts folder
*/

#target photoshop

// Ensure that version 2024 or later is being used
var versionNumber = app.version.split(".");
var versionCheck = parseInt(versionNumber[0]);

if (versionCheck < 25) {

    alert("You must use Photoshop 2024 or later to use this script!");

} else {

    // Main Execution: Get current layer color and determine its index
    try {
        // Get the current layer color
        var activeLayerColor = getLayerColour();
        //alert("Current color is: " + activeLayerColor);  // Log the current color

        if (activeLayerColor !== null) {
            // Proceed to set the next color
            setNextLayerLabelCol(activeLayerColor);
        } else {
            //alert("Error: Couldn't retrieve the layer's label color.");
        }
    } catch (e) {
        //alert("Error in main execution: " + e.message);
    }

    // Function to get the active layer color label
    function getLayerColour() {
        try {
            var ref = new ActionReference();
            ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
            var desc = executeActionGet(ref);

            if (desc.hasKey(stringIDToTypeID('color'))) {
                var colorID = desc.getEnumerationValue(stringIDToTypeID('color'));
                return typeIDToStringID(colorID); // Return color label as a string
            } else {
                return "none"; // If no color label is found
            }
        } catch (e) {
            //alert("Error retrieving layer color: " + e.message);
            return null;
        }
    }

    // Function to determine index of the current color and set the next color
    function setNextLayerLabelCol(currentColor) {
        // Define colorList as an array (original and new colors)
        var colorList = [
            "none",
            "red",
            "orange",
            "yellowColor",
            "grain", // Green
            "seafoam", // New in 2024
            "blue",
            "indigo", // New in 2024
            "magenta", // New in 2024
            "fuchsia", // New in 2024
            "violet",
            "gray" // Gray
        ];

        try {
            // Debugging the current color
            //alert("Current color before lookup: " + currentColor);

            // Manually find the index of the current color
            var currentIndex = -1;
            for (var i = 0; i < colorList.length; i++) {
                if (colorList[i] === currentColor) {
                    currentIndex = i;
                    break;
                }
            }

            // If the current color is not found, set index to 0
            if (currentIndex === -1) {
                //alert("Error: Current color not found in the list. Setting to 'none'.");
                currentIndex = 0; // Default to 'none' if not found
            } else {
                // Set to next color in the list
                currentIndex = (currentIndex + 1) % colorList.length; // Loop back to start
            }

            var nextColor = colorList[currentIndex];
            //alert("Setting new color to: " + nextColor);  // Log the new color

            // Apply the new layer label color
            if (!app.activeDocument.activeLayer.isBackgroundLayer) {
                setLayerLabelCol(nextColor);
            }
        } catch (e) {
            //alert("Error determining color index: " + e.message);
        }
    }

    // Function to set the new layer label color
    function setLayerLabelCol(labelCol) {
        var c2t = function (s) {
            return app.charIDToTypeID(s);
        };
        var s2t = function (s) {
            return app.stringIDToTypeID(s);
        };

        var descriptor = new ActionDescriptor();
        var reference = new ActionReference();
        reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
        descriptor.putReference(c2t("null"), reference);

        // Create a descriptor for the color
        var colorDescriptor = new ActionDescriptor();
        try {
            // Use stringIDToTypeID for the new colors
            var colorID = stringIDToTypeID(labelCol); // Convert label to ID

            // Check if the colorID is valid
            if (colorID === undefined) {
                //alert("Invalid color ID for: " + labelCol);
                return; // Exit if the color ID is not valid
            }

            // Debugging color ID
            //alert("Color ID for '" + labelCol + "': " + colorID);

            colorDescriptor.putEnumerated(s2t("color"), s2t("color"), colorID);

            // Apply the new color label to the layer
            descriptor.putObject(s2t("to"), s2t("layer"), colorDescriptor);

            executeAction(s2t("set"), descriptor, DialogModes.NO);
            //alert("Successfully set layer color to: " + labelCol);
        } catch (e) {
            //alert("Error applying color: " + labelCol + ", " + e.message);
        }
    }

}

 

Translate
Bojan Živković11378569
Community Expert
Community Expert
Aug 22, 2024 4:59 AM UTC

It appears the only method is to record the action for setting the color label on the current layer, then assign a keyboard shortcut to execute that action.

Translate
Stephen Marsh
Community Expert
Community Expert
Aug 22, 2024 5:36 AM UTC
quote

It appears the only method is to record the action for setting the color label on the current layer, then assign a keyboard shortcut to execute that action.


By @Bojan Živković11378569

 

Same for scripting, which allows keyboard shortcuts that are not F-Key based.

Translate
Legend
Aug 22, 2024 2:01 PM UTC
async function actionCommands() {
    let command;
    let result;
    let psAction = require("photoshop").action;

    // Set current layer
    command = {"_obj":"set","_target":[{"_enum":"ordinal","_ref":"layer","_value":"targetEnum"}],"to":{"_obj":"layer","color":{"_enum":"color","_value":"blue"}}};
    result = await psAction.batchPlay([command], {});
}

async function runModalFunction() {
    await require("photoshop").core.executeAsModal(actionCommands, {"commandName": "Action Commands"});
}

await runModalFunction();
Translate