Pages: 1
Print
Author Topic: Script Example: Add Edit Attribute and Insert Element Functions to Context Menu  (Read 6850 times)
Derek Read
Program Manager (XMetaL)
Administrator
Member

Posts: 2443



WWW
« on: April 14, 2010, 06:50:53 PM »

Products:
XMetaL Author Enterprise 6.0.0.122

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.

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

Demo Code:
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.

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

Uninstalling:
1. Remove MCR file.
2. Restart XMetaL Author Enterprise if already running.

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

Usage:
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 <current element name>..."
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.

* demo_contextMenuElemListAttrInsp.zip (25.42 KB - downloaded 618 times.)
« Last Edit: April 14, 2010, 11:15:15 PM by Derek Read » Logged
bzebian
Member

Posts: 27


« Reply #1 on: July 02, 2010, 03:06:41 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.
Logged
mag3737
XMetaL Evangelist
Administrator
Member

Posts: 114

I even use XMetaL to write my business letters.


« Reply #2 on: August 11, 2010, 02:27:16 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.

* demo_contextMenuElemListAttrInsp.zip (25.74 KB - downloaded 429 times.)
« Last Edit: August 11, 2010, 02:55:05 PM by mag3737 » Logged

Tom Magliery
JustSystems Canada, Inc.
TravisBob
Member

Posts: 1

Дешёвые авиабилеты по России


WWW
« Reply #3 on: July 22, 2012, 07:20:24 AM »

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?
Logged

С
Pages: 1
Print
Jump to: