Advertisement
BrainWaveCC

Check4Change.BAT

Mar 23rd, 2025 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 2.26 KB | Source Code | 0 0
  1. @REM - Check4Change.BAT (23 Mar 2025 // 24 Mar 2025): Check for a specific process to monitor by way of process name and window title
  2. @REM - Original Message: https://www.reddit.com/r/Batch/comments/1ji9fii/monitor_window_title_and_trigger_action_on_change/
  3. @ECHO OFF
  4.  
  5. :Variables -- Initialize Environment Variables -- v1.0.1
  6.  SETLOCAL
  7.  SET "#Process=vlc.exe"            rem -- change this to what you want the process to be.
  8.  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
  9.  SET "#TaskList="
  10.  
  11. :InitialCheck -- Search for the window title on the selected process to find the one PID to track from then on
  12.  ECHO Searching for the correct process (%#Process%) via windows title (%#WinTitle%)
  13.  CALL :GetProcessInfo #WhichPID
  14.  IF NOT DEFINED #WhichPID (
  15.      ECHO ERROR: Could not find valid/correct "%#Process%" process to track...
  16.      TASKLIST.EXE /FI "IMAGENAME EQ %#Process%" /V
  17.      GOTO :ExitBatch
  18.  ) ELSE (
  19.      ECHO Tracking PID: %#WhichPID% for changes...
  20.      CALL :GetWindowTitle #InitialTitle
  21.  )
  22.  
  23. :Wait4Change -- Wait for changes to the selected PID (with 5 second delay)
  24.  TIMEOUT 5 /NOBREAK >NUL
  25.  CALL :GetWindowTitle #CurrentTitle
  26.  IF /I "%#CurrentTitle%"=="%#InitialTitle%" GOTO :Wait4Change
  27.  
  28. :RunCURL -- with extra info to show the window changes
  29.  ECHO:
  30.  ECHO Title changed from "%#InitialTitle%" to "%#CurrentTitle%"
  31.  echo curl -X POST "http://192.168.x.xxx/json/state" -d "{Window Title}" -H "Content-Type: application/json"
  32.  SET "#InitialTitle=%#CurrentTitle%"
  33.  GOTO :Wait4Change
  34.  
  35. :ExitBatch -- exit the script and clean out the environment variables
  36.  ENDLOCAL
  37.  EXIT /B
  38.  
  39. :GetProcessInfo -- subroutine (%1 = variable name)
  40.  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
  41.  IF NOT DEFINED #TaskList GOTO :EOF
  42.  FOR /F %%p IN ('ECHO %#TaskList:~25%') DO SET "%~1=%%~p" -- Get the actual PID here
  43.  EXIT /B
  44.  
  45. :GetWindowTitle -- subroutine (%1 = variable name)
  46.  IF "%~1"=="" GOTO :EOF
  47.  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
  48.  EXIT /B
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement