For years, I’ve been using Linux so much so that it’s my everyday Desktop OS. However, when I use Notepad++ the clipboard of WINE sometimes does not correspond the same with Linux’s clipboard. This could be because of how Notepad++ and WINE communicates with the Linux clipboard, more work needs to be done IMHO. In my usual way, I needed a solution so that the clipboards of Notepad++ with WINE’s clipboard and Linux’s clipboard are the same as in synchronized at all times. This solution therefore, makes Notepad++ at least for me, ideal to develop and program with on Linux.
What I created is two programs:
The first program is a Python3 Linux program, that is a server on Linux, to accept the clipboard copy string from Notepad++ and WINE’s clipboard.
The second program, also a Linux program, however this program utilizes Python3 to:
- invoke os.system
- to run a linux wine program
- install pyperclip
- install xclip
In the second program, Linux WINE runs a version of Python3 installed via wine, and within this version, a few pip must be installed. This program sends the copied string to the server.
pip install using wine and python.exe
- pyperclip
- requests
Server.py – Linux Program #1
import os
import http.server
import socketserver
import json
from datetime import datetime, timedelta
import pyperclip # apt install xclip
from http import HTTPStatus
class ReuseableTCPServer(socketserver.TCPServer):
allow_reuse_address = True
class JSONRequestHandler(http.server.BaseHTTPRequestHandler):
def log_message(self, format, *args):
pass
def do_POST(self):
os.system('clear')
content_length = int(self.headers['Content-Length'])
post_data_bytes = self.rfile.read(content_length)
post_data = json.loads(post_data_bytes.decode('utf-8'))
post_data = json.loads(post_data)
#print(f"""{post_data}""")
a1 = dict(post_data['clipboard'][0])
cnt = 1
for name, v in a1.items():
if v == '':
print(cnt)
continue
pyperclip.copy( f"{v}" )
#print(f'** {cnt} **')
#print(f" pyperclip linux clipboard: {pyperclip.paste()}")
print( f"{pyperclip.paste()}")
response_data = {"status": "success", "print": post_data}
response_code = 200
self.send_response(response_code)
self.send_header("Content-type", "application/json")
response_bytes = json.dumps(response_data).encode('utf-8')
self.send_header("Content-Length", str(len(response_bytes)))
self.end_headers()
# 5. Write the response body
self.wfile.write(response_bytes)
# HTTP 1.1 uses persistent connections by default.
# The connection is reused automatically by the server framework
# unless a 'Connection: close' header is explicitly sent by client or server.
# Set up the server
PORT = 8001
# Use ThreadingTCPServer to handle requests concurrently
# This allows multiple requests, potentially from the same persistent connection, to be handled.
try:
with ReuseableTCPServer(("", PORT), JSONRequestHandler) as httpd:
print(f"Serving at port {PORT} with address reuse enabled")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("server stopped")
except KeyboardInterrupt:
print("Server stopped.")
OK, here is the code of the second program.
wine_windows_pyperclip__clipboard.py – Linux program #2
#!/usr/bin/python3
import os
initial_dir = os.getcwd()
#print(f"Initial directory: {initial_dir}")
os.chdir('/home/username/.wine/drive_c/users/username/AppData/Local/Programs/Python/Python314/')
current_dir = os.getcwd()
#print(f"Current directory: {current_dir}")
# wine python.exe -m pip install pyperclip
# wine python.exe -m pip install requests
windows_python = f"""
import os
import pyperclip
import time
import requests
import json
data = \"\"
url = \"http://127.0.0.1:8001\"
while(1):
dataV = pyperclip.paste()
if data != dataV and dataV != \"\":
a1 = []
json_dict = {{}}
t = 1
id = \"MS_Windows\"
a1.append( (id, dataV) )
json_dict = {{\"clipboard\": [{{\"id\": item[0], \"value\": item[1]}} for item in a1],}}
data_output = json.dumps(json_dict, indent=2)
#print( f\"Clipboard: {{dataV}}\" )
data = dataV
response = requests.post(url, json=data_output)
if response.status_code == 200:
os.system(\"cls\")
print( \"[200] Clipboard: Data JSON (Python dict):\", str(response.json()).replace(r\"\\r\", \"\") )
else:
#continue
print(f\"Request failed with status code: {{response.status_code}}\")
time.sleep(.01)
#for x in range(1, 10000):
# continue
"""
while(1):
try:
os.system(f""" wine ./python.exe -c '{windows_python}' """);
except e:
print('error')
These two scripts perhaps are a little raw or at least very early versions, but work nicely and the same for copy/paste on Linux and WINE as if I was using Notepad++ on Windows.
Happy Coding!