Created
December 23, 2011 14:04
-
-
Save upsuper/1514297 to your computer and use it in GitHub Desktop.
Send keys to clipboard or input directly (now only for Mac OS X and partly Linux)
This file contains hidden or 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
# - * - coding: UTF-8 - * - | |
import sys | |
import platform | |
import subprocess | |
def macosx_set_clipboard(text): | |
p = subprocess.Popen('pbcopy', stdin=subprocess.PIPE) | |
p.communicate(text) | |
def macosx_send_keys(text): | |
text = text.replace('\\', '\\\\').replace('"', '\\"') | |
code = 'tell application "System Events" to keystroke "%s"' | |
p = subprocess.Popen('osascript', stdin=subprocess.PIPE) | |
code = code % (text, ) | |
p.communicate(code) | |
def unsupported_platform(*args, **kws): | |
raise 'Unsupported platform' | |
if platform.system() == 'Darwin': | |
# pre send to accelerate sendkeys | |
p = subprocess.Popen('osascript', stdin=subprocess.PIPE) | |
p.stdin.write('tell application "System Events" to keystroke ""') | |
p.stdin.close() | |
# set methods | |
set_clipboard = macosx_set_clipboard | |
send_keys = macosx_send_keys | |
else: | |
set_clipboard = unsupported_platform | |
send_keys = unsupported_platform |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment