Skip to main content
Participant
July 8, 2022
Answered

ExtendScript - Set Text Stroke

  • July 8, 2022
  • 2 replies
  • 998 views

Greetings!

 

I am trying to add a simple stroke to a text object in Illustrator using ExtendScript Toolkit. I have been following some documentation for interacting with the DOM, but I am having a hard time figuring out why this isn't working (no errors are thrown). I have tried printing the strokewidth and stroked boolean values after they are applied to the text, and they show stroked = true and the correct strokeWidth value. However, when I navigate to the directory where I export the file as png (also using the script), the image has no stroke on the text element I was trying to modify.

 

There must be something wrong with my code. Any help is much appreciated!

 

// select layer
doc.selection[0];
text = doc.activeLayer.pageItems.getByName("textLine1");

// define the color white
var newColor = new CMYKColor();
newColor.cyan = 0;
newColor.magenta = 0;
newColor.yellow = 0;
newColor.black = 0;

// add stroke to text
text.stroked = true;
text.strokeWidth = 9.2;
text.strokeColor = newColor;

// get Y position of text
yPos = text.position[1];
			
// if too big, reduce the size of the text
if (text.width >= 1620) {
	targetWidth = 1620;
	ratio = targetWidth/text.width;
	text.width = targetWidth;
	text.height = text.height * ratio;
}
			
// center of artboard and center of text element
docCenterX = doc.width/2;
textXRadius = text.width/2;
			
// assign new position for resized text
text.position = [docCenterX - textXRadius, yPos];

 

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

@Matthias24506020s6ay 

Try following version

doc = app.activeDocument;
text = doc.activeLayer.pageItems.getByName("textLine1");

// define the color white
var newColor = new CMYKColor();
newColor.cyan = 0;
newColor.magenta = 0;
newColor.yellow = 0;
newColor.black = 100;

// add stroke to text
for (var t = 0; t < text.textRanges.length; t++) {
	text.textRanges[t].stroked = true;
	text.textRanges[t].strokeWidth = 0.25;
	text.textRanges[t].strokeColor = newColor;
}

// get Y position of text
yPos = text.position[1];

// if too big, reduce the size of the text
if (text.width >= 1620) {
	targetWidth = 1620;
	ratio = targetWidth / text.width;
	text.width = targetWidth;
	text.height = text.height * ratio;
}

// center of artboard and center of text element
docCenterX = doc.width / 2;
textXRadius = text.width / 2;

// assign new position for resized text
text.position = [docCenterX - textXRadius, yPos];

 

Use values for cmyk and strokeWidth as required.

2 replies

Charu Rajput
Community Expert
Charu RajputCommunity ExpertCorrect answer
Community Expert
July 8, 2022

@Matthias24506020s6ay 

Try following version

doc = app.activeDocument;
text = doc.activeLayer.pageItems.getByName("textLine1");

// define the color white
var newColor = new CMYKColor();
newColor.cyan = 0;
newColor.magenta = 0;
newColor.yellow = 0;
newColor.black = 100;

// add stroke to text
for (var t = 0; t < text.textRanges.length; t++) {
	text.textRanges[t].stroked = true;
	text.textRanges[t].strokeWidth = 0.25;
	text.textRanges[t].strokeColor = newColor;
}

// get Y position of text
yPos = text.position[1];

// if too big, reduce the size of the text
if (text.width >= 1620) {
	targetWidth = 1620;
	ratio = targetWidth / text.width;
	text.width = targetWidth;
	text.height = text.height * ratio;
}

// center of artboard and center of text element
docCenterX = doc.width / 2;
textXRadius = text.width / 2;

// assign new position for resized text
text.position = [docCenterX - textXRadius, yPos];

 

Use values for cmyk and strokeWidth as required.

Best regards
Charu Rajput
Community Expert
Community Expert
July 8, 2022

HI @Matthias24506020s6ay  
How would you like to get stroke. The one which is at the top or at the bottom in the screen shot.

Textframe does not ahve stroked property, if you want to have the efct as upper, then you should apply stroke to TextRange. So please let us know what effect do you want?

 

Best regards
Participant
July 8, 2022

Thank you for your reply, @Charu Rajput. I would like to apply the stroke effect as you have demonstrated in the top of your example screenshot, where the stroke follows the shape of the text.

jduncan
Community Expert
Community Expert
July 8, 2022
var tr = text.textRange;
tr.stroked = true;
tr.strokeColor = newColor;
tr.strokeWeight = 9.2;