General XMetaL Discussion

XMetaL Community Forum General XMetaL Discussion Macro to search through attribute value

  • jshick

    Macro to search through attribute value

    Participants 6
    Replies 7
    Last Activity 5 years, 3 months ago

    I'm using Xmetal Author Enterprise 11. I need to fix a form that works in PlainTextView to work in TagsOnView. Basically, the user enters a reference number (e.g.: 101) and the form cycles through all elements that have an href of “r101”. I need to reprogram the script so that it will work in NormalView.

    I have the following macro which calls a form:

    var dlg = Application.CreateFormDlg(Application.Path + “\Forms\find-ref-callout.xft”);
    dlg.DoModal();
    dlg = null;
    ]]>

    The form has an EditBox and a Search button. The onClick event for the search button is associated with this function:

    Sub Button1_OnClick()
    findText = “ Selection.Find.Execute findText,””,””,,,,boolForward=False
    End Sub

    Thanks!
    Jeff

    Reply

    Derek Read

    Reply to: Macro to search through attribute value

    You might be able to use Find.Execute (the third option in that method, that you have set to be an empty string “”, allows you to limit search to within certain elements — check the Programmers Guide for its full syntax), but I don't think that's what you want.

    If your form just needs to move the active selection to the next occurrence of an element then you probably want to use:
    Selection.MoveToElement(“ref-callout”)

    If that doesn't point you in the right direction what is the exact goal of your script (in Tags On or Normal views)?

    Reply

    jshick

    Reply to: Macro to search through attribute value

    Alright, after some time away from this, I'm back. Perhaps there is no way for me to do what I'm looking for in a macro since it doesn't seem like I can even do it using the dialog box.

    If I bring up the “Find & Replace” dialog box (ctrl+F), there are 3 tabs at the top: Text, Element, Entity. “Text” will find PCDATA within an element. I'm actually trying to find the value (r101) of an attribute (href) in a specific element (ref-callout) when in Normal or TagsOn Views. The ref-callout element doesn't actually contain any PCDATA, only attributes.

    I can switch to plain text and just search for “r101” (but it doesn't seem like I am able to restrict by element in PlainTextView) and it will cycle through all of the occurrences of “r101”. This is not ideal as I do not want the content authors to switch into PlainTextView.

    Thanks,
    Jeff

    Reply

    Derek Read

    Reply to: Macro to search through attribute value

    There's almost always a way. Just need to define the problem in enough detail to understand what needs to be done.

    Maybe something like this is what you need.

    //XMetaL Script Language JScript:
    //name of element you are looking for
    var elemName = "ref-callout";
    //create a range to walk through the document
    var rng = ActiveDocument.Range;
    //move the range to the start of the document
    rng.MoveToDocumentStart();
    //for every element that matches the right element name...
    while(rng.MoveToElement(elemName)) {
    //...if that element has an @href with the value r101
    if(rng.ContainerAttribute("href") == "r101") {
    Application.Alert("found one...");
    //do whatever you need to do to this element here...
    }
    }

    Reply

    jshick

    Reply to: Macro to search through attribute value

    I'm getting the following error:

    Invalid character
    Source line: while(rng.MoveToElement(elemName)) {

    Reply

    Derek Read

    Reply to: Macro to search through attribute value

    Not sure where that would be coming from. Does it not say which character is invalid?

    Try pasting into a different editor?
    Stepping through with a debugger might give more information.

    Reply

    jshick

    Reply to: Macro to search through attribute value

    I copied into Notepad first to make sure it wasn't picking up any special characters from the HTML, then I copied from Notepad into the XMetal Form Layout Tool.

    The first error I get is “Expected statement” on the first line. It apparently doesn't like // being used to denote comments. I can use a ' at the beginning of each comment line and it seems to get along with that just fine.

    The second error I get is “Expected end of statement” on the line 3, the variable definition. It seems like it doesn't like a ; at the end of each line, so I removed those.

    The third error which I can't get passed is the “invalid character” on the “while” line.

    Reply

    Derek Read

    Reply to: Macro to search through attribute value

    It sounds like your XFT form is VBScript. To use my example unmodified you'd need to use it in a JScript XFT form. The scripting language is declared when you first create the form in the XFT editor (you can choose from any of the script engines installed on your machine).

    XFT forms (unlike MCR files) can only include one scripting language.

    If you are comfortable with JScript then you may wish to recreate your form, selecting JScript as the language. If you prefer VBScript then the code would need to be adjusted from JScript to VBScript.

    Reply

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

Lost Your Password?

Products
Downloads
Support