Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @REM - Check4Change.BAT (23 Mar 2025 // 24 Mar 2025): Check for a specific process to monitor by way of process name and window title
- @REM - Original Message: https://www.reddit.com/r/Batch/comments/1ji9fii/monitor_window_title_and_trigger_action_on_change/
- @ECHO OFF
- :Variables -- Initialize Environment Variables -- v1.0.1
- SETLOCAL
- SET "#Process=vlc.exe" rem -- change this to what you want the process to be.
- SET "#WinTitle=VLC media player" rem -- This is what I used for testing, but you should change it to be what you need for the first run
- SET "#TaskList="
- :InitialCheck -- Search for the window title on the selected process to find the one PID to track from then on
- ECHO Searching for the correct process (%#Process%) via windows title (%#WinTitle%)
- CALL :GetProcessInfo #WhichPID
- IF NOT DEFINED #WhichPID (
- ECHO ERROR: Could not find valid/correct "%#Process%" process to track...
- TASKLIST.EXE /FI "IMAGENAME EQ %#Process%" /V
- GOTO :ExitBatch
- ) ELSE (
- ECHO Tracking PID: %#WhichPID% for changes...
- CALL :GetWindowTitle #InitialTitle
- )
- :Wait4Change -- Wait for changes to the selected PID (with 5 second delay)
- TIMEOUT 5 /NOBREAK >NUL
- CALL :GetWindowTitle #CurrentTitle
- IF /I "%#CurrentTitle%"=="%#InitialTitle%" GOTO :Wait4Change
- :RunCURL -- with extra info to show the window changes
- ECHO:
- ECHO Title changed from "%#InitialTitle%" to "%#CurrentTitle%"
- echo curl -X POST "http://192.168.x.xxx/json/state" -d "{Window Title}" -H "Content-Type: application/json"
- SET "#InitialTitle=%#CurrentTitle%"
- GOTO :Wait4Change
- :ExitBatch -- exit the script and clean out the environment variables
- ENDLOCAL
- EXIT /B
- :GetProcessInfo -- subroutine (%1 = variable name)
- FOR /F "DELIMS=" %%L IN ('TASKLIST.EXE /FI "IMAGENAME EQ %#Process%" /V ^| FIND /I "%#WinTitle%"') DO SET "#TaskList=%%L" rem -- Get all the process info here
- IF NOT DEFINED #TaskList GOTO :EOF
- FOR /F %%p IN ('ECHO %#TaskList:~25%') DO SET "%~1=%%~p" -- Get the actual PID here
- EXIT /B
- :GetWindowTitle -- subroutine (%1 = variable name)
- IF "%~1"=="" GOTO :EOF
- FOR /F "TOKENS=2*" %%a IN ('TASKLIST.EXE /FI "PID EQ %#WhichPID%" /FO LIST /V ^| FIND "Window Title"') DO SET "%~1=%%b" rem -- Get Window Title of selected PID info here
- EXIT /B
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement