Copy link to clipboard
Copied
In my last post I tried (successfully) to find out how I can get the values out from self-defined swatches.
Now I would like to know how to determine if a swatch/color already exists in the document before a new is added (by input request).
Thank you in advance.
Hi Andreas,
in this case I would not use a try/catch scenario.
Instead I would ask if the color isValid .
var myColorToCheck = app.documents[0].colors.itemByName("ColorName");
// Positive check: is valid:if(myColorToCheck.isValid){/*do something*/};
// Negative check: is not valid:
if(!myColorToCheck.isValid){/*do something else*/};
But this code is only looking for the color's name.
If you want to make sure that all properties are applied you can enforce this.
Just define all the needed properties with
Copy link to clipboard
Copied
use try catch method
Copy link to clipboard
Copied
Oh, I would, if I could...
Unfortunatelly I´ve never used "try/catch". (shame on me)
Copy link to clipboard
Copied
Sure I will show you.. is it you have/wrote code anything now?
Copy link to clipboard
Copied
try
{
app.activeDocument.colors.item("LineNum").name; //change LineNum as you desired
}
catch (myError){
app.activeDocument.colors.add({name:"LineNum", model:ColorModel.process, colorValue:[0, 0, 0, 100]});
}
Copy link to clipboard
Copied
Does that mean:
lines 1-4 = the script tries to find that particular color
lines 5-7 = if there is not, add color
????
Copy link to clipboard
Copied
Yes.. but try what Uwe said
Copy link to clipboard
Copied
Thank you tpk1982!
I´ve allready written, but just simple things. I´m more designer who want to help himself with tiny scripts.
This one would be (for me) something new. But I appreciate it to learn new things. Let´s see...
Copy link to clipboard
Copied
Hi Andreas,
in this case I would not use a try/catch scenario.
Instead I would ask if the color isValid .
var myColorToCheck = app.documents[0].colors.itemByName("ColorName");
// Positive check: is valid:if(myColorToCheck.isValid){/*do something*/};
// Negative check: is not valid:
if(!myColorToCheck.isValid){/*do something else*/};
But this code is only looking for the color's name.
If you want to make sure that all properties are applied you can enforce this.
Just define all the needed properties with their values as an object and apply that object to the properies property, if the color is already there.
Just as I was showing this here:
Re: Get Colour out of a multidimenisonal Array
To rephrase this:
// The full discription of a color as object:
var myColorToCheckProperties =
{
name : "SF_Rot" ,
colorValue : [15,100,100,0] ,
model : ColorModel.PROCESS ,
space : ColorSpace.CMYK
};
// At this point it's not clear, if the color exists in the document:
var myColorToCheck = app.documents[0].colors.itemByName( myColorToCheckProperties.name );
if(myColorToCheck.isValid)
{
/* Do something: Enforce that the right properties are used: */
myColorToCheck.properties = myColorToCheckProperties
};
if(!myColorToCheck.isValid)
{
/* Do something else: Add the color to the document */
app.documents[0].colors.add
(myColorToCheckProperties
)
};
Regards,
Uwe
Copy link to clipboard
Copied
Hi Uwe.
The hint from tpk1982 works in a short test-script. In my main-script it doesn´t. (Maybe I made a mistake because of the functions inside the script). But I learned something new.
For me your way of doing sounds logical because I´m more familiar with the IF-condition. I'll try to do this after work... I will get back to you later.
Thanks by the way!
Copy link to clipboard
Copied
Well, it works, at last! I took the short version.
Here is the function with comments (in German).
function newColour() {
var myColorToCheck = app.documents[0].colors.itemByName(colourArray[colourSwatch-1].name);
// Wenn die Farbe (Eingabe) vorhanden ist ...
if(myColorToCheck.isValid){
//Holt sich die gewünschte Farbe aus den Dokumentenfarbpalette, anhand der Eingabe und der vordefinierten Farben im colourArray
tabellenFarbe = myColours.itemByName(colourArray[colourSwatch-1].name);
alert("Farbe ist bereits in der Farbpalette vorhanden: " + tabellenFarbe.name);
};
// Wenn die Farbe nicht vorhanden ist ...
if(!myColorToCheck.isValid){
//Erstellt ein neues Farbfeld anhand der Eingabe und der vordefinierten Farben im colourArray
tabellenFarbe = myColours.add({name:colourArray[colourSwatch-1].name, model: ColorModel.PROCESS, space:ColorSpace.CMYK, colorValue:colourArray[colourSwatch-1].colorValue});
alert("Die neue Farbe aus Ihrer Eingabe wurde hinzugefügt: " + colourArray[colourSwatch-1].name);
};
// Einfärben :
// 1. von jeder unterkante einer jeden Zelle
myTable.cells.everyItem().bottomEdgeStrokeColor = tabellenFarbe;
// 2. von jeder Oberkante einer jeden Zelle
myTable.cells.everyItem().topEdgeStrokeColor = tabellenFarbe;
// 3. aller Zeichen in der erste Zeile
myTable.rows.firstItem().cells.everyItem().characters.everyItem().fillColor = tabellenFarbe;
}
Perhaps it helps a newbie ...
Than you again Uwe!