Skip to main content
Known Participant
August 12, 2022
Answered

Script to change the color of the selected texts that grouped

  • August 12, 2022
  • 1 reply
  • 750 views

Hello Everyone,

 

I have  the script to change the color of the text in the selected text frame.

 

But, sometimes in my file certain textframes are grouped together, at that time this script  doesn't change the color of the text in  grouped text frames.

 

Someone would help to figure out this situation.

 

I am attaching the code that worked for the non grouped text frames.

docRef = app.activeDocument;

var selectedObjects = docRef.selection;

var color1 = new CMYKColor();
color1.cyan = 100;
color1.magenta = 0;
color1.yellow = 0;
color1.black = 0;
var spot = docRef.spots.add();
spot.color = color1;
spot.colorType = ColorModel.SPOT;
spot.name = "Keyline Blue";

for (var i = 0; i < selectedObjects.length; i++) {
    if (selectedObjects[i].typename == "TextFrame") {
        selectedObjects[i].textRange.fillColor = keyline.color;
    }
}

 

This topic has been closed for replies.
Correct answer Charu Rajput

Below is the version for the selected objects only. This version will change color for all selected textframes that are grouped or ungrouped.

docRef = app.activeDocument;

var color1 = new CMYKColor();
color1.cyan = 100;
color1.magenta = 0;
color1.yellow = 0;
color1.black = 0;
var spot = docRef.spots.add();
spot.color = color1;
spot.colorType = ColorModel.SPOT;
spot.name = "Keyline Blue";

var selectedObjects = docRef.selection;

for (var i = 0; i < selectedObjects.length; i++) {
    if (selectedObjects[i].typename == "TextFrame") {
        selectedObjects[i].textRange.fillColor = spot.color;
    } else if (selectedObjects[i].typename == "GroupItem") {
        for (var t = 0; t < selectedObjects[i].textFrames.length; t++) {
            selectedObjects[i].textFrames[t].textRange.fillColor = spot.color;
        }
    }
}

 

1 reply

Charu Rajput
Community Expert
Community Expert
August 12, 2022

Try the following version

docRef = app.activeDocument;

var color1 = new CMYKColor();
color1.cyan = 100;
color1.magenta = 0;
color1.yellow = 0;
color1.black = 0;
var spot = docRef.spots.add();
spot.color = color1;
spot.colorType = ColorModel.SPOT;
spot.name = "Keyline Blue";

var _groupItems = docRef.groupItems;
for (var g = 0; g < _groupItems.length; g++) {
    for (var t = 0; t < _groupItems[g].textFrames.length; t++) {
        _groupItems[g].textFrames[t].textRange.fillColor = spot.color;
    }
}

 

I was getting an error in your code at keyline.color beacuse keyline is undefined, Therefore change to spot.color

Best regards
Surya24Author
Known Participant
August 12, 2022

Hi Charu Rajput,

 

Your script will change the grouped text frames in the entired document. but i want to apply the script to the selected grouped text frames in the illustrator file.

 

I just want to change the selected textframes that are grouped.

 

Charu Rajput
Community Expert
Charu RajputCommunity ExpertCorrect answer
Community Expert
August 12, 2022

Below is the version for the selected objects only. This version will change color for all selected textframes that are grouped or ungrouped.

docRef = app.activeDocument;

var color1 = new CMYKColor();
color1.cyan = 100;
color1.magenta = 0;
color1.yellow = 0;
color1.black = 0;
var spot = docRef.spots.add();
spot.color = color1;
spot.colorType = ColorModel.SPOT;
spot.name = "Keyline Blue";

var selectedObjects = docRef.selection;

for (var i = 0; i < selectedObjects.length; i++) {
    if (selectedObjects[i].typename == "TextFrame") {
        selectedObjects[i].textRange.fillColor = spot.color;
    } else if (selectedObjects[i].typename == "GroupItem") {
        for (var t = 0; t < selectedObjects[i].textFrames.length; t++) {
            selectedObjects[i].textFrames[t].textRange.fillColor = spot.color;
        }
    }
}

 

Best regards