Skip to main content
Participant
February 6, 2014
Answered

How can I autorun a script that will process an XML file on open?

  • February 6, 2014
  • 1 reply
  • 703 views

I am using Frame 11. I have a registered script that looks for event notification Constants.FA_Note_PostOpenXML. When this event fires, the script is supposed to examine the root element (so I only edit appropriate XML files), then make some edits to the file. Specifically, I want to be able to delete empty pages, remove room for side heads, and fix table formatting (such as left indent, which I can't directly access, apparently, in the EDD or R/W rules). The snippet of code that starts doing the work follows:


Notification(Constants.FA_Note_PostOpenXML,true);

function Notify(note, object, sparam, iparam) {

    switch (note) {

        case Constants.FA_Note_PostOpenXML:

            doTheWork();

            break;

            }

}

function doTheWork() {

    var doc, flow, root, elemName, mPageAttrib, topicElem, topicElemName, allAttribs;

    doc = app.ActiveDoc;

    flow = doc.MainFlowInDoc;

    root = flow.HighestLevelElement; // will always get something even if unstructured document

    while(root.ObjectValid()) { //only do something for structured docs

        elemName = getElementName(root);

        ...more code to do stuff....

    }

}

I then check the root element name, and if it matches, I make some changes. This code works fine when run manually. But, when run as a registered script, the app.ActiveDoc object is invalid, and there is no structure to edit. (I added debug line if(!doc.ObjectValid()){alert("not valid");}, which is not included above.) It appears that the Constants.FA_Note_PostOpenXML event fires as soon as the XML file is opened, but BEFORE Frame actually reads it.

Does anyone have any recommendations on how to work around this? Is there another event I could use instead of Constants.FA_Note_PostOpenXML? Is there any other way to automatically manipulate an XML file when it loads?

Thanks in advance

This topic has been closed for replies.
Correct answer frameexpert

The Notify event receives four parameters: note, object, sparam, and iparam. For the event you are using, object should be the document object of the FrameMaker document being opened. So, you should be able to use this:

doTheWork(object);

Make sure you update your doTheWork function to receive the Doc object:

function doTheWork(doc) {

    var flow, root, elemName, mPageAttrib, topicElem, topicElemName, allAttribs;

    flow = doc.MainFlowInDoc;

    root = flow.HighestLevelElement; // will always get something even if unstructured document

    while(root.ObjectValid()) { //only do something for structured docs

        elemName = getElementName(root);

        ...more code to do stuff....

    }

}

-Rick

1 reply

frameexpert
Community Expert
frameexpertCommunity ExpertCorrect answer
Community Expert
February 6, 2014

The Notify event receives four parameters: note, object, sparam, and iparam. For the event you are using, object should be the document object of the FrameMaker document being opened. So, you should be able to use this:

doTheWork(object);

Make sure you update your doTheWork function to receive the Doc object:

function doTheWork(doc) {

    var flow, root, elemName, mPageAttrib, topicElem, topicElemName, allAttribs;

    flow = doc.MainFlowInDoc;

    root = flow.HighestLevelElement; // will always get something even if unstructured document

    while(root.ObjectValid()) { //only do something for structured docs

        elemName = getElementName(root);

        ...more code to do stuff....

    }

}

-Rick

www.frameexpert.com
Participant
February 6, 2014

Thanks! That did it.