Pages: 1
Print
Author Topic: Macro to search through attribute value  (Read 149 times)
jshick
Member

Posts: 3


« on: June 09, 2017, 11:01:23 AM »

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 <ref-callout> 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:

<MACRO name="find-ref-callout" key="" lang="JScript" hide="true"><![CDATA[
var dlg = Application.CreateFormDlg(Application.Path + "\\Forms\\find-ref-callout.xft");
dlg.DoModal();
dlg = null;
]]></MACRO>

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 = "<ref-callout href=""r" + EditBox1.Text + """"
   Selection.Find.Execute findText,"","",,,,boolForward=False
End Sub

Thanks!
Jeff

Logged
Derek Read
Program Manager (XMetaL)
Administrator
Member

Posts: 2481



WWW
« Reply #1 on: June 09, 2017, 12:04:25 PM »

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)?
« Last Edit: June 09, 2017, 12:25:31 PM by Derek Read » Logged
jshick
Member

Posts: 3


« Reply #2 on: July 26, 2017, 10:55:20 AM »

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
Logged
Derek Read
Program Manager (XMetaL)
Administrator
Member

Posts: 2481



WWW
« Reply #3 on: July 26, 2017, 01:55:47 PM »

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...
   }
}
Logged
jshick
Member

Posts: 3


« Reply #4 on: July 26, 2017, 02:44:10 PM »

I'm getting the following error:

Invalid character
Source line: while(rng.MoveToElement(elemName)) {
Logged
Derek Read
Program Manager (XMetaL)
Administrator
Member

Posts: 2481



WWW
« Reply #5 on: July 26, 2017, 04:47:55 PM »

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.
Logged
Pages: 1
Print
Jump to:  

email us