Last active
April 26, 2020 12:07
-
-
Save gregoryvit/e6fcb94df1efede9da4aadf5f535e7b0 to your computer and use it in GitHub Desktop.
Simple and quite handy snippets for macOS pasteboard manipulations from python
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 | |
import os | |
def cb_paste(use_bytes=False): | |
""" | |
Returns the data from macOS pastboard | |
""" | |
p = subprocess.Popen(['pbpaste'], | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE) | |
out, err = p.communicate() | |
if err: | |
raise(Exception(err)) | |
if not use_bytes: | |
return out.decode('utf-8') | |
return out | |
def cb_copy(value): | |
""" | |
Writes data to macOS pastboard | |
""" | |
res = os.system(u'printf "%s" | pbcopy' % value.replace('"', '\\"').replace('%', '%%')) | |
if res != 0: | |
raise(Exception("Exit code: %d" % res)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment