aboutsummaryrefslogtreecommitdiffstats
path: root/contrib
diff options
context:
space:
mode:
authorLibravatar Lauri <lauri@icantfeelmylegs.com>2019-10-09 14:10:23 +0300
committerLibravatar Drew DeVault <sir@cmpwn.com>2019-10-16 10:08:08 -0400
commit992726a823e3a31920aa0a774a7985b8b11a3536 (patch)
treefcec476c182cd30875cbbe304ec70f12887d20e4 /contrib
parentbuild: always use the project version (diff)
downloadsway-992726a823e3a31920aa0a774a7985b8b11a3536.tar.gz
sway-992726a823e3a31920aa0a774a7985b8b11a3536.tar.zst
sway-992726a823e3a31920aa0a774a7985b8b11a3536.zip
Grimshot: a helper for screenshots within sway
Usage: grimshot copy|save win|screen|area [FILE] Troubleshoot: grimshot check Requirements: - `grim`: screenshot utility for wayland - `slurp`: to select an area - `swaymsg`: to read properties of current window - `wl-copy`: clipboard utility - `jq`: json uliity to parse swaymsg output - `notify-send`: to show notifications Those are needed to be installed, if unsure, run `grimshot check` Examples: `grimshot copy win` - to copy current window `grimshot save area` - to select area and save it to default file (Pictures/Grimshot-$datetime.png) `grimshot save screen ~/screenshot.png` - to save screenshot under ~/screenshot.png `grimshot` - usage `grimshot check` - verify if tools are installed
Diffstat (limited to 'contrib')
-rwxr-xr-xcontrib/grimshot113
1 files changed, 113 insertions, 0 deletions
diff --git a/contrib/grimshot b/contrib/grimshot
new file mode 100755
index 00000000..de2e7a7f
--- /dev/null
+++ b/contrib/grimshot
@@ -0,0 +1,113 @@
1#!/bin/sh
2
3## Grimshot: a helper for screenshots within sway
4## Requirements:
5## - `grim`: screenshot utility for wayland
6## - `slurp`: to select an area
7## - `swaymsg`: to read properties of current window
8## - `wl-copy`: clipboard utility
9## - `jq`: json uliity to parse swaymsg output
10## - `notify-send`: to show notifications
11## - `mktemp`: to create a temporary file
12## Those are needed to be installed, if unsure, run `grimshot check`
13##
14## Examples:
15## `grimshot copy win` - to copy current window
16## `grimshot save area` - to select area and save it to default file (Pictures/Grimshot-$datetime.png)
17## `grimshot save screen ~/screenshot.png` - to save screenshot under ~/screenshot.png
18## `grimshot` - usage
19## `grimshot check` - verify if tools are installed
20
21ACTION=${1:-usage}
22SUBJECT=${2:-screen}
23FILE=${3:-$(xdg-user-dir PICTURES)/$(date +'Grimshot %Y-%m-%d %H-%M-%S.png')}
24if [ "$ACTION" = "usage" ] ; then
25 echo "Usage:"
26 echo " grimshot copy|save win|screen|area [FILE]"
27 echo "Troubleshoot:"
28 echo " grimshot check"
29 exit
30fi
31
32notify() {
33 notify-send -t 3000 -a grimshot "$@"
34}
35notifyOk() {
36 TITLE=${2:-"Screenshot"}
37 MESSAGE=${1:-"OK"}
38 notify "$TITLE" "$MESSAGE"
39}
40notifyError() {
41 TITLE=${2:-"Screenshot"}
42 MESSAGE=${1:-"Error taking screenshot with grim"}
43 notify -u critical "$TITLE" "$MESSAGE"
44}
45
46die() {
47 MSG=${1:-Bye}
48 notifyError "Error: $MSG"
49 exit 2
50}
51
52check() {
53 COMMAND=$1
54 command -v "$COMMAND" > /dev/null 2>&1
55 if [ $? ]; then
56 RESULT="OK"
57 else
58 RESULT="NOT FOUND"
59 fi
60 echo " $COMMAND: $RESULT"
61}
62
63takeScreenshot() {
64 FILE=$1
65 GEOM=$2
66 if [ -z "$GEOM" ]; then
67 grim "$FILE" || die "Unable to invoke grim"
68 else
69 grim -g "$GEOM" "$FILE" || die "Unable to invoke grim"
70 fi
71}
72
73if [ "$ACTION" = "check" ] ; then
74 echo "Checking if required tools are installed. If something is missing, install it to your system and make it available in PATH..."
75 check grim
76 check slurp
77 check swaymsg
78 check wl-copy
79 check jq
80 check notify-send
81 check mktemp
82 exit
83elif [ "$SUBJECT" = "area" ] ; then
84 GEOM=$(slurp -d)
85 WHAT="Area"
86elif [ "$SUBJECT" = "win" ] ; then
87 FOCUSED=$(swaymsg -t get_tree | jq -r 'recurse(.nodes[]?, .floating_nodes[]?) | select(.focused)')
88 GEOM=$(echo "$FOCUSED" | jq -r '.rect | "\(.x),\(.y) \(.width)x\(.height)"')
89 APP_ID=$(echo "$FOCUSED" | jq -r '.app_id')
90 WHAT="$APP_ID window"
91elif [ "$SUBJECT" = "screen" ] ; then
92 GEOM=""
93 WHAT="Screen"
94else
95 die "Unknown subject to take a screen shot from" "$SUBJECT"
96fi
97
98if [ "$ACTION" = "copy" ] ; then
99 TMP=$(mktemp) || die "Unable to create temp file: is mktemp installed?"
100 takeScreenshot "$TMP" "$GEOM"
101 wl-copy --type image/png < "$TMP" || die "Clipboard error"
102 rm "$TMP"
103 notifyOk "$WHAT copied to buffer"
104else
105 takeScreenshot "$FILE" "$GEOM"
106 if [ $? ]; then
107 TITLE="Screenshot of $SUBJECT"
108 MESSAGE=$(basename "$FILE")
109 notifyOk "$MESSAGE" "$TITLE"
110 else
111 notifyError "Error taking screenshot with grim"
112 fi
113fi