Italic JavaScript that converts regular text into italics

Explorer ,
Apr 13, 2022 Apr 13, 2022

Copy link to clipboard

Copied

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

Views

137

Likes

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
Adobe Community Professional ,
Apr 13, 2022 Apr 13, 2022

Copy link to clipboard

Copied

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

Likes

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

Copy link to clipboard

Copied

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

Likes

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
Adobe Community Professional ,
Apr 13, 2022 Apr 13, 2022

Copy link to clipboard

Copied

In addition to what @brianp311 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 

Likes

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

Copy link to clipboard

Copied

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. 🫤

Likes

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
Adobe Community Professional ,
Apr 14, 2022 Apr 14, 2022

Copy link to clipboard

Copied

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.

Likes

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
Adobe Community Professional ,
Apr 16, 2022 Apr 16, 2022

Copy link to clipboard

Copied

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

 

Likes

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
Adobe Community Professional ,
Apr 16, 2022 Apr 16, 2022

Copy link to clipboard

Copied

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.

Likes

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