Copy link to clipboard
Copied
I tried this solution but it does not work
Where am I doing wrong
var doc = activeDocument;
try{
if (activeDocument.colorSamplers.length > 0)
{alert ("color sampler/s exist/s")};
}
catch(){
{alert ("color sampler/no exist")};
}
Why do you use the try-clause?
As long as a file is open you should be able to use the »else« of the »if«.
if (activeDocument.colorSamplers.length > 0) {alert ("color sampler/s exist/s")}
else {alert ("color sampler/no exist")};
Copy link to clipboard
Copied
Simple activeDocument.colorSamplers.length is sufficent.
But if you want try/catch worked you have to add missing value inside rounded brackets, for ex. catch(err)
Copy link to clipboard
Copied
tried it too but it does not work?
var doc = activeDocument;
try{
if (activeDocument.colorSamplers.length > 0)
{alert ("color sampler/s exist/s")};
}
catch(e){
{alert ("color sampler/no exist")};
}
Copy link to clipboard
Copied
Why do you use the try-clause?
As long as a file is open you should be able to use the »else« of the »if«.
if (activeDocument.colorSamplers.length > 0) {alert ("color sampler/s exist/s")}
else {alert ("color sampler/no exist")};
Copy link to clipboard
Copied
I thought the best way was to try
now I learned another thing
Thank you
Copy link to clipboard
Copied
To use a try clause in a case like this it should include something that is possible only if the desired »thing« exists.
In this case you could define a variable from the first ColorSampler which would throw an error if no ColorSamplers exist.
#target photoshop
var doc = activeDocument;
try {
var theColorSampler = doc.colorSamplers[0];
alert ("color sampler/s exist/s")
} catch (e) {
alert ("color sampler/no exist")
};
Copy link to clipboard
Copied
Alternative way )
alert( has_property("document", "colorSamplerList") )
function has_property(class_name, prpr_name)
{
try
{
var r = new ActionReference();
r.putProperty(charIDToTypeID("Prpr"), stringIDToTypeID(prpr_name));
r.putEnumerated( stringIDToTypeID( class_name ), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
if (executeActionGet(r).hasKey(stringIDToTypeID(prpr_name))) return true;
return false;
}
catch (e) { alert(e); }
}