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

How to set Underline options in character style through Script

Contributor ,
Aug 11, 2021 Aug 11, 2021

Copy link to clipboard

Copied

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

TOPICS
How to , Scripting

Views

677

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 2 Correct answers

Community Expert , Aug 11, 2021 Aug 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 
...

Votes

Translate

Translate
Community Expert , Aug 11, 2021 Aug 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

Votes

Translate

Translate
Community Expert ,
Aug 11, 2021 Aug 11, 2021

Copy link to clipboard

Copied

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);

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
Contributor ,
Aug 11, 2021 Aug 11, 2021

Copy link to clipboard

Copied

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

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 ,
Aug 11, 2021 Aug 11, 2021

Copy link to clipboard

Copied

  • 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

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
Contributor ,
Aug 11, 2021 Aug 11, 2021

Copy link to clipboard

Copied

LATEST

Hi mlb and manan,

 

Thankyou so much! 

Now only i noticed that now it's working perfect.

 

Thanks,

-Jo

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