Home Forums General XMetaL Discussion Old Code not working in Xmetal 6. Anomaly Reply To: Old Code not working in Xmetal 6. Anomaly

Derek Read

Reply to: Old Code not working in Xmetal 6. Anomaly

That seems odd indeed. I'm trying to understand the logic of the script.

I would need to see your files to properly try to reproduce this and give an answer specific to your setup. However, I'm looking at your script and questioning some things (it may work as written but seems like the person that wrote it did not understand all of the APIs available to them).

The intention seems to be:
 1. Insert an element.
 2. Paste some predefined markup from a file stored on disk into that element.

The logic of your script is currently this:

 1. If I can insert then do it (and continue below), otherwise give the user an error.

…the next part gets a little confusing and I wonder why it was done this way:
 2. Open a file from disk (not sure why this is being opened as a template if the user will never do anything to it).
 3. Switch to TagsOn view.
 4. Select the element containing the cursor when the document is open. This might be the root or some other element. XMetaL puts the cursor inside the first element that allows text.
 5. Move the selection left. Context would help understand why, but I suspect you are moving to the root element?
     In that case MoveToDocumentStart() might have been a better choice, so maybe you aren't moving to the root.
 6. Assuming the selection is in the root then calling SelectContainerContents() would select everything inside the root.
    Otherwise, you are selecting everything inside some other element.
 7. Put what was just selected to the clipboard.
 8. Close the document, thereby returning focus to the document the user is working on.
 9. Paste what is on the clipboard inside .

I'm wondering if one of the following simplified processes might be better. In both cases they avoid needing to actually open a file for authoring in the editor in order to copy text out of it.

A)
1. Replace BitReg32_1r.xml with a similar file that *only* contains the text you wish to paste in.
Replace your script with the following:

if (Selection.CanInsert("BitRegister")) {
    Selection.InsertElement("BitRegister");
    var markupToPasteIn = Application.FileToString("C:\DocZone\Template\BitReg32_1r.xml");
    Selection.PasteString(markupToPasteIn);
 } else {
    Application.Alert("BitRegister not allowed here.");
 }

B)
1. Keep your BitReg32_1r.xml file as it is.
2. Use a similar script to mine, but one that will remove anything from the string that you do not want pasted in. This might be tricky depending on what is in the file. Some basic JScript string manipulation can be done though, in the case where you need to strip off an XML declaration, DOCTYPE declaration, etc (at this point I'm assuming the file contains stuff like that).

C)
Eliminate the need for the file altogether and simply hard code the markup you want inserted into the script. The main reason against doing this would be if you wish to allow people to alter the file stored on disk to make changes (rather than having to update the MCR file).

Another possible change would be to use Selection.FindInsertLocation(“BitRegister”) instead of CanInsert(). There may be a reason you use CanInsert() here, but if you use FindInsertLocation() then the user doesn't need to put their cursor in exactly the right spot, it will be found for them (if there is one).

Reply

Products
Downloads
Support