Copy link to clipboard
Copied
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.");
}
Hey sttk3 -
Thanks for the clarification 😉
Here is my Illustrator Javascript adapted (later I'll "couple" it w/ a button to make is easier to use (in my case)):
// Ensure something is selected
if (app.documents.length > 0 && app.selection.length > 0) {
var selectedItem = app.selection; // app.selection is already a TextRange when selecting characters
// Check if the selected item is a TextRange
if (selectedItem.typename === "TextRange") {
try {
// Apply the fon
...
Copy link to clipboard
Copied
When the user is selecting characters in a text frame, the selection becomes a TextRange (not an Array); do not refer to selection[0], but check the typename of the selection directly.
Copy link to clipboard
Copied
Hey sttk3 -
Thanks for the clarification 😉
Here is my Illustrator Javascript adapted (later I'll "couple" it w/ a button to make is easier to use (in my case)):
// Ensure something is selected
if (app.documents.length > 0 && app.selection.length > 0) {
var selectedItem = app.selection; // app.selection is already a TextRange when selecting characters
// Check if the selected item is a TextRange
if (selectedItem.typename === "TextRange") {
try {
// Apply the font "Geomanist Medium" to the selected text range
selectedItem.characterAttributes.textFont = app.textFonts.getByName("Geomanist-Medium");
} catch (e) {
alert("The font 'Geomanist Medium' is not available on your system.");
}
} else {
alert("Please select a portion of text using the Type Tool.");
}
} else {
alert("Please open a document and select some text.");
}
Copy link to clipboard
Copied
Wonderful. It's working as intended.