ExtendScript - Set Text Stroke

New Here ,
Jul 08, 2022 Jul 08, 2022

Copy link to clipboard

Copied

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

 

TOPICS
Scripting , Type

Views

107

Likes

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 2 Correct answers

Participant , Jul 08, 2022 Jul 08, 2022
var tr = text.textRange;
tr.stroked = true;
tr.strokeColor = newColor;
tr.strokeWeight = 9.2;

Likes

Translate

Translate
Adobe Community Professional , Jul 08, 2022 Jul 08, 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 =
...

Likes

Translate

Translate
Adobe Community Professional ,
Jul 08, 2022 Jul 08, 2022

Copy link to clipboard

Copied

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?

 

Screenshot 2022-07-08 at 5.40.12 PM.png

Best regards

Likes

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
New Here ,
Jul 08, 2022 Jul 08, 2022

Copy link to clipboard

Copied

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.

Likes

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 ,
Jul 08, 2022 Jul 08, 2022

Copy link to clipboard

Copied

var tr = text.textRange;
tr.stroked = true;
tr.strokeColor = newColor;
tr.strokeWeight = 9.2;

Likes

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
Adobe Community Professional ,
Jul 08, 2022 Jul 08, 2022

Copy link to clipboard

Copied

LATEST

@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

Likes

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