Created
January 28, 2017 04:20
-
-
Save XuankangLin/7ec82f80a0044a52330720244de2d15a to your computer and use it in GitHub Desktop.
Read from and Write to clipboards in python scripts, works on Mac at least.
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 | |
def getClipboardData(): | |
p = subprocess.Popen(['pbpaste'], stdout=subprocess.PIPE) | |
retcode = p.wait() | |
data = p.stdout.read() | |
return data | |
def setClipboardData(data): | |
p = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE) | |
p.stdin.write(data) | |
p.stdin.close() | |
retcode = p.wait() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A prefix like b"text" for a string or proper encoded bytes conversion is required before entering data to setClipboardData(data) as argument to avoid:
TypeError: a bytes-like object is required, not 'str'
To transform a unicode string to a byte string in Python do this: