Skip to main content
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.");
}

 

 

이 주제는 답변이 닫혔습니다.
최고의 답변: dimitri_cas

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.");
}

 

 

 

1 답변

Legend
October 22, 2024

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.

dimitri_cas작성자답변
Inspiring
October 22, 2024

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.");
}

 

 

 

Legend
October 22, 2024

Wonderful. It's working as intended.