The following python script successfully copies a given string into the clipboard when run from Ubuntu shell:

import os
text = 'The text to be copied to the clipboard.'
os.system("echo -n {s} | xsel -b".format(s=text))

However, when I execute the script from Emacs as follows, it does not work:

(call-process-shell-command "clipboard.py &" nil 0)

What is special about the script run with call-process-shell-command and how do I work around the problem so my Python script would be able to use the clipboard?

EDIT: In addition, (shell-command-to-string "python clipboard.py") freezes.

  • Why not execute the source buffer? Works for me. – Andreas Röhler May 14 '18 at 17:10
  • @AndreasRöhler Do you mean like this: stackoverflow.com/a/25687205/2725810? First, I did not know about this option, so thank you! Second, the script is a server used by the commands that I implemented in ELisp. This server is supposed to run in the background without distracting the user. Can I achieve this with the approach that you are suggesting? Lastly, even if this works, I am curious what causes the problem described in the original question of mine. – AlwaysLearning May 14 '18 at 20:40
  • Maybe try (call-process-shell-command "python clipboard.py &" nil 0) – Andreas Röhler May 15 '18 at 6:47
  • @AndreasRöhler This does not make the clipboard work either. Note that my python script runs just fine. The only thing that's not working is clipboard. – AlwaysLearning May 16 '18 at 5:11

Using the -i option of xsel, which tells xsel to read standard input, fixes it. I do not know why it worked without this option outside of Emacs.

  • Can you please elaborate? Say what that option does? – Drew May 16 '18 at 14:20
  • @Drew Done as per the request. – AlwaysLearning May 16 '18 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.