Custom (editable) message for each user after login
Solution 1:
You can do that in the following setup, which will produce a semi- transparent window, staying for an arbitrary time:
-
Create in a (any) directory, in which each of the users at least has read permissions, for each user a message file, exactly named after each user's username (log in name), no extension, e.g.
jacob
Add text to the file as your message. If you add a new message, start with
###
, the script will then automatically display the last message. The text can contain anything you like, it will look exactly like you put it in the file.The latest message, will be copied as
latest_message.txt
to the user's home directory for reference.As it is, the window will stay for 15 seconds, but you can set it to any value, even make it automatically depend on the length of the text.
An example file could look like:Vraag: Een aap op een fiets, hoe vind je zoiets? Opdracht: Geef antwoord op de vraag! ### Vraag: Hoe is de koffie vandaag? Opdracht: Zet het zelf even als het niet te drinken is!
The message then would look like:
-
Copy the script below into an empty file, edit in the head section of your script the path to the folder in which you store the user(s) messages. Save the script as
show_personalmessage
in (e.g.)/usr/local/bin
(which should be in$PATH
and make it executable(!) (no extension)#!/usr/bin/env python3 import subprocess import os import gi gi.require_version('Gtk', '3.0') from gi.repository import GObject, Gtk, Gdk, Pango from threading import Thread import time import getpass # --- set the path to the message files below, filename = username filedir = "/home/jacob/Bureaublad" # --- set the time to show the window below showtime = 15 # --- # don't change anything below user = getpass.getuser() currmessage = os.environ["HOME"]+"/latest_message.txt" f = filedir+"/"+user text = "Welcome "+user+"\n\n"+open(f).read().split("###")[-1] open(currmessage, "wt").write(text) class Splash(Gtk.Window): def __init__(self): Gtk.Window.__init__(self, title="splashtitle") maingrid = Gtk.Grid() self.add(maingrid) maingrid.set_border_width(80) # set text for the spash window label = Gtk.Label(text) label.modify_font(Pango.FontDescription('Ubuntu 12')) maingrid.attach(label, 0, 0, 1, 1) self.stop = Thread(target=self.close_window) self.stop.start() def close_window(self): time.sleep(showtime) Gtk.main_quit() def splashwindow(): window = Splash() window.set_decorated(False) window.set_resizable(False) window.override_background_color(Gtk.StateType.NORMAL, Gdk.RGBA(0,0,0,1)) window.modify_fg(Gtk.StateFlags.NORMAL, Gdk.color_parse("grey")) window.set_opacity(0.8) window.set_position(Gtk.WindowPosition.CENTER) window.show_all() GObject.threads_init() Gtk.main() splashwindow()
-
Create a launcher in
/etc/xdg/autostart
[Desktop Entry] Type=Application Name=Splash Exec=/bin/bash -c "sleep 10 && show_personalmessage"
The sleep 10 is to make sure the desktop is "ready" to open the window.
Explanation
- Launchers in
/etc/xdg/autostart
run commands for each user on log in. - The launcher then runs the window (called by the command
show_personalmessage
), which looks for the personalized message in the directory you defined. Additionally. The most recent message is copied to the user's home directory. - If required, the path to the message can be altered, even be made user specific by using the
getpass
-module, so that the script (window) will look for a user- specific named file in a directory. Please mention if that would be required.
Additionally
The (Gtk) window
- can be made to stay below everything, as if it would be part of the background
- can be made stay on top of everything
- can be made closeable
etc, etc...
EDIT
To save time writing messages, as discussed in chat, below a version of the script in which you can include a few "permanent" sections:
-
premsg
, which should be just below "Welcome user x", and the body of your message, and -
postmsg
, which comes as the bottom section of your message.
Both sections can be set to none, just by setting ""
as a value.
The script
#!/usr/bin/env python3
import subprocess
import os
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import GObject, Gtk, Gdk, Pango
from threading import Thread
import time
import getpass
# --- set the path to the message files below, filename = username
filedir = "/path/to/message_directory"
# --- set the time to show the window below
showtime = 15
# --- set pre-message below. set premessage = "" for no pre-message
premsg = """We assume you read all 3782 instruction pages on how to use
Ubuntu before you push any button on this computer.
"""
# --- set post-message below. set postmessage = "" for no post-message
postmsg = """Before you go to sleep tonight, make sure to brush your
teeth for at least half an hour
"""
# --- don't change anything below
user = getpass.getuser()
currmessage = os.environ["HOME"]+"/latest_message.txt"
f = filedir+"/"+user
text = "Welcome "+user+"\n\n"+premsg+"\n"+open(f).read().split("###")[-1]+"\n"+postmsg
open(currmessage, "wt").write(text)
class Splash(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="splashtitle")
maingrid = Gtk.Grid()
self.add(maingrid)
maingrid.set_border_width(80)
# set text for the spash window
label = Gtk.Label(text)
label.modify_font(Pango.FontDescription('Ubuntu 12'))
maingrid.attach(label, 0, 0, 1, 1)
self.stop = Thread(target=self.close_window)
self.stop.start()
def close_window(self):
time.sleep(showtime)
Gtk.main_quit()
def splashwindow():
window = Splash()
window.set_decorated(False)
window.set_resizable(False)
window.override_background_color(Gtk.StateType.NORMAL, Gdk.RGBA(0,0,0,1))
window.modify_fg(Gtk.StateFlags.NORMAL, Gdk.color_parse("white"))
window.set_opacity(0.8)
window.set_position(Gtk.WindowPosition.CENTER)
window.show_all()
GObject.threads_init()
Gtk.main()
splashwindow()
Note
Of course, just like the message body, yoy can alter the script to read the pre- and post messages from a file, which makes it even more convenient to maintain. Did it like this, for reasons of simplicity in the answer.
Solution 2:
The script bellow allows admin to store personalized messages in form username_mm_dd_yyyy.txt
in admin-defined directory. Usernames are determined automatically and matched against filename and date of the file to be displayed.
The script can also have a .desktop
entry which is to be placed into /etc/xdg/autostart
directory . That will make the script to be displayed for each user. Alternatively you can place the .desktop
file into each individual user's ~/.config/autostart
directory.
#!/bin/bash
_get_username()
{
qdbus com.canonical.Unity \
/com/canonical/Unity/Session \
com.canonical.Unity.Session.UserName
}
_get_date()
{
date +%m_%d_%Y
}
_show_error()
{
MESSAGE="Can't read the file or file doesn't exist. Contact the admin for assistance"
zenity --error --title="Ooops, something went wrong!" --text="$MESSAGE"
}
main()
{
# replace this with the actual directory
# that you want to use for storing messages
MESSAGES_DIRECTORY="/tmp"
# file name is made up of username_mm_dd_yyyy
FILE="$MESSAGES_DIRECTORY"/"$( _get_username )"_"$( _get_date )".txt
echo "$FILE"
if [ -r "$FILE" ] ; then
zenity --text-info --title="Welcome, $( _get_username )" --filename="$FILE"
else
_show_error
fi
}
main
Bellow you can see a small demo of the script in action. I have created the file in specified format in my /tmp
folder