none
UIAutomation script not returning expected result

    Question

  • There was an interesting question  recently in The Official Scripting Guys forum (here) concerning Windows XP Quick Launch (how to enable / disable it using PowerShell). Existing VBScript solutions rely either on Registry or SendKeys, so I thought that this would be feasible in PowerShell through UIAutomation. My first attempt was just to get  the reference to the AutomationElement that represents the check box that needs to be changed (Control Panel -> Taskbar and Start Menu -> Taskbar tab -> Show Quick Launch check box). Here is the script:

    [void][System.Reflection.Assembly]::LoadWithPartialName("UIAutomationClient")
    [void][System.Reflection.Assembly]::LoadWithPartialName("UIAutomationTypes")
    
    $root = [Windows.Automation.AutomationElement]::RootElement
    
    $condition1 = New-Object Windows.Automation.PropertyCondition([Windows.Automation.AutomationElement]::NameProperty, 'Taskbar and Start Menu Properties')
    $properties = $root.FindFirst([Windows.Automation.TreeScope]::Descendants, $condition1)
    
    $condition2 = New-Object Windows.Automation.PropertyCondition([Windows.Automation.AutomationElement]::NameProperty, 'Show Quick Launch')
    
    $checkboxes = $properties.FindAll([Windows.Automation.TreeScope]::Descendants, $condition2)
    
    foreach($checkbox in $checkboxes)
    {
      $checkbox.Current.Name
      $checkbox.Current.ControlType.ProgrammaticName
    }

    The scripts doesn't error out, but it returns unexpected results:

    Show Quick Launch
    ControlType.Pane

    Instead of ControlType.CheckBox the script sees the AutomationElement as ControlType.Pane. The equivalent (at least I think so) C# console application returns the expected results:

    using System;
    using System.Windows.Automation;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          AutomationElement root = AutomationElement.RootElement;
          AutomationElement properties = root.FindFirst(
            TreeScope.Descendants,
            new PropertyCondition(AutomationElement.NameProperty,
              "Taskbar and Start Menu Properties"));
    
          AutomationElementCollection checkboxes = properties.FindAll(
            TreeScope.Descendants,
            new PropertyCondition(AutomationElement.NameProperty,
              "Show Quick Launch"));
    
          foreach (AutomationElement checkbox in checkboxes)
          {
            Console.WriteLine(checkbox.Current.Name);
            Console.WriteLine(checkbox.Current.ControlType.ProgrammaticName);
          }
        }
      }
    }
    

    Show Quick Launch
    ControlType.CheckBox

    What am I doing wrong? The PowerShell script is executed from the ISE (so V2) and both the script and the C# program assume that the applet is already open / visible. (XP SP3)

     

     


    Uros Calakovic
    Monday, September 13, 2010 1:55 PM