Forum Discussion
AlexKaras
14 years agoChampion Level 3
Hi Peter,
The following code was posted by Automated's Support a long time ago when there was no WaitAliasedChild method in the Aliases object.
The code may not work now, but maybe it might inspire you with some ideas...
============================
The following script gets an object by its full name from the Aliases tree. I hope you will find the code useful:
[JScript]
function Test()
{
var obj;
obj = WaitAliasedObject("Aliases.Sys.MainEdit", 5000);
if (!obj.Exists)
Log.Error("Object was not found");
}
function WaitAliasedObject(alias, timeout)
{
var mappedName;
mappedName = GetMappedNameByAlias(alias);
if (mappedName == "") {
Log.Warning("A mapped name for the " + alias + " was not found");
return Utils.CreateStubObject();
}
return WaitObjectByMappedPath(Utils.CreateStubObject(), mappedName, timeout);
}
function WaitObjectByMappedPath(parentObj, path, timeout)
{
var dotPosition, currentItemName, lowPath, startTime, childObj, newTimeout;
if (path == "")
return Utils.CreateStubObject();
dotPosition = path.indexOf(".");
if (dotPosition < 0) {
currentItemName = path;
lowPath = "";
}
else {
currentItemName = path.substr(0, dotPosition);
lowPath = path.substr(dotPosition + 1);
}
startTime = Win32API.GetTickCount();
if (!parentObj.Exists) {
dotPosition = lowPath.indexOf(".");
if (dotPosition < 0)
return Utils.CreateStubObject();
else {
currentItemName = currentItemName + "." + lowPath.substr(0, dotPosition);
lowPath = lowPath.substr(dotPosition + 1);
}
childObj = eval(currentItemName);
}
else
childObj = parentObj.WaitNamedChild(currentItemName, timeout);
if (!childObj.Exists)
return Utils.CreateStubObject();
if (lowPath == "")
return childObj;
else {
newTimeout = timeout - (Win32API.GetTickCount() - startTime);
if (newTimeout < 0)
return Utils.CreateStubObject();
else
return WaitObjectByMappedPath(childObj, lowPath, timeout);
}
}
function GetMappedNameByAlias(alias)
{
var i, tcNM, nmFilePath, items, logicalNodes, aliasedNode, mappedNode, mappedName;
nmFilePath = Project.Path + "NameMapping\\NameMapping.tcNM";
tcNM = Sys.OleObject("Msxml2.DOMDocument.4.0");
tcNM.async = false;
if (!tcNM.load(nmFilePath)) {
Log.Warning("Cannot load the " + nmFilePath + " NameMapping file");
return "";
}
items = tcNM.selectNodes("//Node[@name='logical nodes']");
if (items.length != 1) {
Log.Warning("An invalid NameMapping file");
return "";
}
logicalNodes = items.item(0);
aliasedNode = GetAliasNode(logicalNodes, alias);
if (aliasedNode == null) {
Log.Warning("The " + alias + " alias was not found");
return "";
}
items = aliasedNode.selectNodes("Prp[@name='owner' and @value]");
if (items.length != 1) {
Log.Warning("An invalid NameMapping file");
return "";
}
return GetMappedNameByID(logicalNodes, items.item(0).attributes.getNamedItem(
"value").nodeValue);
}
function GetMappedNameByID(tcNM, objectID)
{
var items, nmData, nmNode, mappedName;
items = tcNM.selectNodes("//Node[@name='data']");
if (items.length != 1) {
Log.Warning("An invalid NameMapping file");
return "";
}
nmData = items.item(0);
items = nmData.selectNodes("//Node[@name='" + objectID.toLowerCase() + "']");
if (items.length != 1) {
Log.Warning("An invalid NameMapping file");
return "";
}
nmNode = items.item(0);
mappedName = "";
do {
if (mappedName == "")
mappedName = nmNode.selectSingleNode("Prp[@name='name' and @value]").attributes.getNamedItem("value").nodeValue;
else
mappedName = nmNode.selectSingleNode("Prp[@name='name' and @value]").attributes.getNamedItem("value").nodeValue + "." + mappedName;
nmNode = nmNode.parentNode.parentNode;
} while(nmNode.attributes.getNamedItem("name").nodeValue != "data");
return "NameMapping." + mappedName;
}
function GetAliasNode(parentNode, path)
{
var dotPosition, currentItemName, lowPath, items, aliasedNode;
if (path == "")
return null;
dotPosition = path.indexOf(".");
if (dotPosition < 0) {
currentItemName = path;
lowPath = "";
}
else {
currentItemName = path.substr(0, dotPosition);
lowPath = path.substr(dotPosition + 1);
}
items = parentNode.selectNodes("Prp[@name='name' and @value='" + currentItemName + "']");
if (items.length > 0) {
if (lowPath == "")
return parentNode;
items = parentNode.selectNodes("Node[@name]");
for(i = 0; i < items.length; i++) {
aliasedNode = GetAliasNode(items.item(i), lowPath);
if (aliasedNode != null)
return aliasedNode;
}
}
return null;
}
============================
The following code was posted by Automated's Support a long time ago when there was no WaitAliasedChild method in the Aliases object.
The code may not work now, but maybe it might inspire you with some ideas...
============================
The following script gets an object by its full name from the Aliases tree. I hope you will find the code useful:
[JScript]
function Test()
{
var obj;
obj = WaitAliasedObject("Aliases.Sys.MainEdit", 5000);
if (!obj.Exists)
Log.Error("Object was not found");
}
function WaitAliasedObject(alias, timeout)
{
var mappedName;
mappedName = GetMappedNameByAlias(alias);
if (mappedName == "") {
Log.Warning("A mapped name for the " + alias + " was not found");
return Utils.CreateStubObject();
}
return WaitObjectByMappedPath(Utils.CreateStubObject(), mappedName, timeout);
}
function WaitObjectByMappedPath(parentObj, path, timeout)
{
var dotPosition, currentItemName, lowPath, startTime, childObj, newTimeout;
if (path == "")
return Utils.CreateStubObject();
dotPosition = path.indexOf(".");
if (dotPosition < 0) {
currentItemName = path;
lowPath = "";
}
else {
currentItemName = path.substr(0, dotPosition);
lowPath = path.substr(dotPosition + 1);
}
startTime = Win32API.GetTickCount();
if (!parentObj.Exists) {
dotPosition = lowPath.indexOf(".");
if (dotPosition < 0)
return Utils.CreateStubObject();
else {
currentItemName = currentItemName + "." + lowPath.substr(0, dotPosition);
lowPath = lowPath.substr(dotPosition + 1);
}
childObj = eval(currentItemName);
}
else
childObj = parentObj.WaitNamedChild(currentItemName, timeout);
if (!childObj.Exists)
return Utils.CreateStubObject();
if (lowPath == "")
return childObj;
else {
newTimeout = timeout - (Win32API.GetTickCount() - startTime);
if (newTimeout < 0)
return Utils.CreateStubObject();
else
return WaitObjectByMappedPath(childObj, lowPath, timeout);
}
}
function GetMappedNameByAlias(alias)
{
var i, tcNM, nmFilePath, items, logicalNodes, aliasedNode, mappedNode, mappedName;
nmFilePath = Project.Path + "NameMapping\\NameMapping.tcNM";
tcNM = Sys.OleObject("Msxml2.DOMDocument.4.0");
tcNM.async = false;
if (!tcNM.load(nmFilePath)) {
Log.Warning("Cannot load the " + nmFilePath + " NameMapping file");
return "";
}
items = tcNM.selectNodes("//Node[@name='logical nodes']");
if (items.length != 1) {
Log.Warning("An invalid NameMapping file");
return "";
}
logicalNodes = items.item(0);
aliasedNode = GetAliasNode(logicalNodes, alias);
if (aliasedNode == null) {
Log.Warning("The " + alias + " alias was not found");
return "";
}
items = aliasedNode.selectNodes("Prp[@name='owner' and @value]");
if (items.length != 1) {
Log.Warning("An invalid NameMapping file");
return "";
}
return GetMappedNameByID(logicalNodes, items.item(0).attributes.getNamedItem(
"value").nodeValue);
}
function GetMappedNameByID(tcNM, objectID)
{
var items, nmData, nmNode, mappedName;
items = tcNM.selectNodes("//Node[@name='data']");
if (items.length != 1) {
Log.Warning("An invalid NameMapping file");
return "";
}
nmData = items.item(0);
items = nmData.selectNodes("//Node[@name='" + objectID.toLowerCase() + "']");
if (items.length != 1) {
Log.Warning("An invalid NameMapping file");
return "";
}
nmNode = items.item(0);
mappedName = "";
do {
if (mappedName == "")
mappedName = nmNode.selectSingleNode("Prp[@name='name' and @value]").attributes.getNamedItem("value").nodeValue;
else
mappedName = nmNode.selectSingleNode("Prp[@name='name' and @value]").attributes.getNamedItem("value").nodeValue + "." + mappedName;
nmNode = nmNode.parentNode.parentNode;
} while(nmNode.attributes.getNamedItem("name").nodeValue != "data");
return "NameMapping." + mappedName;
}
function GetAliasNode(parentNode, path)
{
var dotPosition, currentItemName, lowPath, items, aliasedNode;
if (path == "")
return null;
dotPosition = path.indexOf(".");
if (dotPosition < 0) {
currentItemName = path;
lowPath = "";
}
else {
currentItemName = path.substr(0, dotPosition);
lowPath = path.substr(dotPosition + 1);
}
items = parentNode.selectNodes("Prp[@name='name' and @value='" + currentItemName + "']");
if (items.length > 0) {
if (lowPath == "")
return parentNode;
items = parentNode.selectNodes("Node[@name]");
for(i = 0; i < items.length; i++) {
aliasedNode = GetAliasNode(items.item(i), lowPath);
if (aliasedNode != null)
return aliasedNode;
}
}
return null;
}
============================
Related Content
- 7 years ago
- 12 years ago
- 12 years ago
Recent Discussions
- 23 minutes ago
- 54 minutes ago