5

I have a program run on the background that simply take a screenshot every N seconds.

eg:

#!/bin/sh

while true; do
  take-screenshot
  sleep 10
done

What I want to achieve is only take screenshot if screen is not locking. eg:

#!/bin/sh

while true; do
  if ! screen-is-locking; then
    take-screenshot
    sleep 10
  fi
done

How can I determine if my desktop is locking in command line?

Bi Ao
  • 704
  • 5
  • 11

1 Answers1

7

According to this link provided by fellow comments under this question, I got a solution or workaround for my question, it works well under 5.4.58-1-MANJARO KDE Plasma (other DE might have different ways according to that answer's statement).

There's a method in dbus service which well fit my need:

#/bin/sh

is_screen_locking()
{
  if dbus-send --session --dest=org.freedesktop.ScreenSaver --type=method_call --print-reply /org/freedesktop/ScreenSaver org.freedesktop.ScreenSaver.GetActive | grep 'boolean true' > /dev/null; then
    return 0
  else
    return 1
  fi
}



if is_screen_locking; then
   echo 'screen is locking'
else
   echo 'screen is not locking'
fi

Bi Ao
  • 704
  • 5
  • 11