Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fengyie007/6fefde6cbee6537d107dff9979b3ea74 to your computer and use it in GitHub Desktop.
Save fengyie007/6fefde6cbee6537d107dff9979b3ea74 to your computer and use it in GitHub Desktop.
Python3 - Set Mac clipboard with HTML content
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