Skip to content

Instantly share code, notes, and snippets.

@mpcabd
Created June 1, 2017 15:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mpcabd/33c3642e34e0cb813bc943e4d843ab3c to your computer and use it in GitHub Desktop.
Save mpcabd/33c3642e34e0cb813bc943e4d843ab3c 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>'
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