6

Lots of times I need to Ctrl+C (or Ctrl+Insert) multiple times to copy something. I would appreciate visual feedback that says "a new thing has been copied" or something like that. Is there someway to add this to Ubuntu?

  • You could maybe set up a keybinding which doesn't capture the key combo, just sends a notification using notify-send (from libnotify-bin). – wjandrea Sep 30 '17 at 21:28
  • @marko I'm not sure why you deleted your answer. I flagged it for moderator attention requesting that it be undeleted. – Elder Geek Oct 1 '17 at 2:31
6

I adapted my script to react on clipboard changes from here so that it shows a native notification bubble whenever you copy something:

enter image description here

#!/usr/bin/env python3

# Configuration:
APPNAME = "Clipboard Notifier"  # an arbitrary application name
TITLE = "Clipboard modified"    # the bold headline of each notification
ICON = "edit-paste"             # name of the icon to show
MAXLENGTH = 100                 # maximum message length

# Imports:
import gi
import signal
import notify2
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk

# Set up signal handler for killing the script with Ctrl+C from terminal:
signal.signal(signal.SIGINT, signal.SIG_DFL)

# Initialize the notifications library:
notify2.init(APPNAME)

# Callback function to handle clipboard modification events:
def callback(*args):
    # Get new clipboard content:
    text = clip.wait_for_text()
    # Truncate long content to avoid huge notification bubbles:
    body = text if len(text) < MAXLENGTH else text[:MAXLENGTH] + "..."
    # Create and show notification bubble:
    notify2.Notification(TITLE, body, ICON).show()

# Set up clipboard and register callback for change events
clip = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
clip.connect("owner-change", callback)

# Start Gtk main loop and wait for events:
Gtk.main()

Save that somewhere (e.g. as /usr/local/bin/clipboard-notifier - you need sudo to be allowed to write in that location though, alternatively put it in ~/bin) and make it executable using the command chmod +x FILENAME.

My script uses the Python 3 package notify2 to display the native notification bubbles. This package is normally not installed by default, you have to add it first with the command below:

sudo apt install python3-notify2

If you want, you can modify the values of the capitalized variables near the beginning of the script to your needs, especially TITLE and MAXLENGTH might be useful to change.

Then simply add it to your startup applications and it will launch automatically when you log in the next time. You can also launch the script manually from e.g. the Alt+F2 HUD in Unity.

| improve this answer | |
  • I appear to be missing a requirement. ImportError: No module named 'notify2' – Elder Geek Oct 1 '17 at 2:26
  • 1
    @ElderGeek Yes, I just noticed I forgot writing that you have to sudo apt install python3-notify2 first. Added that now, thanks for paying attention. – Byte Commander Oct 1 '17 at 10:59
  • That did the trick. ;-) – Elder Geek Oct 1 '17 at 19:41
3

I've created a more standard solution using bash scripting only without extra libraries:

Here is how I did it:

Create a file called: clp-notify and make it executable.

Save the file to: /home/$USER/add-ons/scripts/clp-notify

#!/bin/bash

echo "Script $0 started..."
notify-send -u normal -t 3000 -i info 'Script Started!' $0

clip_command="xclip -selection clipboard -o"
current_value=`$clip_command`

while true
do
   new_value=`$clip_command`

   if [[ ! ("$current_value" == "$new_value") ]];
   then      
      echo 'Clipboard Modified...'
      notify-send -u normal -t 3000 -i info 'Clipboard Modified!' 'Your clipboard has been modified.'

      current_value=$new_value
   else
      echo "Polliing..."
   fi

   sleep 1.5
done

Next, I created a second file called start-clipboard-polling that invokes the above script and it is this second file that gets added to start-up:

#!/bin/bash

script_path=/home/$USER/add-ons/scripts/clp-notify

function IsClipboardPollingRunning
{
   if (ps -A | grep -q "clp-notify")
   then
      return 1;
   else
      return 0;        
   fi
}

if (IsClipboardPollingRunning == 0)
then
   echo "Starting $script_path"   
   nohup $script_path &
else
   echo "$script_path is already running"
   notify-send -u normal -t 3000 -i info 'Clipboard Notify' "$script_path is already running."
fi

The purpose of this second file is to prevent the original clp-notify from starting multiple times because the clp-notify script will continue to run even if you log out and in or restart the X server.

Then you need to add this start-clipboard-polling to your start-up applications.

There is only one problem with this. Although, the second script takes care of preventing clp-notify from starting multiple times when you log out and in (because the original instance would be still running from the previous session), the second time you log in, for some reason, the following condition fails:

if [[ ! ("$current_value" == "$new_value") ]];

and that means notifications will not work. The work around for this is to end the original instance from within start-clipboard-polling and restart clp-notify as a fresh instance and that's it and then you can delete the section that checks whether clp-notify is running but I don't want to use the work around, I am more interested of why it fails.

Otherwise it will continue to work just fine until you log out.

BTW, you need to:

sudo apt install xclip
| improve this answer | |
  • As implemented, IsClipboardPollingRunning should be called IsClipboardPollingNotRunning, because in shell scripts (unlike most programming) 1 means false and 0 means true. Code like if (IsClipboardPollingRunning == 0) in bash is a bug--it does not compare anything to 0--and in this case that bug happens to undo the bug in IsClipboardPollingRunning. What if (IsClipboardPollingRunning == 0) does is to use a subshell (( )) to call IsClipboardPollingRunning and pass two arguments, == and 0, which are ignored. In this script, if IsClipboardPollingRunning has the same effect. – Eliah Kagan Oct 1 '17 at 8:48
  • I tried your original script at it existed Sep 30 21:27. and dropped it in my ~/bin directory, made executable. and tested. Granted it's an infinite loop, but it worked for me. I made no attempt to log out and in as it wasn't something I personally wanted to automate. It notified as expected on clipboard modification. I didn't test your second script. You might find this Q&A on bash boolean variables interesting. stackoverflow.com/questions/2953646/… – Elder Geek Oct 1 '17 at 19:35
  • Thanks, like I said, you can always end-all clp-notify processes from the second script (which would be starting on start-up) and just re-invoke the first script and that would solve the issue (tested it already). I have left the old version of the script here just for other people to see how I had it originally. – marko Oct 1 '17 at 19:55

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.