General XMetaL Discussion

XMetaL Community Forum General XMetaL Discussion Macro to select text within two entities

  • C4

    Macro to select text within two entities

    Participants 5
    Replies 6
    Last Activity 5 years, 5 months ago

    Hi,

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

    I am using XMetaL Author Essential 11.

    Thanks

    Reply

    Derek Read

    Reply to: Macro to select text within two entities

    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?

    Reply

    C4

    Reply to: Macro to select text within two entities

    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.

    Reply

    Derek Read

    Reply to: Macro to select text within two entities

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

    “some text”

    Which view do you need to do that in?

    Reply

    C4

    Reply to: Macro to select text within two entities

    Yes, that's it.

    We are searching in Tags on view.

    Thanks.

    Reply

    Derek Read

    Reply to: Macro to select text within two entities

    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);
    }

    Reply

    C4

    Reply to: Macro to select text within two entities

    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.

    Reply

  • You must be logged in to reply to this topic.

Lost Your Password?

Products
Downloads
Support