Skip to content

Instantly share code, notes, and snippets.

@upsuper
Created December 23, 2011 14:04

Revisions

  1. upsuper revised this gist Jan 17, 2012. 1 changed file with 24 additions and 11 deletions.
    35 changes: 24 additions & 11 deletions sendkeys.py
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,5 @@
    # - * - coding: UTF-8 - * -

    import sys
    import platform
    import subprocess

    def macosx_set_clipboard(text):
    @@ -15,17 +13,32 @@ def macosx_send_keys(text):
    code = code % (text, )
    p.communicate(code)

    def xte_send_keys(text):
    subprocess.call(['xte', 'str ' + text])

    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 = unsupported_platform
    send_keys = unsupported_platform

    import platform
    _system = platform.system()
    if _system == 'Darwin':
    set_clipboard = macosx_set_clipboard
    send_keys = macosx_send_keys
    else:
    set_clipboard = unsupported_platform
    send_keys = unsupported_platform
    # pre send to accelerate sendkeys
    _p = subprocess.Popen('osascript', stdin=subprocess.PIPE)
    _p.stdin.write('tell application "System Events" to keystroke ""')
    _p.stdin.close()
    elif _system == 'Linux':
    def prog_exists(prog):
    import os
    import os.path as path
    for p in os.environ["PATH"].split(os.pathsep):
    exe_file = path.join(p, prog)
    if path.exists(exe_file):
    return True
    return False
    if prog_exists('xte'):
    send_keys = xte_send_keys
  2. upsuper revised this gist Jan 1, 2012. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions sendkeys.py
    Original file line number Diff line number Diff line change
    @@ -19,6 +19,11 @@ 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:
  3. upsuper created this gist Dec 23, 2011.
    26 changes: 26 additions & 0 deletions sendkeys.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    # - * - 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':
    set_clipboard = macosx_set_clipboard
    send_keys = macosx_send_keys
    else:
    set_clipboard = unsupported_platform
    send_keys = unsupported_platform