Inspiring
October 22, 2024
해결됨
• Javascript to change the weight of a typeface (eg. Bold to Medium)
- October 22, 2024
- 1 답변
- 660 조회
Hi -
I try to write a small Adobe Illustrator Javascript that (when called) changes the "weight" of some selected text (eg. Bold to Medium).
Precision: Text selection is made with the "Type Tool" (NOT the "Selection Tool").
NB. The script works when a whole textfield is selected w/ the "Selection Tool"
I always get the following error "Error 21: undefined is not an object." on line 35/36 (cfr. screenshot)
Here is my code:
// #targetengine miscellaneous;
#target Illustrator
#targetengine main
// Ensure something is selected
if (app.documents.length > 0 && app.selection.length > 0) {
var selectedItem = app.selection[0];
alert(selectedItem);
// Check if the selected item is text (whether it's a range or text frame selection)
if (selectedItem.typename === 'TextRange' || selectedItem.typename === 'TextFrame') { // item.typename === 'TextFrame' && (item.kind === TextType.POINTTEXT || item.kind === TextType.AREATEXT)) {
// if (typeof selectedItem === "TextRange" || selectedItem === "TextFrame") {
try {
// Change the font of the selected text (works for both TextRange and full TextFrame)
selectedItem.textRange.characterAttributes.textFont = app.textFonts.getByName("Geomanist-Medium");
} catch (e) {
alert("The font 'Geomanist Medium' is not available on your system.");
}
} else {
alert("Please select some text using the Type Tool or select a text frame.");
}
} else {
alert("Please open a document and/or select some text.");
}
