Skip to content

Instantly share code, notes, and snippets.

@gregoryvit
Last active April 26, 2020 12:07
Show Gist options
  • Save gregoryvit/e6fcb94df1efede9da4aadf5f535e7b0 to your computer and use it in GitHub Desktop.
Save gregoryvit/e6fcb94df1efede9da4aadf5f535e7b0 to your computer and use it in GitHub Desktop.
Simple and quite handy snippets for macOS pasteboard manipulations from python
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