Skip to content

Instantly share code, notes, and snippets.

@khelwood
Created October 26, 2025 22:35
Show Gist options
  • Select an option

  • Save khelwood/26ee0d38353788b59d7a79244af51ba6 to your computer and use it in GitHub Desktop.

Select an option

Save khelwood/26ee0d38353788b59d7a79244af51ba6 to your computer and use it in GitHub Desktop.
This is the clipboard lib that I use on macs
import subprocess
if str is bytes:
def copy(text):
p = subprocess.Popen(['pbcopy', 'w'],
stdin=subprocess.PIPE, close_fds=True)
p.communicate(input=str(text))
def paste():
p = subprocess.Popen(['pbpaste', 'r'],
stdout=subprocess.PIPE, close_fds=True)
stdout, stderr = p.communicate()
return stdout
else:
def copy(text):
p = subprocess.Popen(['pbcopy', 'w'],
stdin=subprocess.PIPE, close_fds=True)
p.communicate(input=text.encode('utf-8'))
def paste():
p = subprocess.Popen(['pbpaste', 'r'],
stdout=subprocess.PIPE, close_fds=True)
stdout, stderr = p.communicate()
return stdout.decode('utf-8')
def copylines(lines):
copy('\n'.join(map(str, lines)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment