-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclipboard.sh
More file actions
executable file
·287 lines (243 loc) · 7.96 KB
/
clipboard.sh
File metadata and controls
executable file
·287 lines (243 loc) · 7.96 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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/bin/bash
CREDIT="Taz"
HISTORY_FILE="$HOME/.clipboard_history"
IMAGE_DIR="$HOME/.clipboard_images"
MAX_ITEMS=50
DAEMON_PID_FILE="$HOME/.clipboard_daemon.pid"
check_deps() {
for cmd in xclip xdotool zenity; do
if ! command -v $cmd >/dev/null 2>&1; then
notify-send "Clipboard Manager" "Missing: $cmd - Install with: sudo apt install $cmd"
exit 1
fi
done
}
init_history() {
[ ! -f "$HISTORY_FILE" ] && touch "$HISTORY_FILE"
[ ! -d "$IMAGE_DIR" ] && mkdir -p "$IMAGE_DIR"
}
save_to_history() {
image_data=$(xclip -selection clipboard -t image/png -o 2>/dev/null | base64 -w 0)
if [ -n "$image_data" ]; then
timestamp=$(date +%s%N)
image_file="$IMAGE_DIR/clip_$timestamp.png"
echo "$image_data" | base64 -d > "$image_file"
if [ -f "$HISTORY_FILE" ]; then
last=$(head -n 1 "$HISTORY_FILE" 2>/dev/null)
[ "IMAGE:$image_file" = "$last" ] && return
fi
temp=$(mktemp)
echo "IMAGE:$image_file" > "$temp"
[ -f "$HISTORY_FILE" ] && cat "$HISTORY_FILE" >> "$temp"
head -n $MAX_ITEMS "$temp" > "$HISTORY_FILE"
rm -f "$temp"
return
fi
current=$(xclip -selection clipboard -o 2>/dev/null)
[ -z "$current" ] && return
if [ -f "$HISTORY_FILE" ]; then
last=$(head -n 1 "$HISTORY_FILE" 2>/dev/null)
[ "$current" = "$last" ] && return
fi
temp=$(mktemp)
echo "$current" > "$temp"
[ -f "$HISTORY_FILE" ] && cat "$HISTORY_FILE" >> "$temp"
head -n $MAX_ITEMS "$temp" > "$HISTORY_FILE"
rm -f "$temp"
}
show_clipboard_menu() {
if [ ! -f "$HISTORY_FILE" ] || [ ! -s "$HISTORY_FILE" ]; then
zenity --info --title="Clipboard Manager" --text="No clipboard history yet" --width=300 2>/dev/null
return
fi
items=()
line_num=0
while IFS= read -r line; do
[ -z "$line" ] && continue
if [[ "$line" == IMAGE:* ]]; then
image_path="${line#IMAGE:}"
if [ -f "$image_path" ]; then
items+=("$line_num")
items+=("[IMAGE] $(basename "$image_path")")
fi
else
preview=$(echo "$line" | head -c 80)
[ ${#line} -gt 80 ] && preview="${preview}..."
items+=("$line_num")
items+=("$preview")
fi
((line_num++))
[ $line_num -ge $MAX_ITEMS ] && break
done < "$HISTORY_FILE"
[ ${#items[@]} -eq 0 ] && return
selected=$(zenity --list \
--title="Clipboard Manager" \
--text="Select item to copy:" \
--column="ID" --column="Content" \
--hide-column=1 \
--width=700 --height=500 \
"${items[@]}" 2>/dev/null)
[ -z "$selected" ] && return
content=$(sed -n "$((selected + 1))p" "$HISTORY_FILE")
if [ -n "$content" ]; then
if [[ "$content" == IMAGE:* ]]; then
image_path="${content#IMAGE:}"
if [ -f "$image_path" ]; then
xclip -selection clipboard -t image/png -i "$image_path"
notify-send "Clipboard Manager" "✓ Image copied"
fi
else
echo -n "$content" | xclip -selection clipboard
notify-send "Clipboard Manager" "✓ Text copied"
fi
fi
}
clear_history() {
> "$HISTORY_FILE"
rm -rf "$IMAGE_DIR"/*
notify-send "Clipboard Manager" "✓ History cleared"
}
start_daemon() {
if [ -f "$DAEMON_PID_FILE" ]; then
old_pid=$(cat "$DAEMON_PID_FILE")
if ps -p "$old_pid" > /dev/null 2>&1; then
echo "Daemon already running (PID: $old_pid)"
return
fi
fi
check_deps
init_history
nohup bash -c "
while true; do
image_data=\$(xclip -selection clipboard -t image/png -o 2>/dev/null | base64 -w 0)
if [ -n \"\$image_data\" ]; then
timestamp=\$(date +%s%N)
image_file=\"$IMAGE_DIR/clip_\$timestamp.png\"
echo \"\$image_data\" | base64 -d > \"\$image_file\"
if [ -f \"$HISTORY_FILE\" ]; then
last=\$(head -n 1 \"$HISTORY_FILE\" 2>/dev/null)
[ \"IMAGE:\$image_file\" = \"\$last\" ] && sleep 2 && continue
fi
temp=\$(mktemp)
echo \"IMAGE:\$image_file\" > \"\$temp\"
[ -f \"$HISTORY_FILE\" ] && cat \"$HISTORY_FILE\" >> \"\$temp\"
head -n $MAX_ITEMS \"\$temp\" > \"$HISTORY_FILE\"
rm -f \"\$temp\"
sleep 2
continue
fi
current=\$(xclip -selection clipboard -o 2>/dev/null)
[ -z \"\$current\" ] && sleep 2 && continue
if [ -f \"$HISTORY_FILE\" ]; then
last=\$(head -n 1 \"$HISTORY_FILE\" 2>/dev/null)
[ \"\$current\" = \"\$last\" ] && sleep 2 && continue
fi
temp=\$(mktemp)
echo \"\$current\" > \"\$temp\"
[ -f \"$HISTORY_FILE\" ] && cat \"$HISTORY_FILE\" >> \"\$temp\"
head -n $MAX_ITEMS \"\$temp\" > \"$HISTORY_FILE\"
rm -f \"\$temp\"
sleep 2
done
" > /dev/null 2>&1 &
echo $! > "$DAEMON_PID_FILE"
echo "✓ Daemon started (PID: $!)"
}
stop_daemon() {
if [ ! -f "$DAEMON_PID_FILE" ]; then
echo "Daemon not running"
return
fi
pid=$(cat "$DAEMON_PID_FILE")
if ps -p "$pid" > /dev/null 2>&1; then
kill "$pid" 2>/dev/null
echo "✓ Daemon stopped"
else
echo "Daemon not running"
fi
rm -f "$DAEMON_PID_FILE"
}
setup_autostart() {
autostart_dir="$HOME/.config/autostart"
autostart_file="$autostart_dir/clipboard-manager.desktop"
script_path="$(readlink -f "$0")"
mkdir -p "$autostart_dir"
cat > "$autostart_file" << EOF
[Desktop Entry]
Type=Application
Name=Clipboard Manager Daemon
Comment=Auto-save clipboard history
Exec=bash -c 'sleep 5 && $script_path --daemon'
Terminal=false
Hidden=false
NoDisplay=true
X-GNOME-Autostart-enabled=true
StartupNotify=false
EOF
chmod +x "$autostart_file"
echo "✓ Autostart enabled!"
echo ""
echo "Starting daemon now..."
start_daemon
echo ""
echo "📝 Next steps:"
echo " 1. Set keyboard shortcut Super+V (Win+V)"
echo " 2. Command: $script_path"
echo " 3. Copy text or images and test!"
}
remove_autostart() {
autostart_file="$HOME/.config/autostart/clipboard-manager.desktop"
if [ -f "$autostart_file" ]; then
rm -f "$autostart_file"
echo "✓ Autostart disabled"
stop_daemon
else
echo "Autostart not configured"
fi
}
case "$1" in
--daemon)
start_daemon
;;
--stop)
stop_daemon
;;
--clear)
clear_history
;;
--setup)
setup_autostart
;;
--remove)
remove_autostart
;;
--status)
if [ -f "$DAEMON_PID_FILE" ]; then
pid=$(cat "$DAEMON_PID_FILE")
if ps -p "$pid" > /dev/null 2>&1; then
echo "✓ Daemon running (PID: $pid)"
else
echo "✗ Daemon not running"
fi
else
echo "✗ Daemon not running"
fi
;;
*)
check_deps
init_history
if [ ! -f "$DAEMON_PID_FILE" ]; then
response=$(zenity --question \
--title="Clipboard Manager" \
--text="Daemon not running!\n\nDo you want to enable auto-start?" \
--ok-label="Yes, Enable" \
--cancel-label="No, Just Show" \
--width=400 2>/dev/null)
if [ $? -eq 0 ]; then
setup_autostart
fi
fi
save_to_history
show_clipboard_menu
;;
esac