-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclipboard-files-kde-plasma
executable file
·241 lines (210 loc) · 6.15 KB
/
clipboard-files-kde-plasma
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/bin/bash
# clipboard-files-kde-plasma
# - Cut, copy and paste files from the clipboard in a Linux terminal.
#
# Read more: https://github.com/larspontoppidan/clipboard-files
#
# By Lars Ole Pontoppidan
#
# Clipboard interfacing code inspired from:
# http://murga-linux.com/puppy/viewtopic.php?t=111880&start=15
# This is a specific version of the clipboard-files script made to work as well as
# possible for KDE Plasma 5.X, which uses the clipboard in a slightly more complicated
# manner for files on the clipboard.
#
# The script features:
# - Copying files from Dolphin to terminal
# - Cutting files from Dolphin to terminal
# - Copying files from terminal to Dolphin
# - Copying files from terminal to terminal
# - Cutting files from terminal to Dolphin is NOT supported (the ccut operation)
#
# The main challenge is that multiple TARGETs on the X11 clipboard are generally used
# by Dolphin for file operations, while the script relies on the xclip terminal command
# which is not able to set multiple clipboard targets. But it is able to read them.
# Thus, xclip can't register a proper Dolphin cut/copy command. It was found that copying
# files with the target: text/uri-list does work, however, so as a compromise the
# following strategy is used:
#
# cpaste/cshow/ccd reads the following clipboard targets:
# - application/x-kde-cutselection (if 1 it's a cut operation)
# - application/x-kde4-urilist
# - text/uri-list (if application/x-kde4-urilist is empty, this implies a copy op.)
#
# ccopy writes:
# - text/uri-list
#
SCRIPT_REV=2022-04-24
function syntax_error {
echo "$(basename -- $0): Invalid command line"
echo "Try 'clipboard-files-kde-plasma' for help"
}
function show_help {
echo "clipboard-files-kde-plasma - cut, copy and paste files from clipboard, rev. $SCRIPT_REV"
echo ""
echo "Usage:"
echo " ccopy FILE [FILE]... Copy files or folders to clipboard"
echo " cpaste [DIR] Paste files from clipboard into DIR or current dir"
echo " cshow Show files on clipboard"
echo " cclear Clear clipboard without any file operations"
echo " . ccd cd to the folder of the 1st file on clipboard"
echo ""
echo "If the usage commands aren't working check the installation steps provided here:"
echo "https://github.com/larspontoppidan/clipboard-files"
echo ""
}
# Is this script being sourced?
if [[ -z $BASH_SOURCE ]] || [[ $(basename ${0#-}) != ${BASH_SOURCE##*/} ]]; then
# Being sourced
sourced=true
EXIT=return
else
# Running directly
sourced=false
EXIT=exit
fi
if [[ $0 == *"ccopy" ]]; then
op=copy
elif [[ $0 == *"ccut" ]]; then
echo "ccut not supported, exiting"
$EXIT
elif [[ $0 == *"cpaste" ]]; then
op=paste
elif [[ $0 == *"cshow" ]]; then
op=show
elif [[ $0 == *"cclear" ]]; then
op=clear
elif [[ $sourced == true ]]; then
# When this is sourced, assume ccd
op=cd
else
show_help
$EXIT
fi
xclip -version 1>/dev/null 2>/dev/null
result=$?
if [ $result != 0 ]; then
echo "xclip doesn't seem to be installed, exiting"
$EXIT
fi
if [ "${op}" = paste ] || [ "${op}" = show ] || [ "${op}" = cd ]; then
target_dir=.
if [[ $op == paste && $# -eq 1 ]]; then
target_dir=$1
elif [[ $# -ne 0 ]]; then
syntax_error
$EXIT
fi
raw=$(xclip -o -selection clipboard -t application/x-kde4-urilist 2>/dev/null)
result=$?
if [ $result != 0 ] || [ ${#raw} == 0 ]; then
paste_mode=copy
raw=$(xclip -o -selection clipboard -t text/uri-list 2>/dev/null)
result=$?
if [ $result != 0 ] || [ ${#raw} == 0 ]; then
echo "No files on clipboard"
$EXIT
fi
else
mode_raw=$(xclip -o -selection clipboard -t application/x-kde-cutselection 2>/dev/null)
if [ "${mode_raw}" = "1" ]; then
paste_mode=cut
else
paste_mode=copy
fi
fi
if [ "${op}" = "show" ]; then
echo "Operation: ${paste_mode}"
elif [ "${op}" = "cd" ]; then
true
elif [ "${paste_mode}" = "cut" ]; then
echo "Moving the following items(s) to $target_dir"
else
echo "Copying the following items(s) to $target_dir"
fi
urldecode() { echo -e "${*//%/\\x}"; }
filelist=$(urldecode "$raw")
carriage_return=$(echo -ne '\r')
while read line
do
line=${line//${carriage_return}/}
if [[ $line == file:\/\/* ]]; then
filename=${line#file:\/\/}
else
# certain programs may add the file path to the clipboard
# with the "file:" prefix, instead of "file://"
filename=${line#file:}
fi
if [ "${op}" = cd ]; then
path=$(dirname -- "${filename}")
cd "${path}"
break
fi
echo ${filename}
if [ "${op}" != show ]; then
if [ -e "${filename}" ]; then
if [ "${paste_mode}" = "cut" ]; then
mv -f "${filename}" "$target_dir"
else
cp -af --no-preserve=context,links "${filename}" "$target_dir"
fi
else
echo "File doesn't exist or error: ${filename}"
fi
fi
done <<< "${filelist}"
if [ "${op}" = paste ] && [ "${paste_mode}" = "cut" ]; then
echo -n | xclip -selection clipboard
fi
elif [ "${op}" = copy ]; then
if [ $# -lt 1 ]; then
syntax_error
$EXIT
fi
count=0
clipboard=
for one_arg in "$@"
do
if [[ ${one_arg} == -* ]]; then
syntax_error
$EXIT
fi
filename=$(realpath "${one_arg}")
if [ -e "${filename}" ]; then
if [ ${#clipboard} == 0 ]; then
clipboard="file://${filename}"
else
clipboard="${clipboard}
file://${filename}"
fi
count=$((count+1))
else
echo "File doesn't exist or error: ${filename}"
fi
done
clipboard="${clipboard}
"
if [ $count -ge 1 ]; then
echo -n "${clipboard}" | xclip -i -selection clipboard -t text/uri-list
result=$?
if [ $result != 0 ]; then
echo "Error copying to clipboard"
else
echo "${count} item(s) copied to clipboard"
fi
fi
elif [ "${op}" = clear ]; then
if [ $# -ne 0 ]; then
syntax_error
$EXIT
fi
echo -n | xclip -selection clipboard
result=$?
if [ $result != 0 ]; then
echo "Error clearing clipboard"
else
echo "Clipboard cleared"
fi
else
syntax_error
fi