mfoster711
11 years agoRegular Contributor
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.
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