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

Italic JavaScript that converts regular text into italics

Explorer ,
Apr 13, 2022 Apr 13, 2022

I use this simple JavaScript (Italic.jsx) that converts highlighted Open Sans text into italics:

app.selection[0].appliedCharacterStyle='Italic';

 

Worked until at least InDesign 16 but now produces this error:

Adobe InDesign script error.png

Anyone have a tip on how to make this work again?

TOPICS
Scripting , Type
1.4K
Translate
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 ,
Apr 13, 2022 Apr 13, 2022

You sure the document has a Character Style called "Italic"?

Translate
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
Explorer ,
Apr 14, 2022 Apr 14, 2022

Well it worked before on hudreds of documents and the font set was the same.

Translate
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 ,
Apr 13, 2022 Apr 13, 2022

In addition to what @brian_p_dts mentioned also make sure that the style is not a part of a group. You could even try the following code snippet

 

app.selection[0].appliedCharacterStyle=app.documents[0].characterStyles.itemByName("Italic");

 

For example on how to deal/find style within groups you can refer to the following discussion. It talks about paragraph styles but the code for character style also remains the same mostly with changes only to the name of property and methods.

https://community.adobe.com/t5/indesign-discussions/script-find-paragraph-style-under-the-folder/m-p...

-Manan 

Translate
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
Explorer ,
Apr 14, 2022 Apr 14, 2022

That didn't work either, produces a similar #30477 error.

 

I created a workaround. Using the standard Shift-Command-I, which used to select Open Sans Condensed Italic because, I guess, that was first in the font menu. So I deleted that particular Italic and now it works but it’s not an optimal solution. 🫤

Translate
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 ,
Apr 14, 2022 Apr 14, 2022

Please paste an image of your Character Styles panel of the document you are getting this error on that shows a named character style called Italic. If the document does not have that Character Style this script will not work. Does not matter if it has worked on hundreds of other documents. The script cares nothing about font sets.

Translate
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 ,
Apr 16, 2022 Apr 16, 2022

If you don't want it to be depending on a character style, then the code is: 

 

try { app.selection[0].fontStyle = "Italic"; } 
catch(e) { 
    if (app.selection.length == 0) { alert("Select something!"); }
    else if (!app.selection[0].hasOwnProperty("fontStyle")) { alert("Select something with a font style!"); }
    else { alert("No Italic style available for this font! It might be named something else!"); }
}

 

Translate
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 ,
Apr 16, 2022 Apr 16, 2022
LATEST

Make sure that that your character style's name is really Italic. There could be an invisible character in the style's name. I've seen quite a few cases of a style name that had a return at the end or a tab. You don't see that in the panel, but you can try this: apply the style to some text, then click somewhere in that text and run this script:

 

app.selection[0].appliedCharacterStyle.name.length

 

In your case it should return 6.

The problem can happen if you (or some other user) copied a name and used that to name the style.

To fix a problem like this, simply re-type the name in the panel.

P.

Translate
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