nirmalya123
Explorer
nirmalya123
Explorer
Activity
‎Sep 13, 2023
05:12 AM
2 Upvotes
Hi @Sheena Kaul I have downloaded the file from http://download.macromedia.com/pub/labs/extensionbuilder3/extensionbuilder_p3_080113.zip I am following the example given in this link Starting Photoshop HTML5 Extension development | by Dominique Briggs | Medium But I am getting the following error when I am trying to create a new Adobe Extension Project. An error has occurred. See error log for more details. org/eclipse/core/databinding/beans/BeanProperties I want to create a HTLM5 extension project for Photoshop. Do you have any tutorial for setting the environment in eclipse? Many thanks in advance. Nirmalya
... View more
‎Sep 11, 2023
11:11 PM
1 Upvote
Hi @c.pfaffenbichler Thanks a lot for your generous help, much thankful! My program logic is that I need to check the colors of each layer, and if they are not white or grey or black i.e. if the red, green and blue values are not the same, then change their colors. Hence I need to check the colors of all the layers in a loop. I have check many sample codes posted in this forum with the following lines - var apl = new AM('application'),
doc = new AM('document'),
lr = new AM('layer'), For example, this one - Solved: Re: Script that updates (Copy/Paste) masks on mul... - Adobe Support Community - 12313689 So I was thinking instead of duplicating each layer as a new document, if we can directly process each layer without creating a new document, the code would run much faster. So our processEachLayer() function should be changed. But anyway, since this code is working fine, I finally gave up after trying for three days! Thanks again for your time and eagerness to solve this issue. I am really really thankful! Have a nice day. Nirmalya
... View more
‎Sep 07, 2023
04:52 AM
Hi I have now corrected the code, it's now processing each layer and correctly determining the layer color. But as you can see, for each layer I am duplicating the layer into a new document and processing the same logic that is applicable for a whole document. So this is not a very good piece of code! Can we do it in a nicer way? const DE_THRESHOLD = 15, RESIZE_TO = 35;
for (var x=0; x<app.activeDocument.layerSets.length; x++) {
var layerSet = app.activeDocument.layerSets[x];
if (layerSet.name=="Images" || layerSet.name=="Images_1" || layerSet.name=="Images_2") {
for (var layerIndex = 0; layerIndex < layerSet.layers.length; layerIndex++) {
var layer = layerSet.layers[layerIndex];
app.activeDocument.activeLayer = layer;
var bounds = layer.bounds;
var width = bounds[2].value - bounds[0].value;
var height = bounds[3].value - bounds[1].value;
app.documents.add( width, height, 300, "temp",NewDocumentMode.RGB, DocumentFill.WHITE,1.0, BitsPerChannelType.EIGHT, "Adobe RGB (1998)" );
app.activeDocument = app.documents[0];
var idslct = charIDToTypeID( "slct" );
var desc219 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref1 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
ref1.putName( idLyr, layer.name);
desc219.putReference( idnull, ref1 );
var idMkVs = charIDToTypeID( "MkVs" );
desc219.putBoolean( idMkVs, false );
var idLyrI = charIDToTypeID( "LyrI" );
var list4 = new ActionList();
list4.putInteger( 13 );
desc219.putList( idLyrI, list4 );
executeAction( idslct, desc219, DialogModes.NO );
// =======================================================
var idDplc = charIDToTypeID("Dplc");
var desc585 = new ActionDescriptor();
var idnull = charIDToTypeID("null");
var ref123 = new ActionReference();
var idLyr = charIDToTypeID("Lyr ");
var idOrdn = charIDToTypeID("Ordn");
var idTrgt = charIDToTypeID("Trgt");
ref123.putEnumerated(idLyr, idOrdn, idTrgt);
desc585.putReference(idnull, ref123);
var idT = charIDToTypeID("T ");
var ref124 = new ActionReference();
var idDcmn = charIDToTypeID("Dcmn");
ref124.putName(idDcmn, "temp");
desc585.putReference(idT, ref124);
var idNm = charIDToTypeID("Nm ");
desc585.putString(idNm, layer.name);
var idVrsn = charIDToTypeID("Vrsn");
desc585.putInteger(idVrsn, 5);
var idIdnt = charIDToTypeID("Idnt");
var list62 = new ActionList();
list62.putInteger(112);
list62.putInteger(113);
list62.putInteger(114);
list62.putInteger(115);
desc585.putList(idIdnt, list62);
executeAction(idDplc, desc585, DialogModes.NO);
app.activeDocument = app.documents[1];
var doc = app.activeDocument;
for (var x = 0; x < app.activeDocument.layers.length; x++) {
var layer = app.activeDocument.layers[x];
if (layer.isBackgroundLayer) {
layer.remove();
}
}
processEachLayer();
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
}
}
function processEachLayer () {
var apl = new AM('application'),
doc = new AM('document');
// doc=app.activeDocument;
if (apl.getProperty('numberOfDocuments')) {
var docRes = doc.getProperty('resolution'),
docW = doc.getProperty('width') * docRes / 72,
docH = doc.getProperty('height') * docRes / 72;
doc.duplicate();
doc.resize(docW > docH ? 'width' : 'height', RESIZE_TO);
var f = new File(Folder.temp + '/colors.raw');
// doc.flatten();
doc.saveToRAW(f);
doc.close('no');
var colors = findColors(f);
// alert (colors);
// alert (colors[0]);
// alert (colors[1]);
// alert (colors[2]);
// f.remove();
for (var i = 0; i < colors.length; i++) {
colors[i][3] = 0;
for (var x = 0; x < colors.length; x++) {
if (x != i && dE(colors[i], colors[x]) <= DE_THRESHOLD) colors[i][3]++
}
}
var result = 0,
idx = null;
for (var i = 0; i < colors.length; i++) {
if (colors[i][3] >= result) { result = colors[i][3]; idx = i; }
}
// alert (result);
var c = new SolidColor;
c.rgb.red = colors[idx][0],
c.rgb.green = colors[idx][1],
c.rgb.blue = colors[idx][2];
foregroundColor = c;
// alert (c);
}
function findColors(f) {
var content = '';
if (f.exists) {
f.open('r');
f.encoding = "BINARY";
content = f.read();
f.close();
f.remove();
return colors = function (s) {
var m = [],
offset = 0;
do {
var c = [];
for (i = 0; i < 3; i++) {
var k = s.charCodeAt(offset + i);
c.push(k)
};
m.push(c)
offset += 3;
} while (offset < s.length)
return m;
}(content);
}
}
function dE(a, b) {
return Math.sqrt(Math.pow(a[0] - b[0], 2) + Math.pow(a[1] - b[1], 2) + Math.pow(a[2] - b[2], 2));
}
function AM(target) {
var s2t = stringIDToTypeID,
t2s = typeIDToStringID,
c2t = charIDToTypeID;
target = target ? s2t(target) : null;
this.getProperty = function (property, id, idxMode) {
property = s2t(property);
(r = new ActionReference()).putProperty(s2t('property'), property);
id != undefined ? (idxMode ? r.putIndex(target, id) : r.putIdentifier(target, id)) :
r.putEnumerated(target, s2t('ordinal'), s2t('targetEnum'));
return getDescValue(executeActionGet(r), property)
}
this.hasProperty = function (property, id, idxMode) {
property = s2t(property);
(r = new ActionReference()).putProperty(s2t('property'), property);
id ? (idxMode ? r.putIndex(target, id) : r.putIdentifier(target, id))
: r.putEnumerated(target, s2t('ordinal'), s2t('targetEnum'));
return executeActionGet(r).hasKey(property)
}
this.resize = function (dimension, value) {
(d = new ActionDescriptor()).putUnitDouble(s2t(dimension), s2t("pixelsUnit"), value);
d.putBoolean(s2t("constrainProportions"), true);
d.putEnumerated(c2t("Intr"), s2t("interpolationType"), s2t("automaticInterpolation"));
executeAction(s2t("imageSize"), d, DialogModes.NO);
}
this.flatten = function () {
executeAction(s2t("flattenImage"), new ActionDescriptor(), DialogModes.NO);
}
this.saveToRAW = function (f) {
(d = new ActionDescriptor()).putBoolean(s2t('copy'), true);
(d1 = new ActionDescriptor()).putObject(s2t("as"), s2t("rawFormat"), d);
d1.putPath(s2t("in"), f);
executeAction(s2t("save"), d1, DialogModes.NO);
}
this.duplicate = function () {
(r = new ActionReference()).putEnumerated(target, s2t("ordinal"), s2t("targetEnum"));
(d = new ActionDescriptor()).putReference(s2t("null"), r);
executeAction(s2t("duplicate"), d, DialogModes.NO);
}
this.close = function (yesNo) {
(d = new ActionDescriptor()).putEnumerated(s2t("saving"), s2t("yesNo"), s2t(yesNo));
executeAction(s2t("close"), d, DialogModes.NO);
}
function getDescValue(d, p) {
switch (d.getType(p)) {
case DescValueType.OBJECTTYPE: return { type: t2s(d.getObjectType(p)), value: d.getObjectValue(p) };
case DescValueType.LISTTYPE: return d.getList(p);
case DescValueType.REFERENCETYPE: return d.getReference(p);
case DescValueType.BOOLEANTYPE: return d.getBoolean(p);
case DescValueType.STRINGTYPE: return d.getString(p);
case DescValueType.INTEGERTYPE: return d.getInteger(p);
case DescValueType.LARGEINTEGERTYPE: return d.getLargeInteger(p);
case DescValueType.DOUBLETYPE: return d.getDouble(p);
case DescValueType.ALIASTYPE: return d.getPath(p);
case DescValueType.CLASSTYPE: return d.getClass(p);
case DescValueType.UNITDOUBLE: return (d.getUnitDoubleValue(p));
case DescValueType.ENUMERATEDTYPE: return { type: t2s(d.getEnumerationType(p)), value: t2s(d.getEnumerationValue(p)) };
default: break;
};
}
}
} Many thanks in advance...
... View more
‎Sep 06, 2023
09:47 AM
Dear experts I have attached a PSD herewith. I want to iterate through the layers and find the colors of them. The layers can be either SOLID FILL layers or NORMAL layers which are rasterized from the COLOR FILL layers. So they have only one color, that is a known factor. Moreover the layers can be like a border, the inside part can be transparent, like layer 4. Getting the colors from the SOLID FILL layers is very easy, but for NORMAL layers we need to check the average color or the most prominent color. We have the code for picking up the most prominent color from a document from my earlier post - https://community.adobe.com/t5/photoshop-ecosystem-discussions/adobe-script-selecting-the-most-prominent-color-from-a-psd-layer/m-p/14056020#M751617 But the above mentioned code is for picking up the most prominent color from the whole document, and not from a layer. Hence I am not able to use this code for my purpose. I have spent a lot of time to modify the above code for my purpose, it is now looping through all the layers, but it's a nasty code; it needs a lot of modification. But since it's AM code, it's beyound my knowledge 🙂 Also it's not working properly. For the 4th layer, it is showing that the most prominent color is the white, because it's flattening the layers. So I need to delete the background layer. Then it would give me the most prominent color. I tried to delete the background layers, but I am not able to. How can I delete the background layer from the temp file colors.raw??? My code - const DE_THRESHOLD = 15, RESIZE_TO = 35;
for (var x=0; x<app.activeDocument.layerSets.length; x++) {
var layerSet = app.activeDocument.layerSets[x];
if (layerSet.name=="Images" || layerSet.name=="Images_1" || layerSet.name=="Images_2") {
for (var layerIndex = 0; layerIndex < layerSet.layers.length; layerIndex++) {
var layer = layerSet.layers[layerIndex];
app.activeDocument.activeLayer = layer;
var bounds = layer.bounds;
var width = bounds[2].value - bounds[0].value;
var height = bounds[3].value - bounds[1].value;
app.documents.add( width, height, 300, "temp",NewDocumentMode.RGB, DocumentFill.WHITE,1.0, BitsPerChannelType.EIGHT, "Adobe RGB (1998)" );
app.activeDocument = app.documents[0];
var idslct = charIDToTypeID( "slct" );
var desc219 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref1 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
ref1.putName( idLyr, layer.name);
desc219.putReference( idnull, ref1 );
var idMkVs = charIDToTypeID( "MkVs" );
desc219.putBoolean( idMkVs, false );
var idLyrI = charIDToTypeID( "LyrI" );
var list4 = new ActionList();
list4.putInteger( 13 );
desc219.putList( idLyrI, list4 );
executeAction( idslct, desc219, DialogModes.NO );
// =======================================================
var idDplc = charIDToTypeID("Dplc");
var desc585 = new ActionDescriptor();
var idnull = charIDToTypeID("null");
var ref123 = new ActionReference();
var idLyr = charIDToTypeID("Lyr ");
var idOrdn = charIDToTypeID("Ordn");
var idTrgt = charIDToTypeID("Trgt");
ref123.putEnumerated(idLyr, idOrdn, idTrgt);
desc585.putReference(idnull, ref123);
var idT = charIDToTypeID("T ");
var ref124 = new ActionReference();
var idDcmn = charIDToTypeID("Dcmn");
ref124.putName(idDcmn, "temp");
desc585.putReference(idT, ref124);
var idNm = charIDToTypeID("Nm ");
desc585.putString(idNm, layer.name);
var idVrsn = charIDToTypeID("Vrsn");
desc585.putInteger(idVrsn, 5);
var idIdnt = charIDToTypeID("Idnt");
var list62 = new ActionList();
list62.putInteger(112);
list62.putInteger(113);
list62.putInteger(114);
list62.putInteger(115);
desc585.putList(idIdnt, list62);
executeAction(idDplc, desc585, DialogModes.NO);
app.activeDocument = app.documents[1];
// var doc = app.activeDocument;
// for (var x = 0; x < app.activeDocument.layers.length; x++) {
// if (app.activeDocument.activeLayer.isBackgroundLayer) {
// app.activeDocument.activeLayer.remove();
// x=x-1;
// }
// }
var apl = new AM('application'),
doc = new AM('document');
// doc=app.activeDocument;
for (var x = 0; x < app.activeDocument.layers.length; x++) {
if (app.activeDocument.activeLayer.isBackgroundLayer) {
app.activeDocument.activeLayer.remove();
}
}
if (apl.getProperty('numberOfDocuments')) {
var docRes = doc.getProperty('resolution'),
docW = doc.getProperty('width') * docRes / 72,
docH = doc.getProperty('height') * docRes / 72;
doc.duplicate();
doc.resize(docW > docH ? 'width' : 'height', RESIZE_TO);
var f = new File(Folder.temp + '/colors.raw');
// doc.flatten();
doc.saveToRAW(f);
doc.close('no');
// for (var x = 0; x < app.activeDocument.layers.length; x++) {
// if (app.activeDocument.activeLayer.isBackgroundLayer) {
// app.activeDocument.activeLayer.remove();
// x=x-1;
// }
// }
var colors = findColors(f);
// alert (colors);
// alert (colors[0]);
// alert (colors[1]);
// alert (colors[2]);
// f.remove();
for (var i = 0; i < colors.length; i++) {
colors[i][3] = 0;
for (var x = 0; x < colors.length; x++) {
if (x != i && dE(colors[i], colors[x]) <= DE_THRESHOLD) colors[i][3]++
}
}
var result = 0,
idx = null;
for (var i = 0; i < colors.length; i++) {
if (colors[i][3] >= result) { result = colors[i][3]; idx = i; }
}
// alert (result);
var c = new SolidColor;
c.rgb.red = colors[idx][0],
c.rgb.green = colors[idx][1],
c.rgb.blue = colors[idx][2];
foregroundColor = c;
// alert (c);
}
function findColors(f) {
var content = '';
if (f.exists) {
f.open('r');
f.encoding = "BINARY";
content = f.read();
f.close();
f.remove();
return colors = function (s) {
var m = [],
offset = 0;
do {
var c = [];
for (i = 0; i < 3; i++) {
var k = s.charCodeAt(offset + i);
c.push(k)
};
m.push(c)
offset += 3;
} while (offset < s.length)
return m;
}(content);
}
}
function dE(a, b) {
return Math.sqrt(Math.pow(a[0] - b[0], 2) + Math.pow(a[1] - b[1], 2) + Math.pow(a[2] - b[2], 2));
}
function AM(target) {
var s2t = stringIDToTypeID,
t2s = typeIDToStringID,
c2t = charIDToTypeID;
target = target ? s2t(target) : null;
this.getProperty = function (property, id, idxMode) {
property = s2t(property);
(r = new ActionReference()).putProperty(s2t('property'), property);
id != undefined ? (idxMode ? r.putIndex(target, id) : r.putIdentifier(target, id)) :
r.putEnumerated(target, s2t('ordinal'), s2t('targetEnum'));
return getDescValue(executeActionGet(r), property)
}
this.hasProperty = function (property, id, idxMode) {
property = s2t(property);
(r = new ActionReference()).putProperty(s2t('property'), property);
id ? (idxMode ? r.putIndex(target, id) : r.putIdentifier(target, id))
: r.putEnumerated(target, s2t('ordinal'), s2t('targetEnum'));
return executeActionGet(r).hasKey(property)
}
this.resize = function (dimension, value) {
(d = new ActionDescriptor()).putUnitDouble(s2t(dimension), s2t("pixelsUnit"), value);
d.putBoolean(s2t("constrainProportions"), true);
d.putEnumerated(c2t("Intr"), s2t("interpolationType"), s2t("automaticInterpolation"));
executeAction(s2t("imageSize"), d, DialogModes.NO);
}
this.flatten = function () {
executeAction(s2t("flattenImage"), new ActionDescriptor(), DialogModes.NO);
}
this.saveToRAW = function (f) {
(d = new ActionDescriptor()).putBoolean(s2t('copy'), true);
(d1 = new ActionDescriptor()).putObject(s2t("as"), s2t("rawFormat"), d);
d1.putPath(s2t("in"), f);
executeAction(s2t("save"), d1, DialogModes.NO);
}
this.duplicate = function () {
(r = new ActionReference()).putEnumerated(target, s2t("ordinal"), s2t("targetEnum"));
(d = new ActionDescriptor()).putReference(s2t("null"), r);
executeAction(s2t("duplicate"), d, DialogModes.NO);
}
this.close = function (yesNo) {
(d = new ActionDescriptor()).putEnumerated(s2t("saving"), s2t("yesNo"), s2t(yesNo));
executeAction(s2t("close"), d, DialogModes.NO);
}
function getDescValue(d, p) {
switch (d.getType(p)) {
case DescValueType.OBJECTTYPE: return { type: t2s(d.getObjectType(p)), value: d.getObjectValue(p) };
case DescValueType.LISTTYPE: return d.getList(p);
case DescValueType.REFERENCETYPE: return d.getReference(p);
case DescValueType.BOOLEANTYPE: return d.getBoolean(p);
case DescValueType.STRINGTYPE: return d.getString(p);
case DescValueType.INTEGERTYPE: return d.getInteger(p);
case DescValueType.LARGEINTEGERTYPE: return d.getLargeInteger(p);
case DescValueType.DOUBLETYPE: return d.getDouble(p);
case DescValueType.ALIASTYPE: return d.getPath(p);
case DescValueType.CLASSTYPE: return d.getClass(p);
case DescValueType.UNITDOUBLE: return (d.getUnitDoubleValue(p));
case DescValueType.ENUMERATEDTYPE: return { type: t2s(d.getEnumerationType(p)), value: t2s(d.getEnumerationValue(p)) };
default: break;
};
}
}
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
}
}
So can you please modify this code so that it can currectly pickup the colors in all the cases? Thanks in advance...
... View more
‎Sep 06, 2023
02:14 AM
Hi @c.pfaffenbichler You are correct 🙂 But I have asked the original question for a basic example and I need to enhance this for the complete solution, which is much complex! So I need to understand the prominent colors of each individual layers and act accordingly. So I woun't set the foreground color with those colors, but actually I will set them in different variables. Hope this clarifies the point 🙂 Thanks again
... View more
‎Sep 06, 2023
01:53 AM
Yes, but that is for the whole document as a whole. If there are many layers, it's flattening all of them and then calculating the most prominent color. How I can do it for each individual layer? I am close to it, but trying for the last two days! That's my question.
... View more
‎Sep 06, 2023
12:53 AM
Hi @jazz-y Please do me a little favor. Do you have the same code for each layer of a psd file? Suppose, I have a layer set named "Group1" and it has many layers within it. I can easily loop though the layers, but how to invoke your code for each of them? Since this is action manager code, I am finding difficulties. I went through your other posts and many times you have used layer as the target, but yet then I am not able to modify your code properly. Can you please help me out? Thanks a ton in advance ...
... View more
‎Sep 02, 2023
10:16 PM
1 Upvote
Hi @c.pfaffenbichler and @jazz-y Both of your answers provided me the right way. Myself and the whole community is really indebted to your knowledge and contribution. Many thanks.
... View more
‎Sep 01, 2023
10:50 AM
Hi @c.pfaffenbichler Many thanks for your help. I followed one script and as you suggested I extracted the 5 colors from the picture and created seperate layers with the rgb values. Now the picture looks like this - Now how to check how many pixels are there for each rgb value? If we select any rgb layer by Select -> Load Selection, we can get the area. Like this - Can we measure this area? Then we can compare them and get the most used color. Thanks
... View more
‎Sep 01, 2023
06:46 AM
I understand that, but please check the following link - https://tympanus.net/Development/ColorExtraction/ or https://github.com/moneypenny/steamy-screenshots You can see they are doing the same thing. It's some sort of calculation to get the most used color pallete. Thanks
... View more
‎Sep 01, 2023
06:13 AM
Hi @c.pfaffenbichler It may not be the exact purple, but even something closer is fine. May be average value of the all purple hues. But I don't want the average valus of the entire picture for sure. In the present code, we are reading the histograms of all the channels. So basically we need to decode the histogram. What should be the logic? Thanks
... View more
‎Sep 01, 2023
05:35 AM
Adobe script: Selecting the most prominent color f... - Adobe Support Community - 14054526
... View more
‎Sep 01, 2023
05:35 AM
Dear experts Suppose I want to extract the most prominent color of an image or a photoshop layer, that is the color which is drawing the most attention, how is that possible? I checked the following example - Auto picking the most distinct accent color from a... - Adobe Support Community - 9875726 Here the code is picking up the average color of the image. Now this color is a mix of all the RGB components, but I want to get the most prominent color, not the average one. Please check the attached picture; though purple is the most prominent color, the code is picking up the brown tone as the average color. How can we enhance this? This is the code from the above mentioned link - var min_saturation = 50;
// create tmp layer and move it on top
app.activeDocument.artLayers.add();
if (app.activeDocument.activeLayer != app.activeDocument.layers[0])
app.activeDocument.activeLayer.move(app.activeDocument.layers[0], ElementPlacement.PLACEBEFORE);
// stamp visible to tmp layer
var d = new ActionDescriptor();
d.putBoolean( charIDToTypeID( "Dplc" ), true );
executeAction( charIDToTypeID( "MrgV" ), d, DialogModes.NO );
// call hsb/hsl filter (rgb -> hsb)
var d = new ActionDescriptor();
d.putEnumerated( charIDToTypeID( "Inpt" ), charIDToTypeID( "ClrS" ), charIDToTypeID( "RGBC" ) );
d.putEnumerated( charIDToTypeID( "Otpt" ), charIDToTypeID( "ClrS" ), charIDToTypeID( "HSBl" ) );
executeAction( charIDToTypeID( "HsbP" ), d, DialogModes.NO );
// select "green" channel
app.activeDocument.activeChannels = [ app.activeDocument.channels[1] ];
// threshold 128 ==> saturation 50% or higher
var d = new ActionDescriptor();
d.putInteger( charIDToTypeID( "Lvl " ), min_saturation * 255/100 );
executeAction( charIDToTypeID( "Thrs" ), d, DialogModes.NO );
// selection from current channel
var r1 = new ActionReference();
r1.putProperty( charIDToTypeID( "Chnl" ), charIDToTypeID( "fsel" ) );
var d = new ActionDescriptor();
d.putReference( charIDToTypeID( "null" ), r1 );
var r2 = new ActionReference();
r2.putEnumerated( charIDToTypeID( "Chnl" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
d.putReference( charIDToTypeID( "T " ), r2 );
executeAction( charIDToTypeID( "setd" ), d, DialogModes.NO );
// remove tmp layer
app.activeDocument.layers[0].remove();
// get average color for selection
var c = new SolidColor();
if (!get_average_color(c)) { alert("Something is wrong"); }
app.activeDocument.selection.deselect();
app.foregroundColor = c;
alert(c.rgb.red + " " + c.rgb.green + " " + c.rgb.blue);
///////////////////////////////////////////////////////////////////////////////////
function get_average_color(color)
{
try {
var chnl = 3;
if (app.activeDocument.mode == DocumentMode.CMYK) chnl = 4;
if (app.activeDocument.mode == DocumentMode.GRAYSCALE) chnl = 1;
var lm = new Array();
var px = new Array();
for (var i = 0; i < chnl; i++)
{
var hst = get_histogram(i+1);
var l = 0;
var p = 0;
if (!hst) { return false; }
for (var n in hst) { l += hst[n] * n; p += hst[n]; }
hst = null;
lm.push(l);
px.push(p);
}
if (app.activeDocument.mode == DocumentMode.RGB)
{
var r =lm[0]/px[0];
var g =lm[1]/px[1];
var b =lm[2]/px[2];
with (color.rgb) { red = Math.round(r); green = Math.round(g); blue = Math.round(b); };
}
else if (app.activeDocument.mode == DocumentMode.LAB)
{
var _l = lm[0]/px[0] * 100/255;
var _a = lm[1]/px[1] - 128;
var _b = lm[2]/px[2] - 128;
with (color.lab) { l = Math.round(_l); a = Math.round(_a); b = Math.round(_b); };
}
else if (app.activeDocument.mode == DocumentMode.CMYK)
{
var c = 100 - lm[0]/px[0] * 100/255;
var m = 100 - lm[1]/px[1] * 100/255;
var y = 100 - lm[2]/px[2] * 100/255;
var k = 100 - lm[3]/px[3] * 100/255;
with (color.cmyk) { cyan = Math.round(c); magenta = Math.round(m); yellow = Math.round(y); black = Math.round(k); };
}
else if (app.activeDocument.mode == DocumentMode.GRAYSCALE)
{
var _l = 100 - lm[0]/px[0] * 100/255;
with (color.gray) { gray = Math.round(_l); };
}
else { alert("Unsupported color mode", "Error", true); return false; }
return true;
}
catch (e) { alert(e); }
}
////////////////////////////////////////////////////////////
function get_histogram(n)
{
try {
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("histogram"));
if (typeof(n) == "string")
r.putName(stringIDToTypeID("channel"), n);
else
{
if (app.activeDocument.mode == DocumentMode.RGB)
{
switch (n)
{
case -1: r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum")); break;
case 0: r.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("channel"), stringIDToTypeID("RGB" )); break;
case 1: r.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("channel"), stringIDToTypeID("red" )); break;
case 2: r.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("channel"), stringIDToTypeID("green")); break;
case 3: r.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("channel"), stringIDToTypeID("blue" )); break;
default: r.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum")); break;
}
}
else if (app.activeDocument.mode == DocumentMode.LAB)
{
switch (n)
{
case -1:
case 0: r.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("channel"), stringIDToTypeID("lab" )); break;
case 1: r.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("channel"), stringIDToTypeID("lightness")); break;
case 2: r.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("channel"), stringIDToTypeID("a" )); break;
case 3: r.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("channel"), stringIDToTypeID("b" )); break;
default: r.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum")); break;
}
}
else if (app.activeDocument.mode == DocumentMode.CMYK)
{
switch (n)
{
case -1: r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum")); break;
case 0: r.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("channel"), stringIDToTypeID("CMYK" )); break;
case 1: r.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("channel"), stringIDToTypeID("cyan" )); break;
case 2: r.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("channel"), stringIDToTypeID("magenta")); break;
case 3: r.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("channel"), stringIDToTypeID("yellow" )); break;
case 4: r.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("channel"), stringIDToTypeID("black" )); break;
default: r.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum")); break;
}
}
else if (app.activeDocument.mode == DocumentMode.GRAYSCALE)
{
switch (n)
{
case -1:
case 0: r.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("channel"), stringIDToTypeID("black")); break;
default: r.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum")); break;
}
}
else { alert("Unsupported color mode", "Error", true); return null; }
}
var ret;
try { ret = executeActionGet(r).getList(stringIDToTypeID("histogram")); } catch (e) { alert(e); return null; }
var hist = new Array();
for (var i = 0; i < 256; i++) hist.push(ret.getInteger(i));
// var max = Math.max.apply(null, hist);
// alert (avg(hist));
return hist;
}
catch (e) { alert(e); return null; }
}
function avg(arr) {
var sum = 0;
// Iterate the elements of the array
for (var i = 0; i < 256; i++) {
sum += arr[i];
}
// Returning the average of the numbers
return sum / arr.length;
}
... View more
‎Sep 01, 2023
04:34 AM
Hi I have created a new post altogether. Thanks
... View more
‎Sep 01, 2023
03:29 AM
Hi @PS-Guru Did you get the code for picking up the most vibrant color? I am trying to do the same based on the histogram analysis, but if you already got the code, please let me know. Thanks
... View more
‎Jul 17, 2023
12:48 AM
1 Upvote
Hi Friends I am thinking loudly, it's just an idea. I am thinking of developing an Adobe script which will detect the type of picture, for e.g. whether it's a bridal portrait, couple portrait, wedding ceremony or even a landscape picture. For that we need to integrate third party javascript libraries within Adobe script. For e.g. - https://www.geeksforgeeks.org/top-10-javascript-libraries-for-machine-learning-and-data-science/ Has anyone done that earlier? Or can it be done in the near future? Thanks
... View more
‎May 20, 2023
05:28 AM
1 Upvote
Hi @Stephen Marsh Yes, sorry for the mistake! Actually it was copy paste from another piece of code where I was accepting input from a folder dialog window. In fact the line should be - if (theFile.name.match(/\.(jpg|jpeg|tif|psd|png|bmp)$/i) || theFile.constructor.name == "File") { Thanks for testing in Mac and correcting my mistake.
... View more
‎May 19, 2023
07:47 PM
1 Upvote
Hi, first of many thanks to all the great minds in this community. They suggested me how to simulate this automatic open close feature with the help of scripts. I have attached the scripts herewith, I have also attached the screenshot of how to add the script in the Scripts Event Manager tab. SequentialOpenFiles.jsx - This is the main script with multi-file selection dialog. The user needs to select the files in the drop down list. All the image files are supported. This script will create a text file in the C:\Users\<username>\AppData\Roaming\Adobe\Adobe Photoshop XXXX\Adobe Photoshop XXXX Settings\ directory. That text file will contain the list of all the files selected in the above selection operation (except the very first file). This script will open the very first first file. Successive files will be opened by the event listener script. function createOpenDialog() {
try {
var fileList;
// DIALOG
// ======
var openDialog = new Window("dialog");
openDialog.text = "Sequential Image Open";
openDialog.orientation = "column";
openDialog.alignChildren = ["center","top"];
openDialog.spacing = 10;
openDialog.margins = 16;
// PANEL1
// ======
var panel1 = openDialog.add("panel", undefined, undefined, {name: "panel1"});
panel1.orientation = "column";
panel1.alignChildren = ["left","top"];
panel1.spacing = 10;
panel1.margins = 10;
var statictext1 = panel1.add("statictext", undefined, undefined, {name: "statictext1"});
statictext1.text = "Please select the images";
// GROUP1
// ======
var group1 = panel1.add("group", undefined, {name: "group1"});
group1.orientation = "row";
group1.alignChildren = ["left","center"];
group1.spacing = 10;
group1.margins = 0;
var edittext1 = group1.add('edittext {properties: {name: "edittext1"}}');
edittext1.helpTip = "Please select the image files";
edittext1.preferredSize.width = 500;
var button1 = group1.add("button", undefined, undefined, {name: "button1"});
button1.text = "Select ...";
button1.onClick = function() {
if ($.os.search(/windows/i) != -1) {
fileList = File.openDialog ("Please select the image files" , '*.jpg;*.jpeg;*.tif;*.psd;*.png;*.bmp', true);
}
else {
fileList = File.openDialog("Please select the image files", getFiles, true)
};
var fileNames=[];
for (var i=0;i<fileList.length;i++) {
fileNames[i]=fileList[i].fsName;
}
edittext1.text=fileNames;
}
function getFiles(theFile) {
if (theFile.name.match(/\.(txt|csv)$/i) || theFile.constructor.name == "Folder") {
return true
};
};
// PANEL2
// ======
var panel2 = openDialog.add("panel", undefined, undefined, {name: "panel2"});
panel2.orientation = "column";
panel2.alignChildren = ["left","top"];
panel2.spacing = 10;
panel2.margins = 10;
// GROUP2
// ======
var group2 = panel2.add("group", undefined, {name: "group2"});
group2.orientation = "row";
group2.alignChildren = ["left","center"];
group2.spacing = 10;
group2.margins = 0;
var button2 = group2.add("button", undefined, undefined, {name: "button2"});
button2.text = "Start";
button2.onClick = function () {
if (edittext1.text.length==0) {
alert("Please select the image files");
return;
}
openDialog.close();
createListFile(fileList);
}
var button3 = group2.add("button", undefined, undefined, {name: "button3"});
button3.text = "Cancel";
// openDialog.show();
openDialog.center();
return openDialog;
}
catch(e) {
alert(e + ': Line ' + e.line);
}
}
function showDialog(myDialog) {
return myDialog.show();
}
main()
function main() {
var openDialog = createOpenDialog();
showDialog(openDialog);
}
function createListFile(fileList) {
try {
if (fileList.length > 0) {
//Sort the fileList
fileList.sort(function (a, b) {
if (app.compareWithNumbers) {
return app.compareWithNumbers(a.name, b.name);
} else {
return sortAlphaNum(a.name, b.name);
}
});
var listFile = new File( app.preferencesFolder + "/" + "OpenClose_FileList.txt" );
listFile.open("a");
for (var index1 = 1; index1 < fileList.length; index1++) {
listFile.writeln(fileList[index1]);
}
listFile.close();
//Open the first file
open(fileList[0]);
}
}
catch(e) {
alert(e + ': Line ' + e.line);
}
} CloseEventListener.jsx - this file should be selected in the Scripts Event Manager tab for Close Document event. So whenever any document will be closed, this script will open the successive file from the above mentioned file list. I have emphasised any because even if a file is opened which is not in the list and then closed, the event will be triggered and the first file from the file list will be opened. After due editing, whenever the file will be closed, this script will delete the first line from the text file. And the operation will continue. var listFile = new File( app.preferencesFolder + "/" + "OpenClose_FileList.txt" );
listFile.open("r");
var contents = listFile.read();
listFile.close();
var rows = processInputFile(contents);
if (rows.length>0) {
var file = new File(rows[0]);
open(file);
//delete first line
listFile.open("w");
for (var i=1;i<rows.length;i++) {
listFile.writeln(rows[i]);
}
listFile.close();
}
function processInputFile(contents) {
try {
// Returns: Array [row][column]
var c = ""; // Character at index.
var index = 0;
var maxIndex = contents.length - 1;
var result = []; // Array of rows.
var row = []; // Array of columns.
var rowSum = 0; // Count of row contents.
var v = ""; // Column value.
while (index < contents.length) {
c = contents[index];
if (c == "\n" || c == "\r") {
// Reached end of line.
// Test for CRLF.
if (c == "\r" && index < maxIndex) {
if (contents[index + 1] == "\n") {
// Skip trailing newline.
index++;
}
}
// Column and row complete.
row.push(v);
rowSum += v.length;
if (rowSum) {
// Add row only when it has content.
result.push(row);
}
v = "";
row = [];
rowSum = 0;
}
else {
// Add character to column value.
v += c;
//alert(c);
}
if (index == maxIndex) {
// Reached end of data; flush.
row.push(v);
rowSum += v.length;
if (rowSum) {
// Add row only when it has content.
result.push(row);
}
break;
}
index++;
}
return result;
}
catch(e) {
alert(e + ': Line ' + e.line);
}
} This way the user has to register the event listener file in Photoshop Scripts Event Manager tab - At anytime if the user wants to break the operation, he has to simply uncheck the Enable ... checkbox. Hope this helps. Thanks Nirmalya
... View more
‎May 19, 2023
10:14 AM
1 Upvote
I have cracked the problem and written two scripts which will resove the issue. Am I allowed to upload the full solution code?
... View more
‎May 19, 2023
06:40 AM
1 Upvote
Thanks again.
... View more
‎May 19, 2023
05:36 AM
1 Upvote
Hi @c.pfaffenbichler Thanks a lot for your reply! But how to handle codes that would be deployed to many end users who would use many different versions of photoshop? Should I explicitly check the specific version of photoshop before file.save() and if it's 24.3, then code differently? Thanks again.
... View more
‎May 19, 2023
02:22 AM
Hi I am using Photoshop 23.0 and the folllowing code is working perfectly for me - var outputFolder = new Folder("E:/Test/");
var outputPath = outputFolder.fsName;
var selectedColorProfile = "Adobe RGB (1998)";
var outputPsdFile = new File(outputPath + "\\" + "test.psd");
var designPsd = app.documents.add( UnitValue(18,'IN'), UnitValue(12,'IN'), 300, "",NewDocumentMode.RGB, DocumentFill.WHITE,1.0, BitsPerChannelType.EIGHT, selectedColorProfile );
designPsd.saveAs(outputPsdFile);
//Many other edit operations
designPsd.save();
designPsd.close(); But my friend is using Photoshop 24.3.0 and he is getting the following error at designPsd.save() line - This is really strange! I changed the line to - app.activeDocument.save(); But getting the same error - Finally I changed the line to - app.activeDocument.close(SaveOptions.SAVECHANGES); This is really surprising! The very simple save() funtion is behaving differently in two version of Photoshop! Can you please explain what is going on and how to resolve it in the best possible way? Many thanks in advance...
... View more
‎May 15, 2023
06:53 PM
2 Upvotes
Hi experts I am writing a adobe photoshop script where I can select the folder name from the dialog window and the script will open the very first image from that folder. I will edit the picture and only when I save and close the image, the second image will be opened. And the same thing will continue. So the script will open only one image at a time. How this is possible? I was doing with many experiments and the current code is like this. var imageFolder = new Folder("C:/Nirmalya/Photo/");
function filter(file) {
return (file instanceof File && /\.(jpg)$/i.test(file.name))
}
var fileList = imageFolder.getFiles(filter);
if (fileList.length > 0) {
//Sort the fileList
fileList.sort(function (a, b) {
if (app.compareWithNumbers) {
return app.compareWithNumbers(a.name, b.name);
} else {
return sortAlphaNum(a.name, b.name);
}
});
loop:
for (var index1 = 0; index1 < fileList.length; index1++) {
if (documents.length<1) {
alert("1");
open(fileList[index1]);
}
else {
do {
// nothing
} while (documents.length==1);
alert("2");
continue loop;
}
}
} But photoshop is hanging in the do while block. It's not allowing me to do any operation on the open file. If I don't use this block, then while the first image is opened, the loop is actually iterating the entire loop, and after the first image is closed, no other image is opened. I know that there is an autoloaded utility. But I don't need that complex tool, I need to open only one image at a time. What could be relevant code from the autoloaded tool that will serve my purpose? Many thanks in advance ...
... View more
‎May 08, 2023
06:53 AM
This is the best code for measuring the layer angles! Many thanks!
... View more
‎Apr 04, 2023
10:44 PM
1 Upvote
Thank you so much @jazz-y - I was looking at javascript examples and wondering why is it not working! Thanks again.
... View more
‎Apr 04, 2023
10:29 PM
Hi experts I am writing a Photoshop script where I am planning to store some crucial information in a map. So that I don't need to access filesystem again and again. This is a simple javascript that should always work. Why is it not working within the Photoshop script? var myMap = {}
myMap["A"]="AAA";
myMap["B"]="BBB";
alert (myMap["A"]);
myMap.delete("A"); The myMap.delete line is giving the following error - Error 24: myMap.delete is not a function But the splice() function is working fine! Have you ever experienced this problem? Many thanks in advance.
... View more
‎Mar 28, 2023
03:21 AM
Hi experts I am wring a photoshop script and wondering whether javascript will be better or scriptlistener ? I read that javascript will be faster, but all the APIs are not compatible with all the versions of photoshop. On the otherhand, scriptlistener may be slower, but it's compatible with all the photoshop versions. Is it true? Can you please confirm? I know that many commands can't be executed by javascript; scriptlistener is the only option. But when both are working which one to choose? For example I am duplicating two layerSets to a different document and aligning it to right side. In Javascript - var openDocs = app.documents;
app.displayDialogs = DialogModes.NO;
var curDoc = openDocs[1];
var topLayerSet1 = curDoc.layerSets.getByName("TOP");
var bottomLayerSet1 = curDoc.layerSets.getByName("BOTTOM");
topLayerSet1.duplicate(openDocs[0], ElementPlacement.PLACEATBEGINNING);
bottomLayerSet1.duplicate(openDocs[0], ElementPlacement.PLACEATEND);
curDoc.close(SaveOptions.DONOTSAVECHANGES);
var doc = app.activeDocument;
var topLayerSet2 = doc.layerSets.getByName("TOP");
var bottomLayerSet2 = doc.layerSets.getByName("BOTTOM");
topLayerSet2.translate(5400, 0);
bottomLayerSet2.translate(5400, 0); In scriptlistener - //Right Align
var idAlgn = charIDToTypeID( "Algn" );
var desc317 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref27 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref27.putEnumerated( idLyr, idOrdn, idTrgt );
desc317.putReference( idnull, ref27 );
var idUsng = charIDToTypeID( "Usng" );
var idADSt = charIDToTypeID( "ADSt" );
var idAdRg = charIDToTypeID( "AdRg" );
desc317.putEnumerated( idUsng, idADSt, idAdRg );
var idalignToCanvas = stringIDToTypeID( "alignToCanvas" );
desc317.putBoolean( idalignToCanvas, false );
executeAction( idAlgn, desc317, DialogModes.NO ); Which one shoud I choose? Will eagerly wait for your valuable comments.
... View more
‎Mar 23, 2023
04:43 AM
1 Upvote
Thanks for the quick and simple solution 🙂 I was trying to add width variables, but not getting any changes! Thanks a ton!
... View more
‎Mar 23, 2023
04:19 AM
Thanks for your quick reply! Yes, that's purposeful. I thought the whole code will be cumborsome and only the relevant portion would be fine. This is the starting portion for the dialog part - // DIALOG
// ======
var dialog = new Window("dialog");
dialog.text = "Dialog";
dialog.orientation = "column";
dialog.alignChildren = ["center","top"];
dialog.spacing = 10;
dialog.margins = 16; I am skipping the code of the first panel which includes the Browse button. Thanks again
... View more
‎Mar 23, 2023
03:54 AM
Hi I am creating a tool with photoshop jsx script. I am creating a tabbedPanel with two tabs. Unfortunately I am not able to cover the full width of the tabbed panel with those two tab names. I have attched the picture for reference. (In the actual project the field names are all difeerent, but here I am just sharing a small code just to highlight this issue). As you can see there is an extra space after the second tab. And I don't want this, I want two tabs should occupy the whole width of the tabbedPanel. I am building this UI from https://scriptui.joonas.me/ Here is the code - // PANEL2
// ======
var panel2 = dialog.add("panel", undefined, undefined, {name: "panel2"});
panel2.orientation = "column";
panel2.alignChildren = ["left","top"];
panel2.spacing = 10;
panel2.margins = 10;
// TPANEL1
// =======
var tpanel1 = panel2.add("tabbedpanel", undefined, undefined, {name: "tpanel1"});
tpanel1.alignChildren = "fill";
tpanel1.preferredSize.width = 200;
tpanel1.preferredSize.height = 150;
tpanel1.margins = 0;
// TAB1
// ====
var tab1 = tpanel1.add("tab", undefined, undefined, {name: "tab1"});
tab1.text = "Saved Books";
tab1.orientation = "column";
tab1.alignChildren = ["center","top"];
tab1.spacing = 10;
tab1.margins = 10;
var dropdown1_array = ["Item 1","-","Item 2"];
var dropdown1 = tab1.add("dropdownlist", undefined, undefined, {name: "dropdown1", items: dropdown1_array});
dropdown1.selection = 0;
// TAB2
// ====
var tab2 = tpanel1.add("tab", undefined, undefined, {name: "tab2"});
tab2.text = "New Book";
tab2.orientation = "column";
tab2.alignChildren = ["center","top"];
tab2.spacing = 10;
tab2.margins = 10;
// TPANEL1
// =======
tpanel1.selection = tab2;
// GROUP3
// ======
var group3 = tab2.add("group", undefined, {name: "group3"});
group3.orientation = "row";
group3.alignChildren = ["left","center"];
group3.spacing = 10;
group3.margins = 0;
var statictext2 = group3.add("statictext", undefined, undefined, {name: "statictext2"});
statictext2.text = "Name";
var edittext2 = group3.add('edittext {properties: {name: "edittext2"}}');
edittext2.text = "EditText";
// GROUP4
// ======
var group4 = tab2.add("group", undefined, {name: "group4"});
group4.orientation = "row";
group4.alignChildren = ["left","center"];
group4.spacing = 10;
group4.margins = 0;
var statictext3 = group4.add("statictext", undefined, undefined, {name: "statictext3"});
statictext3.text = "Author";
var edittext3 = group4.add('edittext {properties: {name: "edittext3"}}');
edittext3.text = "EditText";
// TAB2
// ====
var button2 = tab2.add("button", undefined, undefined, {name: "button2"});
button2.text = "Save";
dialog.show();
I tried a lot from many javascript/nodejs example in the internet. But all the examples are using css! Can you please help me how to solve this issue? Many thanks in advance.
... View more