Last active
July 4, 2020 19:18
-
-
Save kamathln/c7f81dc72129e19d3cf41ea015e1e2ac to your computer and use it in GitHub Desktop.
System tray icon for shell scripts et al. Interface to python pystray library.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# The MIT License (MIT) | |
# Copyright (c) 2017 "Laxminarayan Kamath G A"<[email protected]> | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | |
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE | |
# OR OTHER DEALINGS IN THE SOFTWARE. | |
""" | |
Command line interface to pystray library. SOmehwat incomplete now. | |
TODO: Implement menus | |
Requirements: python3, pystray and PIL | |
""" | |
import PIL.Image | |
import sys | |
import pystray | |
def perr(s): | |
sys.stderr.write(s) | |
sys.stderr.flush() | |
def pout(s): | |
sys.stdout.write(s) | |
sys.stdout.flush() | |
def checkstat(icon): | |
try: | |
while True: | |
commandline = sys.stdin.readline() | |
command = commandline.strip().split(':') | |
found = False | |
if command[0] in ['tray_exit', '']: | |
icon.stop() | |
break | |
if command[0] == 'tray_iconpath': | |
try: | |
icons[command[1]] = PIL.Image.open(command[2]) | |
except FileNotFoundError: | |
perr("File {} not found\n".format(command[2])) | |
found = True | |
if command[0] == 'tray_icon': | |
try: | |
icon.icon = icon.icons[command[1]] | |
except KeyError: | |
perr("Icon name {} not set. Use seticon first\n".format( | |
command[1] | |
)) | |
found = True | |
if command[0] == 'tray_show': | |
icon.visible = True | |
found = True | |
if command[0] == 'tray_hide': | |
icon.visible = False | |
found = True | |
if command[0] == 'tray_warn': | |
icon.warnings = command[1].lower() in ['1', | |
'true', | |
'yes', | |
'on'] | |
found = True | |
if not found: | |
if icon.warnings: | |
perr("Command passed to {} not found: {}".format( | |
sys.argv[0], | |
commandline | |
)) | |
else: | |
pout(commandline) | |
except BrokenPipeError: | |
perr("Exiting\n") | |
icon.stop() | |
sys.exit(0) | |
except KeyboardInterrupt: | |
perr("Exiting\n") | |
icon.stop() | |
sys.exit(0) | |
_usage="""\ | |
USAGE: | |
tray.py [appname] | |
appname: required by system tray protocols to identify the app. Can be\ | |
any alphanumeric name, as long as it is unique, and does not clash with \ | |
other apps. | |
It is only optional for testing, where it will use the default\ | |
'testapp', and generally recommended that it is not left out. | |
Once the script is started, it listens to commands on standard output.\ | |
See the COMMANDS section for more details. | |
tray.py --help | |
Show this help message and exit. | |
COMMANDS: | |
Each command occupies one line. If the commands take parameters, they \ | |
are split using ':'. For now, there is no escaping, and ':' cannot be used\ | |
in any parameters. This may change in future. | |
tray_show - unhides the icon. Note: The icon is initially hidden. Use this\ | |
once you load and map your icons. | |
tray_hide - hides the icon | |
tray_iconpath - loads the image from a file and map it to a name | |
params: | |
icon name - a name to refer to the icon | |
icon path - full path to the icon's image. | |
tray_icon - switch the icon to the image mapped to name | |
params: | |
icon name - the name of the icon mapped using tray_iconpath | |
tray_warn - switch between 1) warning about unknown commands 2) passing on \ | |
unknown commands to stdout. | |
params: | |
behaviour - boolean ("1", "true", "yes" or "on") are taken as true | |
tray_exit - exit the script. (not always necessary as it exits on EOF too) | |
takes no params | |
""".format(sys.argv[0]) | |
if __name__ == '__main__': | |
if len(sys.argv) > 1: | |
name = sys.argv[1] | |
if name in ['--help', '-h']: | |
sys.stderr.write(_usage) | |
sys.exit(0) | |
else: | |
name = "testapp" | |
perr(sys.argv[0] + ": WARNING: App name not set. Setting app name to testapp. To set app name, pass it as first argument on command line\n") | |
icons = { | |
'default': PIL.Image.open('/usr/share/icons/gnome/24x24/apps/utilities-terminal.png') | |
} | |
trayicon = pystray.Icon(name=name, icon=icons['default'], title=name) | |
trayicon.icons = icons | |
trayicon.warnings = True | |
try: | |
trayicon.run(checkstat) | |
except KeyboardInterrupt: | |
perr("Exiting due to interrupt\n") | |
except Exception as e: | |
perr("Exception Occured \n" + str(e)) | |
finally: | |
trayicon.stop() | |
sys.exit(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# example usage for tray.py | |
# uses acpi output and notifies of charger connection/disconnection | |
# on a laptop | |
# Public domain, Author: [email protected] | |
# Note how the messages are passed on to zenity, if not understood by tray.py | |
# This is facilitated by tray_warn:false , where instead of complaining about | |
# unknown commands, it will just pass it on to the next command in the pipeline | |
# via stdout | |
# Note how the iconpaths are assigned in the beginning. It is not necessary to | |
# assign all the icons in the beginning. It is enough to map a name before it | |
# is used but before a particular name is used. | |
cd "$(dirname "$0")" | |
prev="1" | |
( | |
cat <<EOF | |
tray_iconpath:plugged:/usr/share/icons/gnome/24x24/status/battery-good-charging.png | |
tray_iconpath:unplugged:/usr/share/icons/gnome/24x24/status/battery-good.png | |
tray_iconpath:battlow_unplugged:/usr/share/icons/gnome/icons/24x24/status/battery-low.png | |
tray_show | |
tray_warn:false | |
EOF | |
while : | |
do | |
acpi |grep -q Discharging | |
res="$?" | |
if [ "$prev" == "0" -a "$res" == "1" ] | |
then | |
echo 'icon: info' | |
echo 'message: Charger plugged in' | |
fi | |
if [ "$prev" == "1" -a "$res" == "0" ] | |
then | |
echo 'icon: error' | |
echo 'message: Charger unplugged' | |
fi | |
if [ "$res" == "0" ] | |
then | |
echo 'tray_icon:unplugged' | |
else | |
echo 'tray_icon:plugged' | |
fi | |
if [ "$res" == "0" ] | |
then | |
remaining="$(acpi|egrep -o '[0-9][0-9]:[0-9][0-9]:[0-9][0-9]')" | |
remainhh="$(echo "$remaining"|cut -f1 -d ':')" | |
remainmm="$(echo "$remaining"|cut -f2 -d ':')" | |
if [ $remainhh -eq 0 -a $remainmm -lt 45 ] | |
then | |
echo 'tray_icon:battlow_unplugged' | |
echo 'icon: error' | |
echo 'message: BATTERY LOW . CHARGER DISCONNECTED' | |
sleep 15 | |
fi | |
fi | |
prev="$res" | |
sleep 3 | |
done)|python3 tray.py powertray |zenity --notification --listen |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment