-
Notifications
You must be signed in to change notification settings - Fork 0
/
scmenu
executable file
·146 lines (126 loc) · 3.86 KB
/
scmenu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/bash
# https://github.com/b4skyx/scmenu
# Check whether the dependencies have been installed or not
type curl &>/dev/null || {
echo "Couldn't find curl, dependency not satisfied." >&2
exit 17
}
type maim &>/dev/null || {
echo "Couldn't find maim, which is required to take screenshots." >&2
exit 17
}
# Fallback to g if rofi is not found
g_cmd="rofi -dmenu -l 4"
type rofi&>/dev/null || {
type g&>/dev/null || {
echo "Neither rofi nor g installed..." >&2
exit 17
}
g_cmd="dmenu"
}
# Help message
function helpfn() {
echo "Scmenu, a screenshot wrapper built around maim" >&2
echo "Menu navigation through g/rofi" >&2
echo "Note: Make sure you have the dependencies installed" >&2
echo "Usage: Put this script in your path and bind your desired key for quick launch"
}
# Set inital options and choices
save_folder="$HOME/Screenshots"
options=("Fullscreen\nSelection\nCurrent Window\nTimer")
choice=""
# Update choice as required
function get_choice() {
if [ -e /tmp/deletion_link ]
then
options+=$"\nDeletion Link"
fi
choice=$(echo -e "$options" | $g_cmd -p " ")
}
# Processing the choices made by the user
function runner() {
file_name=$(date +Screenshot_%d%m%y_%H%M%S.png)
if [ "$choice" = "Timer" ]
then
seconds=`$g_cmd -p " Seconds"`
choice=$(echo -e "Fullscreen\nCurrent Window" | $g_cmd -p " ")
sleep "$seconds"
runner
fi
if [ "$choice" = "Deletion Link" ]
then
xclip -selection clipboard < /tmp/deletion_link
rm /tmp/deletion_link
notify-send -i /tmp/screenshot.png "Deletion Link copied"
elif [ "$choice" = "Fullscreen" ]
then
sleep 0.2
maim /tmp/screenshot.png
save_clip
elif [ "$choice" = "Selection" ]
then
maim -s /tmp/screenshot.png
save_clip
elif [ "$choice" = "Current Window" ]
then
maim -i $(xdotool getactivewindow) /tmp/screenshot.png
save_clip
fi
}
# Upload screenshot to imgur
function upload_to_imgur(){
# Set client_id for the imgur api
# Replace this with your own id if required
default_client_id=c9a6efb3d7932fd
client_id="${IMGUR_CLIENT_ID:=$default_client_id}"
# Send a request through curl of the screenshot
response=`curl -s -H "Authorization: Client-ID $client_id" -H "Expect: " -F "image=@/tmp/screenshot.png" https://api.imgur.com/3/image.xml`
# Error Handling
# Check for the response code. If failed, stdout with appropriate message.
if [ $? -ne 0 ]; then
echo "Upload failed" >&2
exit 1
# Else parse the response and check for upload status.
elif echo "$response" | grep -q 'success="0"'; then
echo "Error message:" >&2
msg="${response##*<error>}"
echo "${msg%%</error>*}" >&2
exit 1
fi
# If uploaded successfully, parse the response further to get the image and deletion url.
url="${response##*<link>}"
url="${url%%</link>*}"
delete_hash="${response##*<deletehash>}"
delete_hash="${delete_hash%%</deletehash>*}"
# Copy the image link to clipboard and save the deletion_link for further use.
echo $url | sed 's/^http:/https:/' | xclip -selection clipboard
echo "https://imgur.com/delete/$delete_hash" > /tmp/deletion_link
}
# Function to save the screenshot
function save_clip(){
options=("Copy to Clipboard\nSave Image\nUpload to Imgur")
choice=$(echo -e "$options" | $g_cmd -p " ")
if [ "$choice" = "Copy to Clipboard" ]
then
xclip -selection clipboard -t image/png /tmp/screenshot.png
notify-send -i /tmp/screenshot.png "Screenshot Captured!!" "Copied to clipboard"
elif [ "$choice" = "Save Image" ]
then
cp /tmp/screenshot.png $save_folder/$file_name
notify-send -i /tmp/screenshot.png "Screenshot Saved!!" "$file_name"
elif [ "$choice" = "Upload to Imgur" ]
then
notify-send "Uploading to Imgur"
upload_to_imgur
notify-send -i /tmp/screenshot.png -i "Uploaded to Imgur!" "Imgur URL copied to Clipboard."
fi
}
if [ "$1" == "-h" -o "$1" == "--help" ]; then
helpfn
exit 0
elif [ $# -eq 0 ]; then
get_choice
runner
else
echo "Bad arguments passed.">&2
fi