Skip to main content
jothipriya
Inspiring
August 11, 2021
Answered

How to set Underline options in character style through Script

  • August 11, 2021
  • 1 reply
  • 1179 views

Hi Everyone,

 

I have tried to set the underline options for character style like type, weight, offset, color etc... through script but i doesn't got any idea and i have refered the scripting guide still now i'm confused....

I have attached the sample code for more details......

Kindly tell me how to set this???????????

 

 

app.findGrepPreferences = app.changeGrepPreferences =null;   
 var searchText =  "InDesign";
app.findGrepPreferences.findWhat = searchText  ;
var list = app.activeDocument.findGrep ();
try  
{  
var new_Style = app.documents[0].characterStyles.item("underlie_Text");  
new_Style.name;
}  
catch (myError){  
var new_Style = app.documents[0].characterStyles.add()     
new_Style.name = "underlie_Text";  
}  
new_Style.underline= true;
list.appliedCharacterStyle="col";

 

 

Thanks,

-Jo

This topic has been closed for replies.
Correct answer Manan Joshi
  • In the second last line myStyle should be new_Style
  • In the last line you should iterate the list, as it's and array and then apply the character style. Like the following

 

for(var i = 0; i < list.length; i++)
   list[i].appliedCharacterStyle = "underlie_Text"

 

-Manan

1 reply

m1b
Community Expert
August 11, 2021

Hi Jo, I don't fully understand what you are doing, but this may help a little:

var doc = app.activeDocument;

// try to get reference to the character style
var styleName = 'Underlined',
    myStyle = doc.characterStyles.itemByName(styleName);

// if it isn't valid (it's missing) then make it
if (!myStyle.isValid) myStyle = doc.characterStyles.add({ name: styleName });

// give the style a cyan underline
myStyle.underline = true;
myStyle.underlineColor = "C=100 M=0 Y=0 K=0";

// apply the style to a selected text frame's story
doc.selection[0].parentStory.paragraphs.everyItem().applyCharacterStyle(myStyle);
jothipriya
Inspiring
August 11, 2021

What I'm trying to do is to set a character style for the serched text....

While creating the character style

1. the underline options are not set

2. the created character style was not applied to the search text

this was my problem and I have tried 

myStyle.underlineColor = "C=100 M=0 Y=0 K=0";

it's not applying and i have added the comments in my code to understand better

app.findGrepPreferences = app.changeGrepPreferences =null;   
var searchText =  "hi"; //search text
app.findGrepPreferences.findWhat = searchText  ;
var list = app.activeDocument.findGrep ();
try  
{  
var new_Style = app.documents[0].characterStyles.item("underlie_Text");//character style 
new_Style.name;
}  
catch (myError){  
var new_Style = app.documents[0].characterStyles.add()     
new_Style.name = "underlie_Text";  
}  
new_Style.underline= true;//It's Working
myStyle.underlineColor = "C=100 M=0 Y=0 K=0";//It's not working
list.appliedCharacterStyle="underlie_Text";//Style was not applied to the search text

what was the problem in my code???????????

 

-jo

Manan JoshiCorrect answer
Community Expert
August 11, 2021
  • In the second last line myStyle should be new_Style
  • In the last line you should iterate the list, as it's and array and then apply the character style. Like the following

 

for(var i = 0; i < list.length; i++)
   list[i].appliedCharacterStyle = "underlie_Text"

 

-Manan