MichaelE Posted November 19, 2019 Posted November 19, 2019 Hi Everyone. I've been using the older AutoIt to setup my desktop layouts at the start of the day. Basically the script would open up all of the applications I normally use daily and position all of them into the correct locations. This worked well but had some limitations. I normally run on a three monitor desktop and when I undock for meetings all of the application positions would get messed up and I would have to manually move them all back. Using the new AutoItX powershell utilities I've re-written the script. First this function to actually capture a window location and print the details to screen for copying and pasting into the main script. To use this make sure the application is running and then sized and positioned how you want before running the function. function getAppSettings { [CmdletBinding()] param ( [Parameter(ValueFromPipelineByPropertyName=$true, Position=0)] [string]$appName ) $cHandle = (get-process -name $appName).mainwindowhandle | Where {$_ -ne 0} | Select -unique $cExe = (get-process -name $appName -fileversioninfo).FileName | Select -unique $appWinPos = Get-AU3WinPos -WinHandle $cHandle $xSetting = '$x = ' + $appWinPos.location.x $ySetting = '$y = ' + $appWinPos.location.y $widthSetting = '$width = ' + $appWinPos.size.width $heightSetting = '$height = ' + $appWinPos.size.height $appNameSetting = '$appName = "' + $appName + '"' $exeSetting = '$exe = "' + $cExe + '"' $cmdLine = 'handleApp -appName $appName -x $x -y $y -width $width -height $height -exe $exe' Write-host "`n### $appName : Application Settings`n" Write-Host "$xSetting`n$ySetting`n$widthSetting`n$heightSetting`n$appNameSetting`n$exeSetting`n`n$cmdLine" Write-host "`n`n`n" This function outputs something like this: PS C:\WIP> getappsettings chrome ### chrome : Application Settings $x = 107 $y = 0 $width = 1600 $height = 920 $appName = "chrome" $exe = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" handleApp -appName $appName -x $x -y $y -width $width -height $height -exe $exe The main script uses the following function to actually start/move the application windows to the correct locations. This current version still uses the get-childwindow function but it's redundant and can be coded out. I'll include it for now to preserve what I am currently using. function handleApp { <# .SYNOPSIS Moves or Starts application then moves it to specified location. .DESCRIPTION Moves or Starts application then moves it to specified location. .PARAMETER appName File file path to executable .PARAMETER x Integer value of "X" location .PARAMETER y Integer value of "Y" location .INPUTS None .OUTPUTS None .NOTES Version: $Rev$ Author: $Author$ Creation Date: $Date$ Purpose/Change: Initial script development .EXAMPLE <Example goes here. Repeat this attribute for more than one example> #> [CmdletBinding()] param ( [Parameter(ValueFromPipelineByPropertyName=$true, Position=0)] [string]$appName, [Parameter(Mandatory=$true)][int]$x, [Parameter(Mandatory=$true)][int]$y, [Parameter(Mandatory=$true)][int]$width, [Parameter(Mandatory=$true)][int]$height, [Parameter(Mandatory=$true)][string]$exe, [Parameter(Mandatory=$true)][string]$prfl ) process { $cHandle = $null try { Write-Verbose "###`t $prfl : Working on $appName" -verbose $cHandle = (get-process -name $appName | get-childwindow).mainwindowhandle | Where {$_ -ne 0} | Select -unique Move-AU3Win -WinHandle $cHandle -X $x -Y $y -Width $width -Height $height | out-null } catch { $sleepTimer = 0 Write-Verbose "###`t $prfl : $appName not running. Staring application." -verbose & $exe Write-Verbose "###`t $prfl : Waiting for Application" -verbose Do { $cHandle = (get-process -name $appName | get-childwindow).mainwindowhandle | Where {$_ -ne 0} | Select -unique Write-Verbose "###`t $prfl : Waiting for Application - $sleepTimer" -verbose $sleepTimer++ Start-Sleep -Seconds 1 } Until (-not $cHandle) $cHandle = (get-process -name $appName | get-childwindow).mainwindowhandle | Where {$_ -ne 0} | Select -unique Write-Verbose "###`t $prfl : Application handle ($appName) - $cHandle" -verbose Move-AU3Win -WinHandle $cHandle -X $x -Y $y -Width $width -Height $height | out-null } } } There are some issues with the above script. I have a sleep timer instead of a wait-active function to determine if an application has started. As such I tend to need to run it a couple of times if application startup takes longer. Once I figure out how to properly use the AutoItX functions I'll revise it. Then I have the main part of the script that determines where I am currently working using current IP Address. This script lays out the applications for when I am at the office or working from home. # Environment variables $VerbosePreference = "SilentlyContinue" $userPSDir = "$env:USERPROFILE\Documents\WindowsPowerShell" $scriptDir = "$env:USERPROFILE\Documents\WindowsPowerShell\Scripts" . $scriptDir\get-childwindow.ps1 # Set Location $computer="$env:computername" $atWork = $null $nwINFO = Get-WmiObject -ComputerName $Computer Win32_NetworkAdapterConfiguration | Where-Object { $_.IPAddress -ne $null } $nwIfIndex = ($nwInfo.Description.indexOf("Intel(R) Ethernet Connection I219-V")) $gateway = ($nwInfo[$nwIFIndex].DefaultIPGateway[0]|Out-String).trim() if ($gateway -eq '192.0.0.1') { Write-Verbose "###`tRunning Work Profile" -verbose Start-Sleep 5 fnAtWork -prfl '@Work' } else { Write-Verbose "###`tRunning Home Profile" -verbose Start-Sleep 5 fnAtHome -prfl '@Home' } Look at the startOfDay.ps1 script that I will attach so you can see how it is laid out. Also, I run this in verbose mode but you can silence that by setting the $VerbosePreference variable. get-childwindow.ps1 startOfDay.ps1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now