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');
Copy link to clipboard
Copied
Here's the script by Peter Kahrel from the forums
I use this script to label specific characters on different columns with ABC.
The same file was available for use a year ago.
Now suddenly it prompts an error.
Some scripts will not work after ID upgrade?
Help to see it. Thank you very much.
Initial address:
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];
}
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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 🙂
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Yep, that's what I should have done.