Merge branch 'master' of ssh://git.slaskete.net/srv/git/einar-bin
[einar-bin] / screenshot.sh
1 #!/bin/bash
2 # A quick and dirty script to take a screenshot, upload it via scp
3 # and put a link to it in the clipboard for easy pasting to i.e. IRC.
4 # It assumes a lot of things, e.g. that ssh to the target host has
5 # been set up with a working logon with private key, and that xclip and
6 # gnome-screenshot is installed.
7 #
8 # If all you have is a hammer, everything looks like a nail.
9
10 # TODO:
11 # * Undo function
12 # * Check that we don't overwrite an existing file (theoretically possible, but unlikely)
13
14 # Settings
15 RemoteUser="einar"
16 RemoteHost="wowbagger.slaskete.net"
17 RemotePath="/var/www/eina.rjh.im/screenshots/"
18 LocalPath="${HOME}/Pictures/Screenshots/"
19 UrlBase="https://eina.rjh.im/g"
20
21 function errcho() {
22 >&2 echo -e "$@"
23 }
24
25 function take_screenshot() {
26 FileID="$(tr -dc 'a-zA-Z0-9' < /dev/urandom | fold -w 6 | head -n 1)"
27 FileName="${FileID}.png"
28 if [ "$*" = "clipboard" ] || [ "$*" = "jpg" ]; then
29 xclip -out -selection clipboard -t image/png > "/tmp/${FileName}" 2>/dev/null
30 if [ "$(file -b --mime-type "/tmp/${FileName}")" == "image/png" ]; then
31 if [ "$*" = "jpg" ]; then
32 convert "/tmp/${FileName}" "/tmp/${FileID}.jpg"
33 rm "/tmp/${FileName}"
34 FileName="${FileID}.jpg"
35 fi
36 mv "/tmp/${FileName}" "${LocalPath}/"
37 else
38 rm "/tmp/${FileName}"
39 notify-send -a "screenshot.sh" -i dialog-warning "Ooopsie" "Tried to post image from clipboard, but found no image there."
40 fi
41 else
42 gnome-screenshot -f "${LocalPath}/${FileName}" -p "$@"
43 fi
44 if [ -f "${LocalPath}/${FileName}" ]; then
45 scp -q "${LocalPath}/${FileName}" "${RemoteUser}@${RemoteHost}:${RemotePath}"
46 echo -n "${UrlBase}/${FileName}"|xclip -selection p
47 echo -n "${UrlBase}/${FileName}"|xclip -selection c
48 notify-send -a "screenshot.sh" -i applets-screenshooter "Screenshot uploaded" "Screenshot published to ${UrlBase}/${FileName}"
49 fi
50 }
51
52 function show_help() {
53 errcho "Usage: screenshot.sh [option]"
54 errcho "This script wraps gnome-screenshot in order to instantly upload screenshots and get back a URL for sharing to IRC or other social media"
55 errcho "The default option is to take a screenshot of the whole screen."
56 errcho "The following options are available:"
57 errcho "-f, --full Take screenshot of the whole screen"
58 errcho "-w, --window Take screenshot of the active window"
59 errcho "-a, --area Take screenshot of an area/selection"
60 errcho "-s, --selection Take screenshot of an area/selection"
61 errcho "-c, --clipboard Upload image from paste buffer if present"
62 errcho "-p, --paste Upload image from paste buffer if present"
63 errcho "-j, --jpg, --jpeg Convert image from paste buffer to jpeg and upload"
64 errcho "-h, --help Show this help"
65 exit 1
66 }
67
68 case ${1} in
69 -w|--window)
70 take_screenshot -w -b
71 ;;
72 -a|--area|-s|--selection)
73 sleep 0.2
74 take_screenshot -a
75 ;;
76 ""|-f|--full)
77 take_screenshot
78 ;;
79 -c|--clipboard|-p|--paste)
80 take_screenshot clipboard
81 ;;
82 -j|--jpg|--jpeg)
83 take_screenshot jpg
84 ;;
85 -h|--help)
86 show_help
87 ;;
88 *)
89 errcho "ERROR: Unknown option."
90 show_help
91 ;;
92 esac
93