I have user service running shell script, which is trying to access lockscreen state of my session like this:
# Test Unity screen-lock:
isLocked() {
isLocked=$(gdbus call -e -d com.canonical.Unity -o /com/canonical/Unity/Session -m com.canonical.Unity.Session.IsLocked)
}
lock() {
if [[ $isLocked == "(false,)" ]]; then
gnome-screensaver-command -l
elif [[ $isLocked == "(true,)" ]]; then
exit 1
fi
exit 0
}
The problem is service “is a per-user process, and not per-session”, and I don't know how to access session dbus:
GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name com.canonical.Unity was not provided by any .service files
I was facing this same problem, and my research turned up nothing. If someone can find a better way to query the lock status of Unity, I'd love to hear about it.
I recently learned about named pipes, and at the time I wondered what I might use such a thing for. This problem, it turns out, happens to be a perfect application.
Create the following script...
As noted in the comments, you need to call this script in two ways. When your user logs on, start this script in service mode. I use the "Startup Applications" program to call the script, but it doesn't really matter how it's called so long as its called by your user account on login. Then from within your user service, simply make a call to this script and it will return "true" if the screen is locked or "false" if the screen is unlocked.