General XMetaL Discussion
XMetaL Community Forum › General XMetaL Discussion › Macro to search through attribute value
-
jshick June 9, 2017 at 5:01 pm
Macro to search through attribute value
June 9, 2017 at 5:01 pmParticipants 6Replies 7Last Activity 5 years, 8 months agoI'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 SubThanks!
JeffDerek Read June 9, 2017 at 6:04 pm
Reply to: Macro to search through attribute value
June 9, 2017 at 6:04 pmYou 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)?
jshick July 26, 2017 at 4:55 pm
Reply to: Macro to search through attribute value
July 26, 2017 at 4:55 pmAlright, 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,
JeffDerek Read July 26, 2017 at 7:55 pm
Reply to: Macro to search through attribute value
July 26, 2017 at 7:55 pmThere'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...
}
}jshick July 26, 2017 at 8:44 pm
Reply to: Macro to search through attribute value
July 26, 2017 at 8:44 pmI'm getting the following error:
Invalid character
Source line: while(rng.MoveToElement(elemName)) {Derek Read July 26, 2017 at 10:47 pm
Reply to: Macro to search through attribute value
July 26, 2017 at 10:47 pmNot 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.jshick July 27, 2017 at 7:51 pm
Reply to: Macro to search through attribute value
July 27, 2017 at 7:51 pmI 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.
Derek Read August 1, 2017 at 11:24 pm
Reply to: Macro to search through attribute value
August 1, 2017 at 11:24 pmIt 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.
-
AuthorPosts
- You must be logged in to reply to this topic.