Skip to content

Instantly share code, notes, and snippets.

@akaleeroy
Last active November 16, 2018 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akaleeroy/9411160 to your computer and use it in GitHub Desktop.
Save akaleeroy/9411160 to your computer and use it in GitHub Desktop.
When Over - Run command on event (application close, progress finished, idle)

When Over

whenthe muuusic-s.exe "Ooover" "turn /off the-liiiiights"

Usage examples

  • Application closes: whenover 7zG.exe "shutdown -s -t 60" Demo

This will wait for the 7zip archival process to finish and execute system shutdown in 60 seconds

  • Window title becomes "100% of file Bla": whenover chrome.exe "100% of file" "D:\myscript.cmd"
  • Window title changes from "4 Hour Set": whenover chrome.exe -nolonger "4 Hour" "shutdown -h"
  • Custom condition: whenover -c "if not exist D:\Downloads\*.crdownload" "shutdown -s -t 60"
  • Process has ceased I/O activity (disk writes/reads): whenover -io SoulseekQt "shutdown -s -t 60"

Known limitations

  • Window title is main window title, the foreground tab. Background tab titles return Window Title: Unknown.
  • In chrome.exe hovering over another tab makes the foreground tab's Window Title: N/A
:: When Over
:: Usage examples
:: --------------
:: Application closes: whenover 7zG.exe "shutdown -s -t 60"
:: This will wait for the 7zip archival process to finish and execute system shutdown in 60 seconds
:: Window title becomes "100% of file Bla": whenover chrome.exe "100% of file" "D:\myscript.cmd"
:: Window title changes from "4 Hour Set": whenover chrome.exe -nolonger "4 Hour" "shutdown -h"
:: Custom condition: whenover -c "if not exist D:\Downloads\*.crdownload" "winrar.lnk x D:\Downloads\*.zip K:\sdcard0\Music\"
:: Or another example: whenover -cond "timeout /t 10 & if errorlevel 0" "foobar2000.exe /exit"
:: Application has ceased I/O activity (disk writes/reads): whenover -io SoulseekQt "shutdown -s -t 60"
:: Known limitations
:: -----------------
:: Window title is main window title, the foreground tab. Background tab titles return Window Title: Unknown.
:: In chrome.exe hovering over another tab makes the foreground tab's Window Title: N/A
@echo off
:: Small window height, grey text on black background
mode con cols=90 lines=10 & color 0F
:: Check for custom condition
if "%~1"=="-c" goto CustomConditionCheck
if "%~1"=="-cond" goto CustomConditionCheck
if "%~1"=="-condition" goto CustomConditionCheck
:: Check for I/O activity like disk writes and stuff
if "%~1"=="-io" goto DiskWritesCheck
if "%~1"=="-diskwrites" goto DiskWritesCheck
:: Check for when the application closes
if "%~3"=="" goto CloseCheck
:: Check for when window title no longer matches supplied parameter
if "%~2"=="-nolonger" goto NoLongerTitleCheck
:TitleCheck
title Watching if process title is
cls
echo Watching "%~1" for when its title has "%~2".
tasklist /v /fi "imagename eq %1" /fo csv | find /i "%~2" >nul && (
title Executing %~3.
call %~3
exit /b
) || (
:: Test every 1 second. Comment this out if you need an instant reaction.
timeout /t 1 /nobreak >nul
goto :TitleCheck
)
:CloseCheck
title Waiting for "%~1" to close.
tasklist /nh /fi "imagename eq %1" | find /i "%1" >nul && (
timeout /t 1 /nobreak >nul
goto :CloseCheck
) || (
title %~1 is now closed. Executing %~2.
call %~2
exit /b
)
:NoLongerTitleCheck
title Watching if process title isn't
cls
echo Watching "%~1" for when its title no longer matches "%~3".
tasklist /v /fi "imagename eq %1" /fo csv | find /i "%~3" >nul && (
timeout /t 1 /nobreak >nul
goto :NoLongerTitleCheck
) || (
title Executing %~4.
call %~4
exit /b
)
:CustomConditionCheck
title Checking custom condition.
cls
echo Checking condition && echo."%~2" && echo.and doing && echo."%~3"
%~2 (
title Executing %~3.
call %~3
exit /b
) else (
timeout /t 1 /nobreak >nul
goto :CustomConditionCheck
)
:DiskWritesCheck
title Checking process for disk reads/writes
cls
echo Waiting for %~2 to cease I/O activity (disk reads/writes)
echo.then running:
echo.%~3
typeperf -sc 1 "\Process(%~2)\IO Data Operations/sec" | find "0.000000" >nul && (
echo Executing %~3
call %~3
exit /b
) || (
tasklist /fi "imagename eq %~2*" | find /i "%~2" >nul
if errorlevel 1 (
echo Process not found!
pause
goto :DiskWritesCheck
)
timeout /t 10 /nobreak >nul
goto :DiskWritesCheck
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment