General XMetaL Discussion
XMetaL Community Forum › General XMetaL Discussion › I can’t get this macro to replace the text in an element
-
pszwec February 28, 2012 at 2:47 pm
I can’t get this macro to replace the text in an element
February 28, 2012 at 2:47 pmParticipants 3Replies 4Last Activity 10 years, 11 months agoDerek 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”); ]]>Derek Read February 28, 2012 at 6:34 pm
Reply to: I can’t get this macro to replace the text in an element
February 28, 2012 at 6:34 pmI 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.pszwec February 28, 2012 at 6:54 pm
Reply to: I can’t get this macro to replace the text in an element
February 28, 2012 at 6:54 pmThanks 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 'sXXXXXX Thanks,
Peter
Derek Read February 28, 2012 at 8:14 pm
Reply to: I can’t get this macro to replace the text in an element
February 28, 2012 at 8:14 pmStandard JScript string functions and a loop will give you that.
Insert this after rng.SelectContainerContents();
var oldtxt = rng.Text;
var newtxt = "";
for(i=0;inewtxt += "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.
Derek Read February 28, 2012 at 8:16 pm
Reply to: I can’t get this macro to replace the text in an element
February 28, 2012 at 8:16 pmIf 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.
-
AuthorPosts
- You must be logged in to reply to this topic.