Skip to content

Instantly share code, notes, and snippets.

@bmccormack
Created May 3, 2012 15:18
Show Gist options
  • Save bmccormack/2586415 to your computer and use it in GitHub Desktop.
Save bmccormack/2586415 to your computer and use it in GitHub Desktop.
Select-WindowStarted
# Uses WASP from http://wasp.codeplex.com for automating the control of Windows in Powershell.
# The purpose of this function is to start a new process and then return the window
# that was opened by the started process. This can be difficult because if you already have
# multiple windows open for that process; there may be a delay in the window being returned
# after the process is started; and the process that is started is not always the same as the
# window that's created (Chrome).
#
# This function will grab a list of current windows matching the processName, start a new instance,
# and will then loop until the new window is opened or the function times out. example:
#
# Select-WindowStarted notepad.exe -processName
#
function Select-WindowStarted($process, [string]$processName = "", $msTimeout = 1000){
$startTime = get-date
$endTime = $startTime.addMilliseconds($msTimeout)
if ($processName -eq ""){
$processName = $process
}
$currentWindows = Select-Window $processName
$processID = Start-Process $process -passThru
do {
if((get-date) -gt $endTime){
throw "Timeout while trying to Select window by process"
}
}
while ((Select-Window $processName | where {!($currentWindows -contains $_)} | measure).count -eq 0)
Select-Window $processName | where {!($currentWindows -contains $_)}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment