-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdolphin_ultracopier.py
98 lines (87 loc) · 3.33 KB
/
dolphin_ultracopier.py
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
#!/usr/bin/python3
# Dolphin-UltraCopy 0.3
# Release: 28 Aug 2024
#
# Author: ernzo (https://github.com/ernzo)
# Repository: https://github.com/ernzo/Dolphin-UltraCopy
#
# Action script for Dolphin that uses CopyQ to Paste the clipboard content via Ultracopier.
#
# All details and Installation instructions in the README.MD file.
#
# Licensed under GPL3
#
import subprocess
import sys
import urllib.parse
import os
def start_copyq():
"""Ensure CopyQ is running."""
try:
result = subprocess.run(['pgrep', 'copyq'], capture_output=True, text=True)
if result.returncode == 0:
print("CopyQ already running.")
else:
subprocess.Popen(['copyq'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print("CopyQ started.")
except Exception as e:
print(f"Exception starting CopyQ: {e}")
def get_clipboard_data():
"""Retrieve clipboard data from CopyQ."""
try:
result = subprocess.run(['copyq', 'read', '0'], capture_output=True, text=True)
if result.returncode == 0:
data = result.stdout.strip()
if data:
print(f"Clipboard Data Retrieved: {data}")
return data
else:
print("Clipboard Empty.")
return None
else:
print(f"Error running CopyQ command: {result.stderr.strip()}")
return None
except Exception as e:
print(f"Exception retrieving clipboard data: {e}")
return None
def uri_to_path(uri):
"""Convert file URI to local file path."""
if uri.startswith('file://'):
return urllib.parse.unquote(uri[7:])
return uri
def format_sources(sources):
"""Format list of Paths, ensure proper escaping."""
return ' '.join(f'"{source}"' for source in sources)
def main():
"""Execute Copy operation."""
if len(sys.argv) != 2:
print("Usage: paste-clipboard-ultracopier.py <destination_directory>")
sys.exit(1)
destination = uri_to_path(urllib.parse.unquote(sys.argv[1]))
if not os.path.isdir(destination):
print(f"Error: {destination} is not a valid directory.")
sys.exit(1)
start_copyq()
clipboard_data = get_clipboard_data()
if clipboard_data:
sources = [uri_to_path(source) for source in clipboard_data.split('\n') if source.strip()]
sources_formatted = format_sources(sources)
# Quote command string, handle potential issues
command = f'/usr/bin/ultracopier cp {sources_formatted} "{destination}"'
print(f"Executing command: {command}")
try:
# Run command and capture stdout and stderr
result = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
print(f"UltraCopier command output:\n{result.stdout}")
if result.returncode == 0:
print("UltraCopier command executed successfully.")
else:
print(f"UltraCopier command failed with return code: {result.returncode}")
print(f"Error output:\n{result.stderr}")
except subprocess.CalledProcessError as e:
print(f"Exception executing UltraCopier command: {e}")
print(f"Error output:\n{e.stderr}")
else:
print("No valid clipboard data found.")
if __name__ == "__main__":
main()