You are Here: Articles » A Script to Automatically Upload Screenshots to the Web from Ubuntu

A Script to Automatically Upload Screenshots to the Web from Ubuntu

When the print screen button is pressed, take a screenshot, upload it to the specified location with FTP and copy the URL to the clipboard.

Tagged with Software, Open Source, Ubuntu and Graphics
Posted on 22/11/08 by Paul Herron

To show what's on my screen to someone who isn't with me, the easiest solution is usually to grab a screenshot, upload it to the Web and send out the URL. On Windows, I've used the basic but functional SnapTake to help with that, and there are various other desktop- and web-based solutions.

Although I coulnd't find any one Ubuntu package to do the same thing, a few programs combined could, and these were easily patched together with a small bash script.

Dependencies

We'll need a few packages to make this work, so from the terminal, issue the following command:

  1. sudo apt-get install scrot ncftp xclip compizconfig-settings-manager

scrot will take care of grabbing the screenshot and saving it locally. ncftp will let us take that file and upload it with FTP. xclip will take the URL of the uploaded file and insert it into the clipboard. CompizConfig Settings Manager will let us easily bind this whole process to the Print Screen key.

Making the Script

The script is just some variable declarations and a few commands:

  1. #!/bin/bash
  2.  
  3. # Declare FTP access details
  4. FTPSERVER=ftp.example.com
  5. USERNAME=screenshots@example.com
  6. PASSWORD=password
  7. # Declare the local directory to which the file should be saved.
  8. dir=~/screenshots
  9. # Declare a filename. I used a timestamp as a simple way to get a unique filename.
  10. filename=myscreenshot-$(date +%Y-%m-%d_%H%M%S)
  11. # Declare the URL of the directory to which the file will be uploaded.
  12. url=http://example.com/screenshots/
  13.  
  14. # After a delay of 2 seconds, save a PNG screenshot.
  15. scrot -d 2 -q 1  "$dir/$filename.png"
  16. # Upload the screenshot.
  17. ncftpput -u $USERNAME -p $PASSWORD   $FTPSERVER$dir/$filename.png
  18. # Copy the URL of the uploaded screenshot to the clipboard.
  19. echo "$url$filename.png" | xclip -selection c
  20.  
  21. exit

We could just put the script in /usr/bin, but it's useful to create a personalised bin in the home directory, ~/bin. This way, all of your personal scripts can be backed up along with your other documents. Saving the file as ~/bin/shoot is therefore a good way to go.

If they aren't there already, you might need to add the following lines to your ~/.profile or ~/.bash_profile file. This will let us simply call shoot from the terminal, rather than specifying the full path to the script.

  1. # set PATH so it includes user's private bin if it exists
  2. if [ -d "$HOME/bin" ] ; then
  3.     PATH="$HOME/bin:$PATH"
  4. fi

The script should now work if we call shoot from the terminal.

Binding the Script to the Print Screen Key

Handily, we can use Compiz's built-in key binding management to call shoot when the Print Screen button is pressed. CCSM gives us an interface for this. Just select General, then General Options, then choose the Commands tab. Under Screenshot commands, enter shoot in the Sceenshot command line box.

Enter 'shoot' in the box labelled 'Sceenshot command line'.

Improvements

This script has just the bare minimum functionality, but does just what I need. Some improvements could be handy, though:

  • Allow individual windows to be captured, rather than just the entire screen.
  • Include error detection and messages. At the moment, the script just fails silently in the background if something goes wrong.
  • Give a prompt when the file has been uploaded. The prompt could offer functionality such as copying the URL to the clipboard as options.

I'll have a look at those when I get time.

Leave a Comment

CAPTCHA[Refresh]

« Articles

Article Tags

Show all articles, or just those tagged as:

Feed

The articles RSS feed is available.

Elsewhom

See More…

Back to top.