Pages: 1
Print
Author Topic: Script Example: Inserting XML fragments with macros  (Read 6234 times)
mag3737
XMetaL Evangelist
Administrator
Member

Posts: 114

I even use XMetaL to write my business letters.


« on: August 04, 2009, 05:10:13 PM »

Here 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.

USAGE NOTES:

 1. Place the attached macro file XM_example_insert_XML_fragments.mcr into your $XMDIR\Startup 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.

TECH NOTES:

 - 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.

SCRIPT NOTES:

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.

* XM_example_insert_XML_fragments.zip (1.49 KB - downloaded 507 times.)
Logged

Tom Magliery
JustSystems Canada, Inc.
nileshp
Member

Posts: 5


« Reply #1 on: September 25, 2012, 11:56:57 AM »

Is 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.
Logged
Pages: 1
Print
Jump to:  

email us