Created
May 3, 2012 15:18
-
-
Save bmccormack/2586415 to your computer and use it in GitHub Desktop.
Select-WindowStarted
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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