Can I get visual feedback from copying wth Ctrl+C?
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?
I adapted my script to react on clipboard changes from here so that it shows a native notification bubble whenever you copy something:
#!/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.
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