Conveniently sharing screenshots

20.09.2015 20:44

When collaborating remotely or when troubleshooting something, it's often useful to quickly share screenshots in a chat like IRC or instant messaging. There are cloudy services that can do image sharing and provide convenient applications that do that. However, if you have your own web server, it seems wasteful to ship screenshots overseas just to be downloaded a moment later by a colleague a few blocks away. Here is a simple setup that instead uses SSH to copy a screenshot to a folder on a web server and integrates nicely with a GNOME desktop.

First, you need a script that pushes files to a public_html folder (or something equivalent) on your web server and reports back the URL:

#!/bin/bash

set -e

# Enter your server's details here...
SSH_DEST="www.example.com:public_html/boxdrop"
URL_BASE="https://www.example.com/~user/boxdrop"

for i in "$@"; do
	base=`basename "$i"`
	abs=`realpath "$i"`

	chmod o+r "$abs"
	scp -pC "$abs" "$SSH_DEST"

	URL="$URL_BASE/$base"
	zenity --title "Shared image" --info --text "<a href="\"$URL\"">$URL</a>"
done

I use Zenity to display the URL in a nice dialog box (apt-get install zenity). Also, don't forget to setup a passwordless SSH login to your web server using your public key.

Name this script share and put it in some convenient folder. A good place is ~/.local/bin, which will also put it in your PATH on most systems (so you can also conveniently share arbitrary files from your terminal). Don't forget to mark the script executable.

You now have to associate this script with some image MIME types. Put a file named share.desktop into ~/.local/share/applications with the following contents. Don't forget to correct the full path to your share script.

[Desktop Entry]
Version=1.0
Type=Application
Name=Share image
Comment=Share an image in a public HTTP folder
Exec=/home/user/.local/bin/share %U
Terminal=false
MimeType=image/png;image/jpx

To check if this worked, open a file manager like Nautilus and right-click on some PNG file. It should now offer to open the file with Share image in addition to other applications usually used with images. If it doesn't work, you might need to run update-desktop-database ~/.local/share/applications.

Finally, install xfce4-screenshooter and associate it with the Print screen button on the keyboard. In GNOME, this can be done in the keyboard preferences:

Associating xfce4-screenshooter with Print screen key in GNOME.

Now, when you press Print screen and make a screenshot, you should have a Share image option under the Open with drop-down menu.

xfce4-screenshooter with "Share image" selected.

When you select it, a window with the URL should appear. You can copy the URL to the chat window, or click it to open it in your browser.

Zenity dialog with the URL of the shared screenshot.

Of course, if the folder you use on your web server is open to the public, so will be the screenshots you share. You might want to have the folder use a secret URL or setup basic HTTP authentication. In any case, it's good to apply some common sense when using your newly found screenshot sharing powers. It's also useful to have a cronjob (systemdjob?) on the server that cleans up the folder with shared images once in a while. This both prevents the web server's disk from filling up and stale images from lingering for too long on the web. Implementing that is left as an exercise for the reader.

Posted by Tomaž | Categories: Code | Comments »