Trying to understand element selection limitations
I have created an ExtendScript which is used to install structured application definitions the first time that a script is run. Here's what it does:
- Opens a copy of a structapps.fm file that contains the required XML application definitions.
- Creates a backup of the local structapps file.
- Opens the local structapps file.
- Delete any XML applications with names that match the new XML apps.
- Copies the new XML apps into the local structapps file.
- Reads the structapps file using fmDispatcher, saves and closes the files.
This all works very well up to a point. Where I have a problem is with the insertion point for the copied XML applications. I want to insert them directly after the <Version> element.

I can do that when there are other following <XMLApplication> elements. However if there are no following elements it inserts the copied elements before the <Version> element which is invalid. Here's the code:
function copyXApps(localStrappDoc, sourceStrappDoc)
{
var copyApps, ser = new ElementRange(), ter = new ElementRange(), copyApps, target,
sourceElem = sourceStrappDoc.MainFlowInDoc.HighestLevelElement.FirstChildElement.NextSiblingElement;
target = localStrappDoc.MainFlowInDoc.HighestLevelElement.FirstChildElement.NextSiblingElement;
if(sourceElem === null || sourceElem.ObjectValid() === false) return;
ser.beg.parent = ser.end.parent = sourceElem.ParentElement;
ser.beg.child = sourceElem;
ser.end.child = sourceElem.ParentElement.LastChildElement;
ser.end.parent = sourceElem.ParentElement;
ser.beg.offset = 0;
ser.end.offset = 0;
//Set the document element selection.
sourceStrappDoc.ElementSelection = ser;
copyApps = sourceStrappDoc.Copy (0);
if (copyApps === Constants.FE_Success){
ter.beg.parent = ter.end.parent = target.ParentElement;
ter.beg.child = ter.end.child = target;
ter.beg.offset = ter.end.offset = 0;
//Set insertion point for copied Xapps, then paste:
localStrappDoc.ElementSelection = ter;
pasteApps = localStrappDoc.Paste (0);
return pasteApps;
}
}
There's also a problem with the selection code which selects all apps except the last one despite using LastChildElement. I seem to be misunderstanding something here.
Thanks for any help.
Ian
