Copy link to clipboard
Copied
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.
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.
...
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.)
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Hello, Peter Kahrel.
This script is now suggesting an error.
The same file that worked a year ago works using it.
Help to see it. Thank you very much.
Copy link to clipboard
Copied
Do the four referenced character styles (A, B, C, AAA) still exist?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
You are wrong: character style AAA is not available. Rename @ to AAA and the script runs as expected.
Copy link to clipboard
Copied
God, it's been so long since I've used it that I've memorized it as AAA being a paragraph style.
Also didn't observe that sometime the script had actually run silently.
I forgot, I was waiting for it to pop up the dialog ......
I'll make a note of that.
Thank you so much, you're genuinely great.
Copy link to clipboard
Copied
Is it possible to convert only paragraph styles and no character styles?
Copy link to clipboard
Copied
Sure. Replace these two lines:
app.findGrepPreferences.appliedCharacterStyle
= app.activeDocument.characterStyles.item ('AAA');
with
app.findGrepPreferences.appliedParagraphStyle
= app.activeDocument.paragraphStyles.item ('AAA');