Skip to main content
dublove
Legend
Jan 24, 2024 9:23 AM UTC
Answered

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

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.

 

Peter Kahrel
Peter KahrelCorrect answer
Community Expert
Jan 24, 2024 3:35 PM UTC

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

18 replies

dublove
dubloveAuthor
Legend
Dec 17, 2024 12:43 PM UTC

Thank you very much

Translate
Community Expert
Dec 26, 2024 8:20 AM UTC

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

Translate
-Manan
Peter Kahrel
Community Expert
Community Expert
Dec 26, 2024 9:41 AM UTC

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 🙂

Translate
Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
Jan 24, 2024 3:35 PM UTC

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

Translate
dublove
dubloveAuthor
Legend
Jan 24, 2024 5:18 PM UTC

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

Translate
Willi Adelberger
Community Expert
Community Expert
Jan 24, 2024 10:15 AM UTC

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?

Translate