I am writing a small daemon which logs the active window name. The relevant part goes like:
import wnck
import gtk
from plugins import IReporter
class ActiveWindowNameReporter(IReporter):
export_as = 'active_window_name'
def report(self):
name = None
screen = wnck.screen_get_default()
screen.force_update()
while gtk.events_pending():
gtk.main_iteration()
if screen:
active_window = screen.get_active_window()
if active_window:
name = active_window.get_name()
return name
I wrote a simple systemd user service file for this:
[Unit]
Description=Activity Monitor Service
[Service]
ExecStart=/usr/bin/python /home/me/Projects/monitor/monitor.py
[Install]
WantedBy=graphical.target
However, running the service fails. In journalctl -r I can see:
Unit actor.service entered failed state.
actor.service: main process exited, code=exited, status=1/FAILURE
ImportError: could not import gtk
import wnck
File "/home/me/Projects/monitor/reporters/active_window_name.py", line 1, in <mod
from reporters import *
File "/home/me/Projects/monitor/monitor.py", line 16, in <module>
Traceback (most recent call last):
detected unhandled Python exception in '/home/tbabej/Projects/actor/actor.py'
Started Activity Monitor Service.
Starting Activity Monitor Service...
Seems like the import of gtk module fails because the session is not available? Setting the XDG_SESSION_ID variable in the script did not help though.