UIAutomation Events on Powershell

I’m trying to listen UIAutomation events using Powershell, and wrote somethings like this.

$propChangeHandler= [System.Windows.Automation.AutomationPropertyChangedEventHandler]

# <summary>
# Adds a handler for property-changed event in particular, a change in the enabled state.
# </summary>
# <param name="element">The UI Automation element whose state is being monitored.</param>
function Subscribe-PropertyChange
{
param(([System.Windows.Automation.AutomationElement]$element))

[System.Windows.Automation.Automation]::AddAutomationPropertyChangedEventHandler($element,
        [System.Windows.Automation.TreeScope]::Element, 
        $propChangeHandler($OnPropertyChange),
        [System.Windows.Automation]::AutomationElement.IsEnabledProperty)
}

# <summary>
# Handler for property changes.
# </summary>
# <param name="src">The source whose properties changed.</param>
# <param name="e">Event arguments.</param>
$OnPropertyChange=
{
param([System.Object]$src, [System.Windows.Automation.AutomationPropertyChangedEventArgs]$e)
    [System.Windows.Automation.AutomationElement]$sourceElement = $src -as [System.Windows.Automation.AutomationElement]
    if ($e.Property -eq [System.Windows.Automation.AutomationElement]::IsEnabledProperty)
    {
        [System.Boolean]$enabled = $e.NewValue -as [System.Boolean]
        # TODO: Do something with the new value. 
        # The element that raised the event can be identified by its runtime ID property.
        Write-Host("{0} is Enable" -f $sourceElement.Current.Name)
    }
    else
    { 
        # TODO: Handle other property-changed events.
    }
}

function Unsubscribe-PropertyChange
{
param([System.Windows.Automation.AutomationElement]$element)
    if ($propChangeHandler -ne $null)
    {
        [System.Windows.Automation.Automation]::RemoveAutomationPropertyChangedEventHandler($element, $propChangeHandler)
    }
}

But I got a problem.

The AddautomationPropertyChangedEventHandler method will need a AutomationPropertyChangedEventHandler (with an eventHandler as parameter) as parameter, but both AutomationPropertyChangedEventHandler and eventHandler are functions.

That means function(function(function()))

(Please find the $propChangeHandler($OnPropertyChange) from the above code.)

Obviously writing it as $propChangeHandler($OnPropertyChange) is not correct. How can I revise?

What exactly isn’t working? The OnPropertyChange is a ScriptBlock, not a function, basically when the event is triggered call this script. When the event triggers, it will return an eventHandler object that has a src (source) and e (element) property that is being processed be the OnPropertyChange script. It looks like you call the Subscribe-PropertyChange with an element parameter to indicate what element to monitor.

It appears you cross-posted this here as well: https://answers.microsoft.com/en-us/windows/forum/all/uiautomation-events-on-powershell/4e636b5b-3fde-40e1-a400-06ab6f724aeb

[quote quote=229504]What exactly isn’t working? The OnPropertyChange is a ScriptBlock, not a function, basically when the event is triggered call this script. When the event triggers, it will return an eventHandler object that has a src (source) and e (element) property that is being processed be the OnPropertyChange script. It looks like you call the Subscribe-PropertyChange with an element parameter to indicate what element to monitor.

It appears you cross-posted this here as well: https://answers.microsoft.com/en-us/windows/forum/all/uiautomation-events-on-powershell/4e636b5b-3fde-40e1-a400-06ab6f724aeb

[/quote]

It get this syntax error:

 

  • $propChangeHandler($OnPropertyChange),
  • ~
    Missing ‘)’ in method call.

 

but when I remove ($OnPropertyChange), it report no syntax error.

Not sure why $propChangeHandler is set like that, but it’s odd to call variable like a function with params, try it like this:

[System.Windows.Automation.Automation]::AddAutomationPropertyChangedEventHandler($element,
        [System.Windows.Automation.TreeScope]::Element, 
        [System.Windows.Automation.AutomationPropertyChangedEventHandler]($OnPropertyChange),
        [System.Windows.Automation]::AutomationElement.IsEnabledProperty)