XMetaL Tips and Tricks
Derek Read April 15, 2010 at 12:50 am
Script Example: Add Edit Attribute and Insert Element Functions to Context MenuApril 15, 2010 at 12:50 amParticipants 2Replies 3Last Activity 12 years, 5 months ago
XMetaL Author Enterprise 188.8.131.52
Should be possible to run on much older versions but not tested. For really old versions the Application-level event macro called “On_Application_Document_Context_Menu” will not exist. In this case you may adjust this to use “On_Context_Menu” and integrate the solution with your document-level customization. None of the other APIs used here are new and have been around for a very long time.
Tested with various DTD-based document types, including DITA, DocBook 5 and the Journalist demo DTD. Specifically not tested with XSD Schemas, though it may function in many cases except possibly for namespaced elements or attributes, for this some adjustments might need to be made as some APIs used here are not “namespace aware”.
Although I have tested this with our DITA functionality many users might find it confusing to have these additional items on the context menu as there are already options listed there for editing attributes and inserting elements. This was initially designed more as a demo to adapt and take ideas from rather than to solve a specific author's needs by dropping it into an installation and using it just as it is.
1. Allow authors to access and change attribute values from the context menu, avoiding the need to open and interact with the Attribute Inspector. Otherwise, the functionality may be similar, with the added benefit that you have more screen real-estate displaying your document.
2. Allow authors to insert elements using the context menu. Two functions are provided. One that lists all elements allowed at the current selection (same as the Element List) and one that lists the elements allowed immediately following the current element. In both cases selecting one of the listed elements inserts it.
3. Demonstrate a method for retrieving which item in the context menu was selected (no API is provided for that) and passing that value into a macro (also not normally possible). This demo does not actually pass real values, but instead uses fixed known positions in the context menu (because the items are also built at the same time in the correct order) to obtain a number which is then used to call a macro which in turn calls a function and passes the value to that function. I don't know that this is the “best” way to do this, however, it is the only method I can think of.
XMetaL Author does not support passing parameters to macro. It also does not return a value you can query based on the item selected on the context menu, you may only assign an OnAction value when defining the context menu item. This demo shows how having at least as many macros as there are options the user might allows us to work around that with some fairly tricky code. The good thing is that they can all call the same function and just pass a different value into it that the function can use.
Having so many macros loaded should not cause any issues (in theory), however, if you know the element with the largest number of elements / attributes / enumerated values has less than N values you could remove all the extras if you like.
Before using this script please read the notes and comments in the MCR file (which also includes some legal stuff). Basically, this code is provided as a demo and should be treated as if it were completely untested. I have tested it as best I can, but it has not gone through our regular rigorous test process. You may also wish to adapt this code by altering the script logic itself as you may find that the functionality does not meet the exact needs of your end users, etc.
Please also do not use this script without the permission of the people that maintain your XMetaL installation (if that isn't you). Although the possibility is low given the way I have coded this it could conflict with special customizations or scripts, 3rd party tools or plug-ins, a specific work-flow they have set up and wish you to follow, or any number of other things I cannot even guess at. I would recommend telling them about your wish to have something like this and let them integrate it and test it for you.
1. Unzip attached ZIP file.
2. Place MCR file in the Startup folder of your XMetaL Author Enterprise installation.
3. Restart XMetaL Author Enterprise if already running.
1. Remove MCR file.
2. Restart XMetaL Author Enterprise if already running.
Nothing special is required to run these macros as they do not use anything other than the Windows JScript engine and standard XMetaL Author Enterprise 6.0 APIs.
Warning: Do not use this script with real content until you are satisfied it passes your testing.
1. Open or create a new test document.
2. In either TagsOn or Normal view place cursor inside an element or make a selection then right click or press the “Context Menu” key (special Windows keyboards) or press Shift+F10. Two new menu items should be visible on the context menu called “Insert Element…” and “Attributes for
3. Select either option and play around with it in various situations.
For Legalese, Modifications, Extending Code, and Known Limitations:
See comments in the MCR file.bzebian July 2, 2010 at 9:06 pm
Reply to: Script Example: Add Edit Attribute and Insert Element Functions to Context MenuJuly 2, 2010 at 9:06 pm
I've found with XMetaL 5.1 that when you click Cancel on the Enter Value for Attribute Dialog, it deletes the attribute & its value from the XML, even if you left the value as is.mag3737 August 11, 2010 at 8:27 pm
Reply to: Script Example: Add Edit Attribute and Insert Element Functions to Context MenuAugust 11, 2010 at 8:27 pm
I updated this sample code to address the issue mentioned by bzebian. It uses an XFT form instead of the builtin “Prompt()” API so that it can detect which button was clicked.
Usage instructions and all other information is the same as before except there are now two files that you must place in the Startup folder together: demo_contextMenuElemListAttrInsp.mcr and Edit_attribute.xft.
I tested this XFT form with XMetaL 6.0.1.030, but I believe it should work with previous versions going years back. If anyone has problems, please let us know.
One remaining nuisance is that I can't see a way to “pre-select” an existing attribute value when it is placed into the form. Instead, the cursor goes at the beginning of the field. I suspect this is a limitation of XFT, but if anyone knows a trick, I'd be interested to hear it.TravisBob July 22, 2012 at 1:20 pm
Reply to: Script Example: Add Edit Attribute and Insert Element Functions to Context MenuJuly 22, 2012 at 1:20 pm
Thats what I needed to know. My function will only be the equation I suppose, and I have to use the for statement to assign each element of my amount array within the main function when i call the extend function, right?
- You must be logged in to reply to this topic.