Forum Discussion

mfoster711's avatar
mfoster711
Regular Contributor
10 years ago

How to check if Aliases name exists

Is there a method to determine if an Aliases name is valid name used in my Name Mapping file before trying to use it? 



Example:



I have code where I perform the following in VBScript:



VisibleProp = Eval(MyAliasesName & ".Visible")



MyAliasesName is a value passed into the function. As long as it is a valid Aliases name then this works but if it is not valid then I get "Unable to find the object...." error. I want to check for this error condition before trying to use the invalid Aliases name.
  • I had interpreted Mark's question to be:  "can I check if an entry is present in the Aliases map?"  without regard to the underlying presence in the NameMap or any existence in the running system.

    We can't use IsSupported() because it will return True regardless, so it becomes an exercise in traversing the Aliases tree.  I *think* this will do it....

    ' VBScript

    'Option Explicit
    ' ' propertyOf - return child object of 'obj' whose name is "prop", or Empty object if not found ' Function propertyOf(obj, prop)   Dim p, propColl, propItem, propName, value   Set propColl = aqObject.GetProperties(obj, True)   Set p = Nothing   Do While propColl.HasNext()     Set propItem = propColl.Next()     propName = propItem.Name     If aqString.Compare(propName, prop, true) = 0 Then       Set value = propItem.Value       Set p = value       Exit Do     End If   Loop   Set propertyOf = p End Function ' ' Check that the object chain "name" is in the Aliases map.  ' "name" starts with "Aliases".  Eg: "Aliases.notepad.wndNotepad" ' Sub check(name)   Dim arr, arrItem, obj, nm, propObj     arr = split(name, ".")   For Each arrItem In arr     If aqString.Compare(arrItem, "Aliases", true) = 0 Then       nm = "Aliases"       Set obj = Aliases     Else       nm = nm & "." & arrItem       Set propObj = propertyOf(obj, arrItem)       If propObj Is Nothing Then         Log.Message(nm & " is NOT present")         Exit For       Else         Log.Message(nm & " is present")         Set obj = propObj       End If     End If   Next End Sub Sub Test1   check("Aliases.notepad.wndNotepad")   check("Aliases.notepad.foo") End Sub
  • I had interpreted Mark's question to be:  "can I check if an entry is present in the Aliases map?"  without regard to the underlying presence in the NameMap or any existence in the running system.

    We can't use IsSupported() because it will return True regardless, so it becomes an exercise in traversing the Aliases tree.  I *think* this will do it....

    ' VBScript

    'Option Explicit
    ' ' propertyOf - return child object of 'obj' whose name is "prop", or Empty object if not found ' Function propertyOf(obj, prop)   Dim p, propColl, propItem, propName, value   Set propColl = aqObject.GetProperties(obj, True)   Set p = Nothing   Do While propColl.HasNext()     Set propItem = propColl.Next()     propName = propItem.Name     If aqString.Compare(propName, prop, true) = 0 Then       Set value = propItem.Value       Set p = value       Exit Do     End If   Loop   Set propertyOf = p End Function ' ' Check that the object chain "name" is in the Aliases map.  ' "name" starts with "Aliases".  Eg: "Aliases.notepad.wndNotepad" ' Sub check(name)   Dim arr, arrItem, obj, nm, propObj     arr = split(name, ".")   For Each arrItem In arr     If aqString.Compare(arrItem, "Aliases", true) = 0 Then       nm = "Aliases"       Set obj = Aliases     Else       nm = nm & "." & arrItem       Set propObj = propertyOf(obj, arrItem)       If propObj Is Nothing Then         Log.Message(nm & " is NOT present")         Exit For       Else         Log.Message(nm & " is present")         Set obj = propObj       End If     End If   Next End Sub Sub Test1   check("Aliases.notepad.wndNotepad")   check("Aliases.notepad.foo") End Sub
  • AlexKaras's avatar
    AlexKaras
    Champion Level 3
    Hi Mark,



    If I got your question right, it looks like you are looking for something like this (VBScript pseudocode; also assuming that MyAliasesName equals to something like "Aliases.Process.Window.Panel.Control"):

    arr = split(MyAliasesName, ".")

    For Each element In arr

      If ("Aliases" = element) Then

        Set obj = Eval(element)

      Else

        Set obj = obj.WaitAliasChild(element)

      End If

      If (Not obj.Exists) Then

        Call Log.Error(aqString.Format("Aliased object '%s' was not found for the '%s' name", element, MyAliasesName), element)

      End If

    Next

  • try this shortcut :




    var objName = Aliases.browser.page***.objname;



    type = aqObject.GetVarType(objName);



    if (objName.Exists)

    {

    }

    if (type == varDispatch)

    {

    //does not exist

    }


  • mfoster711's avatar
    mfoster711
    Regular Contributor
    Just to make sure I am clear.....I am not asking about to determine if the mapped object exists within the application. I know how to do that. I am wanting to know if the Aliases name I am trying to use is a valid name in the name mapping file. I am basically trying to determine if somebody might have typo'd the name.
  • mfoster711's avatar
    mfoster711
    Regular Contributor
    Joseph's solution worked perfect. Thanks!



    Dell, your solution would not work because you can't check the objName.exists property. This might fail with a valid Aliases name just because the field does not exist on the screen currently.



    Alexei, your solution also worked but it would cause an error on the obj.WaitAliasChild line. I added an On Error Resume Next before this line to catch the error and this worked. 



    Ultimately, Joseph's solution was exactly what I was looking for.



    Thanks all for your help!!!