• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

•  Javascript to change the weight of a typeface (eg. Bold to Medium)

Engaged ,
Oct 22, 2024 Oct 22, 2024

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

 

 

TOPICS
Scripting

Views

251

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 1 Correct answer

Engaged , Oct 22, 2024 Oct 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 fon
...

Votes

Translate

Translate
Adobe
Community Expert ,
Oct 22, 2024 Oct 22, 2024

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.

Votes

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
Engaged ,
Oct 22, 2024 Oct 22, 2024

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

 

 

 

Votes

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
Community Expert ,
Oct 22, 2024 Oct 22, 2024

Copy link to clipboard

Copied

LATEST

Wonderful. It's working as intended.

Votes

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