XMetaL Tips and Tricks
XMetaL Community Forum › XMetaL Tips and Tricks › Script Example: Inserting XML fragments with macros
-
mag3737 August 4, 2009 at 11:10 pm
Script Example: Inserting XML fragments with macros
August 4, 2009 at 11:10 pmParticipants 0Replies 1Last Activity 13 years, 7 months agoHere is a little bit of script to illustrate a few useful XMetaL APIs for pasting bits of XML code into your active document. I have tested this code with XMetaL 5.5, but it probably will work just fine with any version, older or newer.
[u]USAGE NOTES:[/u]
1. Place the attached macro file XM_example_insert_XML_fragments.mcr into your $XMDIRStartup folder.
2. Restart XMetaL.
3. The macros “Insert Fragment 1” (and 2 and 3) will be available on the Macros toolbar and Macros dialog.
4. See notes below about how to change the XML fragments that will be inserted.[u]TECH NOTES:[/u]
– The MCR file contains three XML fragments hard-coded as strings in the JScript.
– To change the fragments, edit the MCR file in any text editor. (MCR files are XML files, and XMetaL can open them, too.)
– Be mindful of the JScript string syntax, especially if your XML fragment contains quote marks or apostrophes!– Each fragment has a corresponding macro “Insert fragment N”.
– You do NOT need to edit these macros if you change your fragments.
– You DO need to add more macros if you add MORE fragments; specify the fragment number in the function call for insert_XML_fragment(N).
– You must restart XMetaL to reload the MCR file after you make changes.– The three XML fragments included in the MCR file are all DITA.
– You can include fragments of other types of XML, even different types in the same file.
– These are “application level” macros, which means that they will all be available no matter what type of document you have open (DITA or otherwise), or even if you have no documents open.
– If you run one of these Insert macros in an XML document of the wrong type, it will simply report that it could not find a place to paste the XML fragment.[u]SCRIPT NOTES:[/u]
The helper function insert_XML_fragment() contains the script code which is the purpose of this example. It uses XMetaL APIs to find a location in which a string can be pasted into the active document, and then pastes it there. The simple algorithm used is to look first at the current cursor location, and then look forward (down) through the rest of the document until a suitable (i.e. valid) paste location is found, or the end of the document is reached. There are ways to make this smarter, including cleverer use of the CanPaste() and string-pasting APIs for interpretation of the XML fragment to be pasted; looking both forwards and backwards in the document for a paste location; and prompting the user for the desired action if a location cannot be easily determined.
nileshp September 25, 2012 at 5:56 pm
Reply to: Script Example: Inserting XML fragments with macros
September 25, 2012 at 5:56 pmIs there a way we can flush the variable values after executing a macro? I used the code in this macro and modified it to insert conrefs. There are 4 variations of the conrefs and whichever variation i use the first, the subsequent actions on the macros that are executed only insert what was inserted the first time. It looks like the macro values are getting cached from the Array index or the rng.PasteString() function is using the cached value.
Sometimes when I run the Refresh Macros macro, the values are correctly inserted.
-
AuthorPosts
- You must be logged in to reply to this topic.