Ok, so i had my web programmer create this script for me, though he has no experience with inDesign scripting!
The script looks for the X position of the word proceeding the text anchor and updates the anchored object offset to center it above the word.
If anyone has any suggestions on simplifying the script or making it run faster, please let me know.
Here is the script:
main();
function main(){
//Make certain that user interaction (display of dialogs, etc.) is turned on.
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
if(app.documents.length != 0){
if(app.selection.length != 0){
//Get the first item in the selection.
var mySelection = app.selection[0];
//Process the selection. If text or a text frame is
//selected, do something; otherwise, do nothing.
switch(mySelection.constructor.name){
case “Text”:
case “InsertionPoint”:
case “Character”:
case “Word”:
case “Line”:
case “TextStyleRange”:
case “Paragraph”:
case “TextColumn”:
case “TextFrame”:
var story = mySelection.parentStory;
processStory(story);
break;
default:
alert(“Please select some text or a text frame and try again.”);
}
}
else{
alert(“Please select some text or a text frame and try again.”);
}
}
else{
alert(“Please open a document and try again.”);
}
return ‘done’;
}
function processStory(story) {
story.recompose()
for (var iLine = 0; iLine < story.lines.length; iLine++) {
//if (iLine > 0) break;
var sLine = story.lines[iLine];
//alert(sLine.characters.length);
sLine.showText();
processLine(sLine);
//$.writeln(sLine.characters.length);
}
}
function processLine(sLine) {
var iStart = 0, iEnd = 0, bGetStart = true, lEndOffset = 0;
var reIgnore = new RegExp(‘[ ,\.: ]’);
for (var iChar = 0; iChar < sLine.characters.length; iChar++) {
//if (iChar > 10) { break; }
var c = sLine.characters[iChar];
if (reIgnore.test(c.contents)) {
if (bGetStart) {
//do nothing, just ignore character
} else {
// add to offset
lEndOffset-= (c.endHorizontalOffset – c.horizontalOffset); //using – because we are going right to left and the endHorizontalOffset is less than the horizontalOffset
}
} else {
if (bGetStart) {
iStart = c.horizontalOffset;
bGetStart = false;
}
//$.writeln(c.endHorizontalOffset);
// $.writeln(iChar + ‘: ‘ + c.contents);
if (c.pageItems.length>0) {
var pi = c.pageItems[0];
if (pi.hasOwnProperty(‘anchoredObjectSettings’)) { //found anchor character
pi.anchoredObjectSettings.anchorXoffset = lEndOffset + ((iStart-iEnd)/2)
bGetStart = true;
lEndOffset = 0;
}
} else {
iEnd = c.endHorizontalOffset;
lEndOffset = 0; //reset offset if we find a regular character after an ignore character before the next anchor
}
}
}
}