• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

if colour exists

Participant ,
Apr 17, 2011 Apr 17, 2011

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';

in my script i've tried to watch for it existing with "isValid"?

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();

}

TOPICS
Scripting

Views

3.7K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 19, 2011 Apr 19, 2011

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

...

Votes

Translate

Translate
Adobe
Community Expert ,
Apr 18, 2011 Apr 18, 2011

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 18, 2011 Apr 18, 2011

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…

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 18, 2011 Apr 18, 2011

Copy link to clipboard

Copied

Previous thing is work fine in my environment, isn't you?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 18, 2011 Apr 18, 2011

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…?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 18, 2011 Apr 18, 2011

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;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 18, 2011 Apr 18, 2011

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 18, 2011 Apr 18, 2011

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…

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 18, 2011 Apr 18, 2011

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{
     mySpot = docRef.swatches.getByName("SpotRed");

    } 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;
        mySpot= docRef.swatches.getByName("SpotRed");
        }
   
    docRef.pathItems[0].fillColor = mySpot.color;
        redraw();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 18, 2011 Apr 18, 2011

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…

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 18, 2011 Apr 18, 2011

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 18, 2011 Apr 18, 2011

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();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 18, 2011 Apr 18, 2011

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 18, 2011 Apr 18, 2011

Copy link to clipboard

Copied

Previous sample scripts target is pathItems. Did you create pathItems like a rectangles or an ellipse before run it?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 18, 2011 Apr 18, 2011

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.size = txObj.paragraphs.characters.size -1
            txObj.paragraphs.characters.leading = txObj.paragraphs.characters.leading -1
            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();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 19, 2011 Apr 19, 2011

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.size = txObj.paragraphs.characters.size -1
            txObj.paragraphs.characters.fillColor = mySpot.color;                        //apply spotColor
            txObj.paragraphs.characters.leading = txObj.paragraphs.characters.leading -1
            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 ].

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 19, 2011 Apr 19, 2011

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 18, 2011 Apr 18, 2011

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 18, 2011 Apr 18, 2011

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…

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines