General XMetaL Discussion

XMetaL Community Forum General XMetaL Discussion Old Code not working in Xmetal 6. Anomaly

  • russurquhart1

    Old Code not working in Xmetal 6. Anomaly

    Participants 2
    Replies 3
    Last Activity 10 years, 7 months ago

    Hi,

    We have the following macro defined to put a template of a table into a document:

    if (Selection.CanInsert(“BitRegister”)) {
            Selection.InsertElement (“BitRegister”);
    Documents.OpenTemplate (“C:\DocZone\Template\BitReg32_1r.xml”);
            ActiveDocument.ViewType = 1;
    Selection.SelectElement();
    Selection.MoveLeft();
    Selection.SelectContainerContents();
            Selection.Copy();
            ActiveDocument.Close();
    Selection.Paste();
      } else {
      Application.Alert(“BitRegister not allowed here.”);
      }
    ]]>

    The anomaly comes in, in that, we have three different document types: UG, DM, and DS. Previously, in Xmetal 5, this code worked to but a template table in allowed areas, in all of the document types.

    Now, in Xmetal 6, it works in the UG & DM document types, but in the DS type, it appears like it tries to work, but only an empty BitRegister element is inserted.

    To make things even fuzzier, i CAN manually paste this table into a DS document. Is there some restriction with the Selection.Paste() function?

    Any help is greatly appreciated!

    Thanks,

    Russ

    Reply

    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

    russurquhart1

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

    Hi Derek,

    Yeah, i am not the original author of this, but i seem to recall, when i was dabbling more with these, that the preferred method to do this sort of thing might be closer to your solution A that what was implemented.

    In either case, i implemented your A version, as it was quicker to test, and that seems to do the trick!

    I'm curious as to why the other way was acting up?

    Anyway thanks again for a working solution!

    Russ

    Reply

    Derek Read

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

    Yes, it is odd. Best thing to do in this case (if you need to know) is to debug it. If you have a debugger installed you can insert debugger; into your code to have it trigger the debugger, then hopefully your debugger supports functions that allow you to step through line by line and examine the results.

    Reply

  • You must be logged in to reply to this topic.

Lost Your Password?

Products
Downloads
Support