Forked from mpcabd/python-mac-set-html-clipboard.py
Last active
January 18, 2023 13:35
-
-
Save fengyie007/6fefde6cbee6537d107dff9979b3ea74 to your computer and use it in GitHub Desktop.
Python3 - Set Mac clipboard with HTML content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
html = '<p><strong>Hello</strong> <em>Bob</em> <code>How</code> are you!</p>' | |
def setClipboardData(html): | |
# print(html) | |
p_hex = subprocess.Popen( | |
('hexdump', '-ve', '1/1 "%.2x"'), | |
stdin=subprocess.PIPE, | |
stdout=subprocess.PIPE | |
) | |
p_hex_output = p_hex.communicate(html.encode('utf-8'))[0] | |
# backward compatiability, use subprocess.run and fallback to | |
# subprocess.call | |
p_osascript = getattr(subprocess, 'run', getattr(subprocess, 'call'))( | |
('osascript', | |
'-e', | |
'set the clipboard to «data HTML{0}»'.format( | |
p_hex_output.decode('ascii') | |
) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment