[この質問は以前にPowerShellTechnetフォーラムに投稿しましたが、回答はありません]
Windows XPのクイック起動設定を変更しようとしています(PowerShellを使用して有効/無効にします)。既存のVBScriptソリューションはレジストリまたはSendKeysのいずれかに依存しているため、これはUIAutomationを介してPowerShellで実行可能であると思いました。私の最初の試みは、変更が必要なチェックボックスを表すAutomationElementへの参照を取得することでした([コントロールパネル]->[タスクバーとスタートメニュー]->[タスクバー]タブ->[クイック起動の表示]チェックボックス)。スクリプトは次のとおりです。
[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
}
スクリプトはエラーになりませんが、予期しない結果を返します。
クイック起動を表示
ControlType.Pane
ControlType.CheckBoxの代わりに、スクリプトはAutomationElementをControlType.Paneとして認識します。同等の(少なくとも私はそう思う)C#コンソールアプリケーションは、期待される結果を返します。
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);
}
}
}
}
クイック起動を表示
ControlType.CheckBox
私は何が間違っているのですか?PowerShellスクリプトはISE(つまり、V2)から実行され、スクリプトとC#プログラムの両方が、アプレットが既に開いている/表示されていることを前提としています。(XP SP3)