11

The computer is being used by three users (+1 admin) and they use unity login. The three users come, use the computer and logout. I have to give some instructions, news and details every time they login.

I want to have a utility/tool where I can type in a custom message to each user. The latest message would then be displayed on the desktop (for each user) (with date of my last edit) for say 10-15 secs. A copy of the text also be available (to the user) somewhere with all messages and dates like a log.

enter image description here

*Admin has SSH access to the system.

I am not sure if this is possible (and may be I am looking for too much !).

  • Can be done. What's your targeted version of Ubuntu ? Are popups ok ? Or you'd prefer on-screen text like your example shows ? – Sergiy Kolodyazhnyy May 14 '16 at 5:30
  • Running 14.04 LTS and no plans to upgrade anytime soon. Popups / notification are fine if they stay for some seconds (~10-15 sec) – ankit7540 May 14 '16 at 6:07
  • I've implemented a quick script, posted an answer. It allows creating a file and that file will be matched to each user. Let me know what other functionality you'd want me to add there,and in case you need any other clarification. The script is not fancy , but fairly stable. – Sergiy Kolodyazhnyy May 14 '16 at 6:31
  • Hi @ankit7540 posted my answer, in the first "edition", forgot to mention to make the script executable, but that might be obvious :) – Jacob Vlijm May 14 '16 at 6:45
  • Thanks for your efforts @Serg : I like the approach, let me test and reply. – ankit7540 May 14 '16 at 6:47
11

You can do that in the following setup, which will produce a semi- transparent window, staying for an arbitrary time:

  1. 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:

    enter image description here

  2. 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()
    
  3. 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.

enter image description here

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.

  • Tried. It works very nicely. Looking at the code I understand how to change the time its stays. One question: What if I do not have SSH access to the user's home (but only to my home) ? – ankit7540 May 14 '16 at 7:07
  • You (or I) can set any path, but we need to make it use getpass then to make each user atomatically request the named file. What path would you have in mind? It must be a path that each user has read access to. Wait, will edit in a minute... – Jacob Vlijm May 14 '16 at 7:09
  • @ankit7540 edited it into the script (and into the explanation). – Jacob Vlijm May 14 '16 at 7:26
  • @ankit7540 Ah, I see you first line: "Welcome user..." Is that important to you? is just a line in the code if it is. – Jacob Vlijm May 14 '16 at 7:30
  • About that line "Welcome $user", you can just tell me here and I can add it in the script. Additionally, consider user has a folder, /home/message. It would be good to copy the recent message text (catenate etc.. ) in a fixed file (for reference ) – ankit7540 May 14 '16 at 7:36
6

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

enter image description here

  • Sorry to keep you waiting. It works super nice. I like the buttons 'Cancel' and 'OK'. I used it with .desktop entry and it works perfect ! – ankit7540 May 14 '16 at 7:25
  • @ankit7540 Glad you like it. Any additional features that you'd want me to add ? – Sergiy Kolodyazhnyy May 14 '16 at 7:27
  • Consider user has a folder, /home/message. It would be good to copy the recent message file and keep there for reference. – ankit7540 May 14 '16 at 7:33
  • @ankit7540 can be done. It's merely a matter of adding a line with cp command , first argument is the admin's copy and second argument/home/$( _get_username )/messages/$FILE.txt I do need to sleep but i can add that in the morning. It's just one line. – Sergiy Kolodyazhnyy May 14 '16 at 7:47
  • And if you want to catenate the file thats just cat $FILE >> /home/$( _get_username )/messages/Log_of_all_messages. txt – Sergiy Kolodyazhnyy May 14 '16 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.