-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclip.sh
executable file
·83 lines (68 loc) · 2.19 KB
/
clip.sh
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
#!/bin/sh
set -u
# TARGETS configuration
_pref="
image/png
image/bmp
image/jpeg
text/uri-list
code/file-list
"
_utf8="UTF8_STRING"
_out="/tmp/xclip.out"
_history="/tmp/xclip.history"
_script="$(basename "${0}")"
_log() {
printf '[\033[94m%s\033[0m] %s' "${_script}" "${1}" | xargs >&2
}
_usage() {
_log "Invalid arguments."
exit 1
}
[ "${#}" -gt 0 ] && _usage
_iteration() {
# target test of current selection
_tc="$(xclip -selection clipboard -o -t TARGETS)"
_tcec="${?}"
_log "TARGETS check exited with: ${_tcec}"
if [ "${_tcec}" -ne 0 ]
then
# on empty wait for any selection
_log "Waiting on initial selection with: ${_utf8}"
# take ownership of clipboard so we block the loop and wait for someone to take ownership
# if this fails probably X connection is lost, so exit the script
xclip -verbose -in -selection clipboard -t "${_utf8}" /dev/null || exit 3
else
_log "Clipboard targets: ${_tc}"
_log "Preferred targets: ${_pref}"
_log "Default targets: ${_utf8}"
# join both lists together, and print first item of targets occuring in _pref
_match="$(printf '%s\n%s\n%s\n' "${_tc}" "${_pref}" "${_utf8}" | grep -v '^\s*$' | awk 'a[$0]++' | head -n1)"
if [ -n "${_match}" ]
then
_log "Matched target: ${_match}"
# put clipboard content into temp file
xclip -verbose -out -selection clipboard -t "${_match}" > "${_out}"
_log "xclip out exited"
if [ "${_match}" = "${_utf8}" ]
then
printf '%s\n' "$(cat "${_out}")" >> "${_history}"
_log "Added to history ${_history}"
fi
# read temp file, take ownership of clipboard and wait for pastes
# after something else is copied, xclip loses ownership and exits, and another iteration begins
xclip -verbose -in -selection clipboard -t "${_match}" "${_out}"
_log "xclip in exited"
else
_log "Unable to match targets"
sleep 1
fi
fi
}
_log "${_script} @ $(readlink /proc/$$/exe)"
while true
do
_log "Start"
_iteration
_log "End"
done