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];
var tr = text.textRange;
tr.stroked = true;
tr.strokeColor = newColor;
tr.strokeWeight = 9.2;
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 =
...
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?
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.
Copy link to clipboard
Copied
var tr = text.textRange;
tr.stroked = true;
tr.strokeColor = newColor;
tr.strokeWeight = 9.2;
Copy link to clipboard
Copied
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.