Skip to content

Instantly share code, notes, and snippets.

@luqmaan
Last active April 5, 2023 16:35
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save luqmaan/d8bc61e746207bb12f11 to your computer and use it in GitHub Desktop.
Save luqmaan/d8bc61e746207bb12f11 to your computer and use it in GitHub Desktop.
Write a python string to the clipboard via pbcopy (OS X)
def write_to_clipboard(output):
import subprocess
process = subprocess.Popen('pbcopy', env={'LANG': 'en_US.UTF-8'}, stdin=subprocess.PIPE)
process.communicate(output.encode())
@macdet
Copy link

macdet commented Dec 14, 2018

thx. good job

@adi928
Copy link

adi928 commented Oct 21, 2020

For dictionary objects,
test_dict = {"a": 1, "b":2}; write_to_clipboard(json.dumps(test_dict).encode('utf-8')

@john012343210
Copy link

For dictionary objects,
test_dict = {"a": 1, "b":2}; write_to_clipboard(json.dumps(test_dict).encode('utf-8')

'bytes' object has no attribute 'encode'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment