Changing Font of TextFrame via Extend Script in Python
Hi,
I am trying to change the Font of the contents of a TextFrame, however when doing so, Illustrator crashes.
I am fairly sure the code is correct, as it correctly replaces the *text* of the TextFrame, just not it's Font.
Below is the snippit in question:
# Find and modify the text object
text_modified = False
for item in target_layer.PageItems:
if item.typename == "TextFrame": # Check if the item is a text object
# Update the text content
item.TextRange.Contents = new_text_content
print(f"Successfully changed Text to '{new_text_content}'")
# Change the font of the text object
try:
# Get the exact font name (ensure this is the correct name from the list)
desired_font = app.TextFonts[new_font_name]
# Apply the font to the entire text range
item.TextRange.CharacterAttributes.TextFont = desired_font
print(f"Font '{new_font_name}' successfully applied!")
except Exception as e:
print(f"Font '{new_font_name}' not found or not applicable: {e}")
text_modified = True
break
new_text_content="Abraham Lincoln",
new_font_name="TimesNewRomanPS-BoldMT", # Use the full font name as Illustrator recognizes it
I have tried various fonts, and when they are spelled and formatted correctly, IE "VerdanaPro-CondBlack" Illustrator will always crash, however if I mis-spell the font or give an incomplete name, IE "Verdana" the script will fail to find it and continue without crashing.
Can anyone suggest how to correctly apply a specified font to a TextFrame? Is there an obvious error I am overlooking?
Your time is appreciated.
