Thank you so much. I will try it. I am grateful to you for this post link.
By @Suraiya A
You're welcome!
Here is a custom script for you, no adjustment layer is necessary, the grayscale conversion will use the red channel. Updated to a 1.1 version using smart objects/smart filters.
Press "Yes" to process ALL layers or "No" to process only the selected/active layers.
Note: Locked layers are skipped, although that is easily changed if you wish for the script to automatically unlock them.
/*
Layers to Gray Based on Red Channel.jsx
Stephen Marsh
v1.0 - 27th February 2025
v1.1 - Adds a smart object / smart filter for lossless conversion of colour to gray
https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-can-i-convert-color-into-this-format/td-p/15179655
Note: Locked layers are skipped
*/
#target photoshop
// Ensure a document is open
if (app.documents.length > 0) {
if (app.activeDocument.mode == DocumentMode.RGB) {
// Single history stage undo
activeDocument.suspendHistory("Undo Script", "main()");
function main() {
// Layer selection logic
if (confirm("Process all layers (Yes), or only process the selected layers (No)?", false)) {
selectAllLayers();
}
// Capture the initial layer visibility and layer selection
var currentLayersState = getLayersVisiblity();
// Get the selected layers: courtesy of jazz-y
var s2t = stringIDToTypeID;
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var lrs = executeActionGet(r).getList(p),
sel = new ActionReference();
// Loop over the selected layers: courtesy of jazz-y
for (var l = 0; l < lrs.count; l++) {
sel.putIdentifier(s2t('layer'), p = lrs.getReference(l).getIdentifier(s2t('layerID')));
(r = new ActionReference()).putIdentifier(s2t('layer'), p);
(d = new ActionDescriptor()).putReference(s2t("target"), r);
//d.putBoolean(s2t("makeVisible"), false);
executeAction(s2t('select'), d, DialogModes.NO);
// Check if the layer is locked
if (isLayerLocked()) {
//app.activeDocument.activeLayer.allLocked = false; // Unlock the layer
continue; // Skip processing this layer if it is locked
}
// Create a new embedded smart object from the selected layers
executeAction(stringIDToTypeID("newPlacedLayer"), undefined, DialogModes.NO);
// Process the active layer
channelMixer();
}
// Restore the initial layer visibility and selection
setLayersVisiblity(currentLayersState);
}
} else {
alert("The document must be RGB mode!");
}
} else {
alert("A document must be open!");
}
///// Functions /////
function selectAllLayers(ignoreBackground) {
// Should be pretty snappy since it uses the native 'Select > All Layers' method and then adds background layer if it exists.
// selectAllLayers(true); // Ignores background layer
// Select all layers (doesn't include Background)
try {
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
desc.putReference(charIDToTypeID('null'), ref);
executeAction(stringIDToTypeID('selectAllLayers'), desc, DialogModes.NO);
} catch (e) { }
if (!ignoreBackground) {
// Add Background Layer to the selection (if it exists)
try {
activeDocument.backgroundLayer;
var bgID = activeDocument.backgroundLayer.id;
var ref = new ActionReference();
var desc = new ActionDescriptor();
ref.putIdentifier(charIDToTypeID('Lyr '), bgID);
desc.putReference(charIDToTypeID('null'), ref);
desc.putEnumerated(stringIDToTypeID('selectionModifier'), stringIDToTypeID('selectionModifierType'), stringIDToTypeID('addToSelection'));
desc.putBoolean(charIDToTypeID('MkVs'), false);
executeAction(charIDToTypeID('slct'), desc, DialogModes.NO);
} catch (e) { }
}
}
function channelMixer() {
var s2t = function (s) {
return app.stringIDToTypeID(s);
};
var descriptor = new ActionDescriptor();
var descriptor2 = new ActionDescriptor();
descriptor.putEnumerated(s2t("presetKind"), s2t("presetKindType"), s2t("presetKindCustom"));
descriptor.putBoolean(s2t("monochromatic"), true);
descriptor2.putUnitDouble(s2t("red"), s2t("percentUnit"), 100);
descriptor.putObject(s2t("gray"), s2t("channelMatrix"), descriptor2);
executeAction(s2t("channelMixer"), descriptor, DialogModes.NO);
}
function getLayersVisiblity() {
// by jazz-y
var s2t = stringIDToTypeID,
t2s = typeIDToStringID;
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var targetLayers = executeActionGet(r).getList(p),
seletion = [],
visiblity = {};
for (var i = 0; i < targetLayers.count; i++) seletion.push(targetLayers.getReference(i).getIdentifier());
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('numberOfLayers'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var len = executeActionGet(r).getInteger(p);
for (var j = 1; j <= len; j++) {
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('layerSection'));
r.putIndex(s2t('layer'), j);
if (t2s(executeActionGet(r).getEnumerationValue(p)) == 'layerSectionEnd') continue;
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('layerID'));
r.putIndex(s2t('layer'), j);
var id = executeActionGet(r).getInteger(p);
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('visible'));
r.putIndex(s2t('layer'), j);
var visible = executeActionGet(r).getBoolean(p);
visiblity[id] = visible;
}
return {
selection: seletion,
visiblity: visiblity
};
}
function setLayersVisiblity(layersStateObject) {
// by jazz-y
var s2t = stringIDToTypeID;
for (var a in layersStateObject.visiblity) {
makeVisible = layersStateObject.visiblity[a] ? "show" : "hide";
(r = new ActionReference()).putIdentifier(s2t('layer'), a);
(d = new ActionDescriptor()).putReference(s2t('target'), r);
executeAction(s2t(makeVisible), d, DialogModes.NO);
}
if (layersStateObject.selection.length) {
var r = new ActionReference();
for (var i = 0; i < layersStateObject.selection.length; i++)
r.putIdentifier(s2t("layer"), layersStateObject.selection[i]);
(d = new ActionDescriptor()).putReference(s2t("target"), r);
d.putBoolean(s2t("makeVisible"), false);
executeAction(s2t("select"), d, DialogModes.NO);
} else {
(r = new ActionReference()).putEnumerated(s2t("layer"), s2t('ordinal'), s2t('targetEnum'));
(d = new ActionDescriptor()).putReference(s2t('target'), r);
executeAction(s2t('selectNoLayers'), d, DialogModes.NO);
}
}
function isLayerLocked() {
var s2t = stringIDToTypeID;
var ref = new ActionReference();
ref.putProperty(s2t('property'), s2t('layerLocking'));
ref.putEnumerated(s2t('layer'), s2t('ordinal'), s2t('targetEnum'));
var desc = executeActionGet(ref).getObjectValue(s2t('layerLocking'));
return desc.getBoolean(s2t('protectAll')) || desc.getBoolean(s2t('protectComposite'));
}
Copy the code text to the clipboard
Open a new blank file in a plain-text editor (not in a word processor)
Paste the code in
Save as a plain text format file – .txt
Rename the saved file extension from .txt to .jsx
Install or browse to the .jsx file to run (see below)
https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html
... View more