Inspiring
October 2, 2024
Answered
How to Anchor a Text Frame to a Specific Character Style in InDesign Scripting
- October 2, 2024
- 1 reply
- 1190 views
Hi
I have a requirement to anchor a text frame with numbers in it to a text where a particular character style is applied. The text frame which needs to be anchored has a paragraph style and an object style applied to it.
I have attached a screenshot and and IDML file also.
Here is the code:
//First copy the text frame with object style applied to it.
//This frame will be anchored at the word/sentence with a particular character style
// Get the active document
var doc = app.activeDocument;
//Get the object style
var objectStyle = doc.objectStyles.itemByName("boxedObject");
//Change object style setting
objectStyle.anchoredObjectSettings.anchoredPosition = AnchorPosition.ANCHORED;
objectStyle.anchoredObjectSettings.anchorPoint = AnchorPoint.TOP_LEFT_ANCHOR;
objectStyle.anchoredObjectSettings.horizontalReferencePoint = AnchoredRelativeTo.ANCHOR_LOCATION;
//Reset findchange grep preferences
app.findGrepPreferences = app.changeGrepPreferences = null;
//find one or more words separated by a space
app.findGrepPreferences.findWhat="\\w+(?:\\s\\w+)*";
//Filter the search for a particular character style
app.findGrepPreferences.appliedCharacterStyle = "boxedul";
//Paste the copied textframe back
app.changeGrepPreferences.changeTo="~c$0";
//Execute the find change
var found = doc.changeGrep();
All is working fine except that I am unable to change the width of the anchored textframe as per the width of the text where a particular character style is appiled. I believe the screenshot attached will make my question more clear .
I am unable to write that last part of the code. It seems be way above my knowlege.
Thanks
<Title renamed by MOD>
