1

How to program a batch program to determine if a the screensaver is running or if the user went to sleep, or locked the computer?

In bash in Ubuntu Linux I am using the code: /gnome-screensaver-command -q | grep "is active" to determine if screensaver is running.

Note: I am not seeking recommendations for books, tools, software libraries...

Unless there is a better approach, I am working off of this script to look for processes running. But I have to find the name of the screensaver process.

@echo off
set pn=%1
echo looking for %pn%
tasklist /FI "IMAGENAME eq %pn%" 2>NUL | find /I /N "%pn%">NUL
if %ERRORLEVEL%==0 (
    echo Found program running
) else (
    echo NOT FOUND running  
)
jdl
  • 6,151
  • 19
  • 83
  • 132
  • 2
    Your previous version of this question was closed as "seeking recommendation" because there's (_inexplicably_) no close reason for "request for code." – SomethingDark Dec 25 '20 at 01:43

2 Answers2

1

The name of the screensaver process will vary by which screensaver is running, but it should always end in .scr.

tasklist | find ".scr" >nul
if %errorlevel% EQU 0 (
    echo Screensaver is running
) else (
    echo Screensaver is not running
)

The default Windows 10 screensavers have the following process names:

3D Text - ssText3d.scr
Blank   - scrnsave.scr
Bubbles - Bubbles.scr
Mystify - Mystify.scr
Photos  - PhotoScreensaver.scr
Ribbons - Ribbons.scr

When the computer is locked, the process LogonUI.exe runs.

tasklist | find "LogonUI.exe"
if %errorlevel% EQU 0 (
    echo Computer is locked
) else (
    echo Computer is unlocked
)
SomethingDark
  • 13,229
  • 5
  • 50
  • 55
  • ok...I locked the computer.... should this script output screensaver is running ? – jdl Dec 25 '20 at 20:47
  • @jdl - If the screensaver runs while the computer is locked and the script is running on a loop (because that snippet only runs once and I don't know how you'd run the script _after_ locking the computer), then it should detect the screensaver. – SomethingDark Dec 25 '20 at 20:56
  • it is in the task scheduler and kicks off every minute – jdl Dec 25 '20 at 20:58
  • always says Screensaver is not running at every minute... and I am viewing this remotely via SSH – jdl Dec 25 '20 at 20:58
  • _Is_ the screensaver running? I know that locking my computer doesn't automatically turn the screen saver on. Try running a `tasklist` and see if there's anything that looks screensaver-y. – SomethingDark Dec 25 '20 at 21:00
  • can we detected when the computer is locked? this is different than linux screensaver... ie you don't necessarily see the screen saver... just that the user was put to sleep – jdl Dec 25 '20 at 21:03
  • when i do "TASKLIST /FI "imagename eq *pad*" /svc" to look for notepad... it won't work. I need to do TASKLIST /FI "imagename eq note*" /svc. I have to have the first part of the name than a star to get the list. I would get this: INFO: No tasks are running which match the specified criteria. – jdl Dec 25 '20 at 21:11
  • Don't use `/FI` at all; just `tasklist | find "note"` – SomethingDark Dec 25 '20 at 21:19
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/226425/discussion-between-jdl-and-somethingdark). – jdl Dec 25 '20 at 21:21
  • 1
    @SomethingDark : `screensaver-y`? As distinct from `screensweet-y`, perhaps? – Magoo Dec 27 '20 at 05:20
0
@echo off 

pushd "%__AppDir__%"
title <nul & title ..\%~nx0
setlocal EnableDelayedExpansion
set "_Reg_Key=HKCU\Control Panel\Desktop"

:loop
for /f skip^=2tokens^=2* %%i in (
';2^>^&1 reg.exe query "!_Reg_Key!" /v SCRNSAVE.EXE
')do if /i "%%~xj" == ".scr" (set "_scr_svr=%%~nxj") else (
     echo/[ !date! ^| !time:~0,8! ] Screen Saver: Disable " - - - - -!"
     rem./ With ScreenSaver is disabled no status is need && goto:next
    )

for /f skip^=2tokens^=2* %%i in (
';2^>nul reg query "!_Reg_Key!" /v ScreenSaveActive')do if %%j equ 1 (
      2>nul tasklist.exe/svc /fo list|find.exe/i "!_scr_svr!">nul && (
      echo/[ !date! ^| !time:~0,8! ] Screen Saver: Running "!_scr_svr!"
    ) || (
      echo/[ !date! ^| !time:~0,8! ] Screen Saver: Enabled "!_scr_svr!"
      echo/[ !date! ^| !time:~0,8! ] Screen Saver: Stopped "!_scr_svr!")
    )

:next
2>nul tasklist.exe/svc /fo list|find.exe/i "LogonUI.exe" >nul && (
         echo/[ !date! ^| !time:~0,8! ] Work Station: Locked  "!ComputerName!"
    ) || echo/[ !date! ^| !time:~0,8! ] Work Station: Logged  "!ComputerName!"

<con: set "_scr_svr=" & >nul timeout/t 10 /nobreak & goto :Loop  

1. Use a Reg Query to see if the screen saver is currently enabled

for /f skip^=2tokens^=2* %%i in (
';2^>nul reg query "!_Reg_Key!" /v ScreenSaveActive')do if %%j equ 1 (

2. If the screen saver is currently enabled, use an additional Reg Query to obtain and save The_Name + .Extension

3. Avoid a partial string in | find ".scr" and use find /i "The_Full_File_Name.scr" for insensitive case and for matches some_name.scr ≠≠ Some_Name.SCR and some_name.scr == Some_Name.SCR

  • Obs.1 In using tasklist for the task list with |find ".scr" any string present in the name that also contains a string .str (lower case only), this will also be listed whether a screensaver may or may not be running.

  • Obs.2 If the length of the characters in the screen_saver_long_name.scr name (including the extension) is greater than 25, with tasklist | find ".src", you will never filter/found this file, because the tasklist (without any /flag) has only 26 characters length in output (, ending with.)

  • Obs.:3 For list process where some screen saver name like Security Screensaver NG.scr is running, use tasklist /svc /fo list

4. Use a loop and keep the monitoring perceptible to any changes that may occur at any time within the loop process, constantly querying the registry value.


Possible status and outputs:

  • Screen Saver enabled and not running, but user logged:
[ Sun 12/27/2020 | 23:26:17 ] Screen Saver: Enabled "Fliqlo.scr"
[ Sun 12/27/2020 | 23:26:17 ] Screen Saver: Stopped "Fliqlo.scr"
[ Sun 12/27/2020 | 23:26:17 ] Work Station: Logged  "LAME_SLUG"
  • Screen Saver enabled and running, but work station locked:
[ Sun 12/27/2020 | 27:09:07 ] Screen Saver: Enabled "Fliqlo.scr"
[ Sun 12/27/2020 | 27:09:07 ] Screen Saver: Running "Fliqlo.scr"
[ Sun 12/27/2020 | 27:09:07 ] Work Station: Locked  "LAME_SLUG"
  • Screen Saver enabled not running, but user logged:
[ Sun 12/27/2020 | 25:26:21 ] Screen Saver: Enabled "Fliqlo.scr"
[ Sun 12/27/2020 | 25:26:21 ] Screen Saver: Running "Fliqlo.scr"
[ Sun 12/27/2020 | 25:26:21 ] Work Station: Logged  "LAME_SLUG"
  • Screen Saver enabled and not running, work station locked:
[ Sun 12/27/2020 | 25:15:28 ] Screen Saver: Enabled "Fliqlo.scr"
[ Sun 12/27/2020 | 25:15:28 ] Screen Saver: Stopped "Fliqlo.scr"
[ Sun 12/27/2020 | 25:15:28 ] Work Station: locked  "LAME_SLUG"
  • Screen Saver disabled not running, but user logged:
[ Sun 12/27/2020 | 22:51:46 ] Screen Saver: Disable " - - - - -"
[ Sun 12/27/2020 | 22:51:46 ] Work Station: Logged  "LAME_SLUG"
  • Screen Saver disabled not running, but work station locked:
[ Sun 12/27/2020 | 22:44:11 ] Screen Saver: Disable " - - - - -"
[ Sun 12/27/2020 | 22:44:11 ] Work Station: Locked  "LAME_SLUG"

  • To limit to 10 loops, with 10 intervals/timeout of 10 seconds each
@echo off 

set/a "_c+=0"
pushd "%__AppDir__%"
title <nul & title ..\%~nx0
setlocal EnableDelayedExpansion
set "_Reg_Key=HKCU\Control Panel\Desktop"

:loop
for /f ^skip^=2tokens^=2* %%i in (
';2^>^&1 reg.exe query "!_Reg_Key!" /v SCRNSAVE.EXE
')do if /i "%%~xj" == ".scr" (set "_scr_svr=%%~nxj") else (
     echo/[ !date! ^| !time:~0,8! ] Screen Saver: Disable " - - - - -!"
     rem./ With ScreenSaver is disabled no status is need && goto:next
    )

for /f skip^=2tokens^=2* %%i in (
';2^>nul reg query "!_Reg_Key!" /v ScreenSaveActive')do if %%j equ 1 (
      2>nul tasklist.exe/svc /fo list|find.exe/i "!_scr_svr!">nul && (
      echo/[ !date! ^| !time:~0,8! ] Screen Saver: Running "!_scr_svr!"
    ) || (
      echo/[ !date! ^| !time:~0,8! ] Screen Saver: Enabled "!_scr_svr!"
      echo/[ !date! ^| !time:~0,8! ] Screen Saver: Stopped "!_scr_svr!")
    )

:next
2>nul tasklist.exe/svc /fo list|find.exe/i "LogonUI.exe" >nul && (
         echo/[ !date! ^| !time:~0,8! ] Work Station: Locked  "!ComputerName!"
    ) || echo/[ !date! ^| !time:~0,8! ] Work Station: Logged  "!ComputerName!"

if !_c! leq 9 (
      set /a "_c+=1" && set "_scr_svr=" && >nul timeout/t 10 & goto:Loop
    ) else popd && endlocal && goto:eof
Io-oI
  • 2,514
  • 3
  • 22
  • 29