
Photo by
cassetteboy
PowerShell: Anyway, I record all the code I used.
#abracadabra
add-type -AssemblyName System.Windows.Forms
#Return to previous window
[System.Windows.Forms.SendKeys]::SendWait("%{TAB}")
Start-Sleep -m 100
#Launch Google Chrome
Start-Process -FilePath "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Google Chrome.lnk"
Start-Sleep -m 1000
#loops
#5 loops from here
for ($i=0; $i -lt 5; $i++){
Write-Output ("count:"+$i)
#Loop up to here
}
#Moving a window
#操作したいウィンドウのタイトル
$MAIN_WINDOW_TITLE='chrome'
#ウィンドウの新しい幅 (ピクセル単位)
$width=1295
#ウィンドウの新しい高さ (ピクセル単位)
$heigh=1047
#コントロールの左側の絶対画面座標。
$x=3832
#コントロールの上部の絶対画面座標。
$y=246
Add-Type -AssemblyName UIAutomationClient
#検索をデスクトップから開始するためデスクトップを取得する
$root= [System.Windows.Automation.AutomationElement]::RootElement
#検索対象は子要素だけとする
$scope=[System.Windows.Automation.TreeScope]::Children
#とりあえずGet-Processで取得できた一つ目のハンドルを対象とする。
$hwnd=(Get-Process |?{$_.MainWindowTitle -match $MAIN_WINDOW_TITLE})[0].MainWindowHandle
#ハンドルからウィンドウを取得する
$window=[System.Windows.Automation.AutomationElement]::FromHandle($hwnd)
#ウィンドウサイズの状態を把握するためにWindowPatternを使う
$windowPattern=$window.GetCurrentPattern([System.Windows.Automation.WindowPattern]::Pattern)
#ウィンドウサイズを変更する準備としてサイズを通常に変更する
$windowPattern.SetWindowVisualState([System.Windows.Automation.WindowVisualState]::Normal)
#ウィンドウサイズを変更するためのパターン。
$transformPattern=$window.GetCurrentPattern([System.Windows.Automation.TransformPattern]::Pattern)
#Maximamだと移動もサイズ変更もできないので注意。
$transformPattern.Resize($width,$heigh)
$transformPattern.Move($x,$y)
#chromeのウィンドウをアクティブに
$MAIN_WINDOW_TITLE='chrome'
Add-Type -AssemblyName UIAutomationClient
$root= [System.Windows.Automation.AutomationElement]::RootElement
$scope=[System.Windows.Automation.TreeScope]::Children
$hwnd=(Get-Process |?{$_.MainWindowTitle -match $MAIN_WINDOW_TITLE})[0].MainWindowHandle
$window[System.Windows.Automation.AutomationElement]::FromHandle($hwnd)
$windowPattern=$window.GetCurrentPattern([System.Windows.Automation.WindowPattern]::Pattern)
$windowPattern.SetWindowVisualState([System.Windows.Automation.WindowVisualState]::Normal)
#Dialog
#「実行しますか?」実行確認
$result = [System.Windows.Forms.MessageBox]::Show("実行しますか?","確認","YesNo","Question","Button2")
#「実行しますか?」Yesの場合 ここから
If($result -eq "Yes"){
[void][System.Windows.Forms.MessageBox]::Show("実行します","結果","OK","Information")
#「実行しますか?」Yesの場合 ここまで
}
#「実行しますか?」Noの場合
Else{
[void][System.Windows.Forms.MessageBox]::Show("では後ほど","結果","OK","Information")
}
下記参照ください
https://acoustic-groove2.hatenablog.com/entry/2018/01/29/233637