Skip to main content
dublove
Legend
January 24, 2024
Answered

What is the method to identify which column a certain character style is in?

  • January 24, 2024
  • 3 replies
  • 1126 views

What is the method to search for a certain character style (such as Character Style AAA), identify its A, B, or C columns in two (or three) columns, and then apply Character Style A, Character Style B, and Character Style C respectively

 

The manual workload is too heavy, help~ please 

Thank you.

 

Correct answer Peter Kahrel

Determining the column index of some text is something that comes back regularly. In this case, to apply a character style to some text based on its column index can be done as follows:

 

cstyles = [
  app.activeDocument.characterStyles.item('A'),
  app.activeDocument.characterStyles.item('B'),
  app.activeDocument.characterStyles.item('C'),
];

app.findGrepPreferences = null;
app.findGrepPreferences.appliedCharacterStyle  
  = app.activeDocument.characterStyles.item ('AAA');
  
instances = app.activeDocument.findGrep();

for (i = 0; i < instances.length; i++) {
  columnIndex = instances[i].parentStory.insertionPoints.itemByRange (
    instances[i].parentTextFrames[0].insertionPoints[0].index,
    instances[i].index
  ).textColumns.length-1;
  instances[i].appliedCharacterStyle = cstyles[columnIndex];
}

 

In other words, you count the number of columns between the parent frame's first insertion point and the text's first insertion point. (The script can be made more efficient by working story by story, but that doesn't change the basic principle of getting a column index.)

3 replies

dublove
dubloveAuthor
Legend
December 17, 2024

Thank you very much

Community Expert
December 26, 2024

Looking at the error line the thing that could be an issue according to me is either the character style does not exist or there is some issue with the name of the character style.

-Manan

-Manan
Community Expert
December 27, 2024

This discussion was locked because it repeated another one, where the correct answer was given. I don't know what happened to the note I left here that pointed that out.

Anyway, it shows that your answer is correct, @Manan Joshi 🙂


Ahh, now I notice the locked icon. However, no note present. Ideally I would have merged this to the original post or rather marked it for deletion as a duplicate.

-Manan

-Manan
Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
January 24, 2024

Determining the column index of some text is something that comes back regularly. In this case, to apply a character style to some text based on its column index can be done as follows:

 

cstyles = [
  app.activeDocument.characterStyles.item('A'),
  app.activeDocument.characterStyles.item('B'),
  app.activeDocument.characterStyles.item('C'),
];

app.findGrepPreferences = null;
app.findGrepPreferences.appliedCharacterStyle  
  = app.activeDocument.characterStyles.item ('AAA');
  
instances = app.activeDocument.findGrep();

for (i = 0; i < instances.length; i++) {
  columnIndex = instances[i].parentStory.insertionPoints.itemByRange (
    instances[i].parentTextFrames[0].insertionPoints[0].index,
    instances[i].index
  ).textColumns.length-1;
  instances[i].appliedCharacterStyle = cstyles[columnIndex];
}

 

In other words, you count the number of columns between the parent frame's first insertion point and the text's first insertion point. (The script can be made more efficient by working story by story, but that doesn't change the basic principle of getting a column index.)

dublove
dubloveAuthor
Legend
January 24, 2024

Hi~Peter Kahrel

This script is too good. It's working

I can only say that you are too great.

Thank you very, very much

Willi Adelberger
Community Expert
Community Expert
January 24, 2024

Look intzthe paragraph and character style panels. 

why do you not work with paragraph styles?

Why do you use additional returns to force space between paragraphs?