General XMetaL Discussion

XMetaL Community Forum General XMetaL Discussion I can’t get this macro to replace the text in an element

  • pszwec

    I can’t get this macro to replace the text in an element

    Participants 3
    Replies 4
    Last Activity 10 years, 7 months ago

    Derek sent me this macro years ago to delete some elements. It works great. Now I've been asked to keep the elements but modify the text to be generic. I get a warning that says “The paste operation cannot be completed even with rules checking turned off.” We have xMetal Author 5.5. I commented the delete command and added the selectcontainercontents and typetext lines above it.

    Thanks,

    Peter

    // Peter Szwec 2/2012
    // This macro replaces some of our HOLC data in meta tags with generic text var rng = ActiveDocument.Range;
    rng.MoveToDocumentStart(); while(rng.MoveToElement()) {      var elem = rng.ContainerName        if (elem==”holc-attorney” || elem==”version-creator”|| elem==”holc-creator”){               rng.SelectElement();
    rng.SelectContainerContents(); rng.typetext(” holc text”);
                 // rng.Delete();       } }
    var msg = “”;
    Application.MessageBox(“HOLC Tags have been removed”,64,”StripoutholcComp Macro”); ]]>

    Reply

    Derek Read

    Reply to: I can’t get this macro to replace the text in an element

    I think I understand what you want.
    If my assumption is correct you should only need to delete this line:

    rng.SelectElement();

    As it is now, the script is doing this:
    1) Moving just inside each of the named elements
    2) Selecting the entire element.
    3) Selecting the content of the element that is containing that element.

    You want it to do this:
    1) Move to the named element.
    2) Select everything inside it.
    3) Type over whatever is selected with some text.

    Reply

    pszwec

    Reply to: I can’t get this macro to replace the text in an element

    Thanks Derek, that was it.

    The only this better would be if I replaced the text with the same number of charaters. Is there a way to count text length?  So if the element was peters then I would replace it with  6 XXXXXX 's
    XXXXXX

    Thanks,

    Peter

    Reply

    Derek Read

    Reply to: I can’t get this macro to replace the text in an element

    Standard JScript string functions and a loop will give you that.

    Insert this after rng.SelectContainerContents();

    var oldtxt = rng.Text;
    var newtxt = "";
    for(i=0;i newtxt += "x";
    }

    Then replace the current TypeText with this:
    rng.TypeText(newtxt);

    Note that the above assumes the element does not contain anything other than text (ie: no child elements). To handle that case would require quite a bit more logic, and probably different logic using DOM or something else would make things easier to write (for me) but harder to understand.

    If you use change tracking at your company and the element might contain a change tracking PI then you might use rng.TextWithRM instead of rng.Text.

    Reply

    Derek Read

    Reply to: I can’t get this macro to replace the text in an element

    If this is meant to be something that the author will type over (the xxxxx part) you might want to insert a XMetaL Replace-Text processing instruction instead.

    Reply

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

Lost Your Password?

Products
Downloads
Support