Copy link to clipboard
Copied
I would like to change the colour of some text.
The problem is if the colour exists I keep getting the error:
8707 the name is in use... -> thisSpot.name = 'SpotRed';
var myColor = new CMYKColor;
myColor.cyan = 0;
myColor.magenta = 100;
myColor.yellow = 60;
myColor.black = 0;
var thisSpot = docRef.spots.add();
if(docRef.swatches.getByName('SpotRed').isValid != false){
thisSpot.name = 'SpotRed';
thisSpot.color = myColor
thisSpot.colorType = ColorModel.SPOT;
}else{
var mySpot = docRef.swatches.getByName('SpotRed');
docRef.textFrames[0].textRange.fillColor = mySpot.color;
redraw();
}
At first, we must check spots. And under 70% mean scaling character size, so we had better apply spot color in function of scalingSize.
#target illustrator
//colour check
if (!isColorExist('SpotRed')){
var myColor = new CMYKColor;
myColor.cyan = 0;
myColor.magenta = 100;
myColor.yellow = 20;
myColor.black = 0;
var thisSpot = docRef.spots.add();
thisSpot.name = 'SpotRed';
thisSpot.color = myColor
thisSpot.colorType = ColorModel.SPOT;
}
var mySpot= docRef.swatches.getByNa
...Copy link to clipboard
Copied
, How about like below.
var docRef = app.activeDocument;
try{
var mySpot = docRef.swatches.getByName('SpotRed');
docRef.pathItems[0].fillColor = mySpot.color;
redraw();
} catch (e){
var myColor = new CMYKColor;
myColor.cyan = 0;
myColor.magenta = 100;
myColor.yellow = 20;
myColor.black = 0;
var thisSpot = docRef.spots.add();
thisSpot.name = 'SpotRed';
thisSpot.color = myColor
thisSpot.colorType = ColorModel.SPOT;
docRef.pathItems[0].fillColor = docRef.swatches.getByName('SpotRed').color;
redraw();
}
When error occured, cleate a new spots and apply.
Copy link to clipboard
Copied
Can you try/catch a getByName() like this? It's never worked for me? I just get a runtime error…
Copy link to clipboard
Copied
Previous thing is work fine in my environment, isn't you?
Copy link to clipboard
Copied
I've never been able to get this way to work… I just get 'No such element' and a script freeze at the getByName() line… Nothing is thrown over to the catch to create the swatch…?
Copy link to clipboard
Copied
the problem is the second line in try:
docRef.pathItems[0].fillColor = mySpot.color;
should be:
docRef.pathItems[0].fillColor = mySpot.color.spot.color;
Copy link to clipboard
Copied
After adding docRef.pathItems[0].fillColor = mySpot.color.spot.color;
I'm still getting the same runtime error as MuppetMark!
Copy link to clipboard
Copied
Chris, getByName() is what causes an error for me… It's all fine and dandy when the element is in the collection… But if Im not sure if this is going to be the case then I have to test the collection elements by name property then proceed… Putting a try catch statement around this just does not work for me…? I would be happy if it returned null and I could carry on…
Copy link to clipboard
Copied
strange, mark and mitch
ive tried it from within illustrator(cs3), and it works(or not) like i wrote above
from estk2 it makes no difference (swatch.color or swatch.color.spot.color), both works ???
why should it be illegal to try/catch the getByName? who knows...
fails this also? :
var docRef = app.activeDocument;
var mySpot;
try{Copy link to clipboard
Copied
Chris, I went and ran some checks and it would appear to work when ran from inside of Illustrator's scripts folder by the app. It still errors at the getByName() line every which way I try write… No such element… When ran in ESTK with Illustrator as target… it's the same Im sure in other app's where I use this method…
Copy link to clipboard
Copied
I've had the problem for a while now that's why i tried the 'isValid'.
Is there any other method for isolating the color? I have seen something for character styles?
Copy link to clipboard
Copied
In Indesign, all Objects have isValid property, but Illustrator don't. So we have to validate ourselves. When I think that we can collecting error and then make spots object. But it could not for some environments. I don't know why couldn't catch it and really want to know what happen.
So we have to find out another approach.
I write down below function, can check all swatches name propaty.
function isColorExist(colorName){
var clrs = app.activeDocument.swatches;
for (var i=0;i<clrs.length;i++){
if (clrs.name==colorName){
return true;
}
}
return false;
}
var docRef = app.activeDocument;
if (!isColorExist('SpotRed')){
var myColor = new CMYKColor;
myColor.cyan = 0;
myColor.magenta = 100;
myColor.yellow = 20;
myColor.black = 0;
var thisSpot = docRef.spots.add();
thisSpot.name = 'SpotRed';
thisSpot.color = myColor
thisSpot.colorType = ColorModel.SPOT;
}
var mySpot= docRef.swatches.getByName("SpotRed");
docRef.pathItems[0].fillColor = mySpot.color;
redraw();
Copy link to clipboard
Copied
Hi Ten,
I've tried youre script but I get an error at "docRef.pathItems[0].fillColor = mySpot.color;"
saying No such element?
Copy link to clipboard
Copied
Previous sample scripts target is pathItems. Did you create pathItems like a rectangles or an ellipse before run it?
Copy link to clipboard
Copied
I'm using it to change the colour of text in text frames,
replacing it with 'docRef.textFrames[0].textRange.fillColor = mySpot.color;' as per (Muppet Marks help)
seems to make a difference.
I've attached what i have so far, basically anythinge scaled below 70% needs to be highlighted red. It's changing everything red at the moment.
#target illustrator
var counter = 0;
slct = app.activeDocument.selection[0];
cntsLength = getChars(slct);
visibleChar =getVisible(slct);
var vscale = 1.0;
stepNum = 0.02;
while (cntsLength != visibleChar) {
if (vscale>0.7){
scalingWidth(slct, [vscale,1]);
} else {
scalingSize(slct);
isColorExist(slct);
}
visibleChar =getVisible(slct);
vscale = vscale-stepNum;
}
function getVisible(txObj){
var result=0;
for (var i=0;i<txObj.lines.length;i++){
result += txObj.lines.characters.length;
}
return result;
}
function getChars(txObj){
var result=0;
for (var i=0;i<txObj.paragraphs.length;i++){
result += txObj.paragraphs.characters.length;
}
return result;
}
function scalingSize(txObj){
for (var i=0;i<txObj.paragraphs.length;i++){
for(var j=0;j<txObj.paragraphs.characters.length;j++ ){
txObj.paragraphs.characters
txObj.paragraphs.characters
redraw();
}
}
}
function scalingWidth(txObj,wscl){
for (var i=0;i<txObj.paragraphs.length;i++){
txObj.paragraphs.scaling = wscl;
redraw();
}
}
function isColorExist(txObj,){
var clrs = app.activeDocument.swatches;
for (var i=0;i<clrs.length;i++){
if (clrs.name==colorName){
return true;
}
}
return false;
}
var docRef = app.activeDocument;
if (!isColorExist('SpotRed')){
var myColor = new CMYKColor;
myColor.cyan = 0;
myColor.magenta = 100;
myColor.yellow = 20;
myColor.black = 0;
var thisSpot = docRef.spots.add();
thisSpot.name = 'SpotRed';
thisSpot.color = myColor
thisSpot.colorType = ColorModel.SPOT;
}
var mySpot= docRef.swatches.getByName("SpotRed");
docRef.textFrames[0].textRange.fillColor = mySpot.color;
redraw();
Copy link to clipboard
Copied
At first, we must check spots. And under 70% mean scaling character size, so we had better apply spot color in function of scalingSize.
#target illustrator
//colour check
if (!isColorExist('SpotRed')){
var myColor = new CMYKColor;
myColor.cyan = 0;
myColor.magenta = 100;
myColor.yellow = 20;
myColor.black = 0;
var thisSpot = docRef.spots.add();
thisSpot.name = 'SpotRed';
thisSpot.color = myColor
thisSpot.colorType = ColorModel.SPOT;
}
var mySpot= docRef.swatches.getByName("SpotRed");
//now we can apply mySpot.color
var counter = 0;
slct = app.activeDocument.selection[0];
cntsLength = getChars(slct);
visibleChar =getVisible(slct);
var vscale = 1.0;
stepNum = 0.02;
while (cntsLength != visibleChar) {
if (vscale>0.7){
scalingWidth(slct, [vscale,1]);
} else {
scalingSize(slct);
}
visibleChar =getVisible(slct);
vscale = vscale-stepNum;
}
function getVisible(txObj){
var result=0;
for (var i=0;i<txObj.lines.length;i++){
result += txObj.lines.characters.length;
}
return result;
}
function getChars(txObj){
var result=0;
for (var i=0;i<txObj.paragraphs.length;i++){
result += txObj.paragraphs.characters.length;
}
return result;
}
function scalingSize(txObj){
for (var i=0;i<txObj.paragraphs.length;i++){
for(var j=0;j<txObj.paragraphs.characters.length;j++ ){
txObj.paragraphs.characters
txObj.paragraphs.characters
txObj.paragraphs.characters
redraw();
}
}
}
function scalingWidth(txObj,wscl){
for (var i=0;i<txObj.paragraphs.length;i++){
txObj.paragraphs.scaling = wscl;
redraw();
}
}
function isColorExist(txObj,){
var clrs = app.activeDocument.swatches;
for (var i=0;i<clrs.length;i++){
if (clrs.name==colorName){
return true;
}
}
return false;
}
textRange mean renge of text. paragraph lines and characters are textRange Object. If you select a textFrame and run below script,
alert(app.activeDocument.selection[0].paragraphs[0])
return to [ textRange ].
Copy link to clipboard
Copied
Hi Ten,
I have it working a treat now much appreciated.
Just like to thank you and the rest of the lads who have helped me understand this.
Copy link to clipboard
Copied
I just ran the above script by chris in AICS4 from the ESTK with AI as the target app and it did everything it was supposed to; added a new swatch called "SpotRed" and used it to fill a path item.
Copy link to clipboard
Copied
Well, Im now at home so I went back to the older install of the ESTK and CS2 and the result is the same as at work… In the newer tool kit and CS5 all is well and it works as I had hoped it would before now… Thanks Chris I will just have to bear that in mind…