milianw / shell-helpers

a bunch of little programs I wrote to make my life on the command line easier and less type-intensitive

shell-helpers / clipboard
100755 39 lines (35 sloc) 1.152 kb
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
#!/bin/bash
 
# Access your KDE 4 klipper on the command line
# usage:
# ./clipboard
# will output current contents of klipper
# echo "foobar" | ./clipboard
# will put "foobar" into your clipboard/klipper
 
# check for stdin
# since we don't want to wait endlessly we set a timeout
# a pity `read` only supports seconds and no fractions...
read -t 1 stdin
if [[ "$stdin" != "" ]]; then
  # get the rest of stdin
  stdin=$stdin$'\n'$(cat)
  # oh, nice - user input! we set that as current
  # clipboard content
  dbus-send --type=method_call --dest=org.kde.klipper \
    /klipper org.kde.klipper.klipper.setClipboardContents \
    string:"$stdin"
  exit
fi
 
# if we reach this point no user input was given and we
# print out the current contents of the clipboard
# note: I hate the output of dbus, dcop was much easier in that regard!
dbus-send --print-reply --dest=org.kde.klipper /klipper \
    org.kde.klipper.klipper.getClipboardContents | awk '
BEGIN { output = ""; }
{
if ( NR > 1 ) {
output = output $0 "\n";
}
}
END {
print substr(output, 12, length(output) - 13);
}'