-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchangexseltextcase
executable file
·50 lines (43 loc) · 1.43 KB
/
changexseltextcase
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
#!/usr/bin/env bash
#
# File:
# changexseltextcase
#
# Description:
# Change the case of the selected text.
#
# Usage:
# changexseltextcase (--upper | --lower)
#
# Notes:
# This utility should be used with a keybinding or in a script.
#
# If the text is not being converted, increase the usleep time in the xte
# command.
#
case "${1}" in
'--upper'|'--lower')
readonly MODE="${1}"
;;
*)
# output errors as a desktop notification since this script is meant to be
# used with a keybinding and errors printed to stderr will not be seen.
msg='Mode not specified or invalid; must use option\n--upper or --lower'
notify-send 'changecase (err)' "${msg}"
exit 1
;;
esac
readonly PREV_CLIPBOARD_CONTENT="$(xclip -o -selection c)"
# use "xclip -out -selection primary" instead of "xsel" as it causes the
# selected text to be unselected in some applications.
readonly SEL_TEXT="$(xclip -out -selection primary)"
if [ "${MODE}" = '--upper' ]; then
chngdCase="$(echo -n "${SEL_TEXT}" | tr '[:lower:]' '[:upper:]')"
else
chngdCase="$(echo -n "${SEL_TEXT}" | tr '[:upper:]' '[:lower:]')"
fi
echo -n "${chngdCase}" | xclip -selection c
xte 'usleep 200000' 'keydown Control_L' 'key v' 'keyup Control_L'
# Must sleep before restoring the clipboard content because otherwise the
# keyevents will run after, causing it to get pasted instead.
(sleep .1 && echo -n "${PREV_CLIPBOARD_CONTENT}" | xclip -selection c) &