Home › Forums › General XMetaL Discussion › Macro to select text within two entities › Reply To: Macro to select text within two entities
Reply to: Macro to select text within two entities
May 5, 2017 at 12:13 amThis is a bit tricky because the entity is part of the DOM and not really part of the XML source or what you can actually see, so the usual Find.Execute won't work here, and you have to get a bit inventive.
I think something like this will probably get you close:
//XMetaL Script Language JScript:
if(ActiveDocument.ViewType == sqViewTagsOn) {
var ent1 = "ldquo";
var ent2 = "rdquo";
//find an ldquo
var rng1 = ActiveDocument.Range;
var rng2 = rng1.Duplicate;
rng1.GotoNext(0);
while(rng2.IsLessThan(rng1)) {
if(rng2.ContainerName == ".ENTREF") {
if(rng2.ContainerNode.nodeName == ent1) {
rng2.Select();
break;
}
}
rng2.GotoNext(0);
rng1.GotoNext(0);
}
//find an rdquo
var rng3 = ActiveDocument.Range;
var rng4 = rng3.Duplicate;
rng3.GotoNext(0);
while(rng4.IsLessThan(rng3)) {
if(rng4.ContainerName == ".ENTREF") {
if(rng4.ContainerNode.nodeName == ent2) {
rng4.MoveRight();
break;
}
}
rng4.GotoNext(0);
rng3.GotoNext(0);
}
//do one final check in case the entity is the last thing in the doc
//this seems necessary due to the way GotoNext works
if(rng3.ContainerName == ".ENTREF") {
if(rng3.ContainerNode.nodeName == ent2) {
rng4.MoveRight();
}
}
//extend the selection from the first ent (rng2) to the second ent (rng4)
Selection.ExtendTo(rng4);
}