Answered
Get Photoshop text color
hello
Using scripting, it's possible to get the current Photoshop text color and add it to the swatch color menu?
thanks!
hello
Using scripting, it's possible to get the current Photoshop text color and add it to the swatch color menu?
thanks!
Oh, it works!
I have to learn more Javascript.
Thank you so much~! 🙂
See how this works for you...
/*
Text Layer Color to Swatch.jsx
v1.0 - 10th March 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/get-photoshop-text-color/td-p/14475583
*/
#target photoshop
// Store the current foreground colour
var savedForegroundColor = app.foregroundColor;
/*
Based on code from the late Michael Hale
https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-get-color-info-of-text-in-text-layer/m-p/3342131
*/
var ref = new ActionReference();
ref.putEnumerated(stringIDToTypeID("layer"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var desc = executeActionGet(ref);
var list = desc.getObjectValue(charIDToTypeID("Txt "));
var tsr = list.getList(charIDToTypeID("Txtt"));
var info = [];
for (var i = 0; i < tsr.count; i++) {
var tsr0 = tsr.getObjectValue(i);
var textStyle = tsr0.getObjectValue(charIDToTypeID("TxtS"));
var color = textStyle.getObjectValue(charIDToTypeID('Clr '));
var textColor = new SolidColor();
textColor.rgb.red = color.getDouble(charIDToTypeID('Rd '));
textColor.rgb.green = color.getDouble(charIDToTypeID('Grn '));
textColor.rgb.blue = color.getDouble(charIDToTypeID('Bl '));
}
// Set the foreground colour
var solidColorValue = new SolidColor();
solidColorValue.rgb.red = Math.round(textColor.rgb.red);
solidColorValue.rgb.green = Math.round(textColor.rgb.green);
solidColorValue.rgb.blue = Math.round(textColor.rgb.blue);
app.foregroundColor = solidColorValue;
// Set a new swatch
var c2t = function (s) {
return app.charIDToTypeID(s);
};
var s2t = function (s) {
return app.stringIDToTypeID(s);
};
var descriptor = new ActionDescriptor();
var descriptor2 = new ActionDescriptor();
var descriptor3 = new ActionDescriptor();
var reference = new ActionReference();
reference.putClass(s2t("colors"));
descriptor.putReference(s2t("null"), reference);
descriptor2.putString(s2t("name"), ~~textColor.rgb.red + "R " + ~~textColor.rgb.green + "G " + ~~textColor.rgb.blue + "B"); // Swatch name
descriptor2.putString(s2t("ID"), uuid()); // Create a faux random ID
descriptor2.putString(c2t("CCid"), "");
descriptor3.putDouble(s2t("red"), textColor.rgb.red);
descriptor3.putDouble(s2t("grain"), textColor.rgb.green);
descriptor3.putDouble(s2t("blue"), textColor.rgb.blue);
descriptor2.putObject(s2t("color"), s2t("RGBColor"), descriptor3);
descriptor.putObject(s2t("using"), s2t("colors"), descriptor2);
//descriptor.putBoolean(s2t("pushToDesignLibraries"), true); // Add to CC library
executeAction(s2t("make"), descriptor, DialogModes.ALL);
// Restore the original foreground colour
app.foregroundColor = savedForegroundColor;
function uuid() {
/* https://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid */
var chars = '0123456789abcdef'.split('');
var uuid = [],
rnd = Math.random,
r;
uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
uuid[14] = '4'; // version 4
for (var i = 0; i < 36; i++) {
if (!uuid[i]) {
r = 0 | rnd() * 16;
uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r & 0xf];
}
}
return uuid.join('');
}
Already have an account? Login
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.