-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclip.py
More file actions
166 lines (139 loc) · 5.44 KB
/
Copy pathclip.py
File metadata and controls
166 lines (139 loc) · 5.44 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
import asyncio
import platform
# To avoid sending back items that were sent from another device
last_sent_item = None
PLATFORM = platform.system().lower()
# https://stackoverflow.com/a/62517779
async def __get_most_recent_clipboard_item():
if PLATFORM == "windows":
proc = await asyncio.create_subprocess_exec(
"powershell",
"-Command", "Get-Clipboard",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, _ = await proc.communicate()
return stdout.decode().strip()
elif PLATFORM == "linux":
proc = await asyncio.create_subprocess_exec(
"which",
"termux-clipboard-get",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, _ = await proc.communicate()
if len(stdout.decode().strip()) > 0 and stdout.decode().strip()[0] == "/":
proc = await asyncio.create_subprocess_exec(
"termux-clipboard-get",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, _ = await proc.communicate()
return stdout.decode().strip()
else:
pass
proc = await asyncio.create_subprocess_exec(
"which",
"xclip",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, _ = await proc.communicate()
if len(stdout.decode().strip()) > 0 and stdout.decode().strip()[0] == "/":
proc = await asyncio.create_subprocess_exec(
"xclip",
"-sel", "clip", "-o",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, _ = await proc.communicate()
return stdout.decode().strip()
else:
pass
raise ValueError(
"This program cannot work without xclip or termux available on the target device \
Either use a supported OS, install the needed programs, or open an issue on the repository, \
asking for support: \
https://github.com/ARandomBoiIsMe/clipshare/issues"
)
else:
raise ValueError(
"This OS is not supported. Open an issue on the repository, asking for support: \
https://github.com/ARandomBoiIsMe/clipshare/issues"
)
async def wait_for_new_clipboard_addition():
global last_sent_item
last_addition = await __get_most_recent_clipboard_item()
while True:
current_addition = await __get_most_recent_clipboard_item()
if current_addition == last_addition:
await asyncio.sleep(0.5)
continue
if current_addition == last_sent_item:
await asyncio.sleep(0.5)
continue
if type(current_addition) != str:
await asyncio.sleep(0.5)
continue
return current_addition
async def get_from_clipboard():
return await wait_for_new_clipboard_addition()
async def __set_most_recent_clipboard_item(item):
if PLATFORM == "windows":
proc = await asyncio.create_subprocess_exec(
"powershell",
"Set-Clipboard", "-Value", f"{item}",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
await proc.communicate()
elif PLATFORM == "linux":
proc = await asyncio.create_subprocess_exec(
"which",
"termux-clipboard-get",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, _ = await proc.communicate()
if len(stdout.decode().strip()) > 0 and stdout.decode().strip()[0] == "/":
proc = await asyncio.create_subprocess_exec(
"termux-clipboard-set",
f"{item}",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
return
else:
pass
proc = await asyncio.create_subprocess_exec(
"which",
"xclip",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, _ = await proc.communicate()
if len(stdout.decode().strip()) > 0 and stdout.decode().strip()[0] == "/":
proc = await asyncio.create_subprocess_exec(
"echo",
f"{item}", "|", "xclip", "-sel", "clip", "-i",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
return
else:
pass
raise ValueError(
"This program cannot work without xclip or termux available on the target device \
Either use a supported OS, install the needed programs, or open an issue on the repository, \
asking for support: \
https://github.com/ARandomBoiIsMe/clipshare/issues"
)
else:
raise ValueError(
"This OS is not supported. Open an issue on the repository, asking for support: \
https://github.com/ARandomBoiIsMe/clipshare/issues"
)
async def save_to_clipboard(item):
global last_sent_item
await __set_most_recent_clipboard_item(item)
last_sent_item = item