Pages: 1
Print
Author Topic: Macro to select text within two entities  (Read 626 times)
C4
Member

Posts: 10


« on: April 26, 2017, 06:18:57 PM »

Hi,

Is it possible to write a macro that can select text within two entities?

I am using XMetaL Author Essential 11.

Thanks
Logged
Derek Read
Program Manager (XMetaL)
Administrator
Member

Posts: 2502



WWW
« Reply #1 on: May 01, 2017, 06:43:41 PM »

Can you provide a bit more context?
Why specifically two entities? Is there something you need to do with two vs one?

When you say text does that mean the entity reference (something like &entityname; is what you want) or the content defined for that entity?

When you have that (or create a selection of some kind) what do you want to do with that?
What's the end goal?
« Last Edit: May 01, 2017, 06:46:42 PM by Derek Read » Logged
C4
Member

Posts: 10


« Reply #2 on: May 01, 2017, 09:46:42 PM »

What we need to do is find and highlight text between left double quotes “ and right double quotes ” including the quotes. The quotes are marked up as entities.

eg "find and highlight this text" but not this text.

Thanks.
« Last Edit: May 01, 2017, 09:58:06 PM by C4 » Logged
Derek Read
Program Manager (XMetaL)
Administrator
Member

Posts: 2502



WWW
« Reply #3 on: May 02, 2017, 02:52:58 PM »

So, what you really want to find and select is this?

“some text”

Which view do you need to do that in?
Logged
C4
Member

Posts: 10


« Reply #4 on: May 03, 2017, 07:23:45 PM »

Yes, that's it.

We are searching in Tags on view.

Thanks.
Logged
Derek Read
Program Manager (XMetaL)
Administrator
Member

Posts: 2502



WWW
« Reply #5 on: May 04, 2017, 06:13:54 PM »

This 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);
}
Logged
C4
Member

Posts: 10


« Reply #6 on: May 15, 2017, 06:10:11 PM »

That was exactly what we needed.

I changed the line rng4.MoveRight(); in the find an rdquo section to rng4.MoveLeft(); so it wouldn't select the rdquo.

Thanks so much for your help.
Logged
Pages: 1
Print
Jump to: