Created
June 1, 2017 15:40
-
-
Save mpcabd/33c3642e34e0cb813bc943e4d843ab3c to your computer and use it in GitHub Desktop.
Python3 - Set Mac clipboard with HTML content
This file contains 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>' | |
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