Stary2001

autopaste.py

Apr 8th, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.67 KB | None | 0 0
  1. #!/usr/bin/python
  2. import subprocess, time
  3. key_press_delay_ms = 12
  4. character_mapping = [
  5.         { "char":'\n', "mapped": 'Return' },
  6.         { "char":'-', "mapped": 'minus'},
  7.         { "char":'[', "mapped": 'bracketleft'},
  8.         { "char":']', "mapped": 'bracketright'},
  9.         { "char":'{', "mapped": 'braceleft'},
  10.         { "char":'}', "mapped": 'braceright'},
  11.         { "char":'\"', "mapped": 'quotedbl'},
  12.         { "char":'&', "mapped": 'ampersand'},
  13.         { "char":'/', "mapped": 'slash'},
  14.         { "char":'\`', "mapped": 'grave'},
  15.         { "char":'$', "mapped": 'dollar'},
  16.         { "char":'\'', "mapped": 'apostrophe'},  
  17. ]
  18. def flushTyping( arguements ):
  19.         #print("Typing:\n" + arguements )
  20.         time.sleep( key_press_delay_ms * 1.0 / 1000 )
  21.         command = ["xdotool", "type", "--clearmodifiers", "--delay", str( key_press_delay_ms ), str( arguements )]
  22.         subprocess.call(command)
  23. def flushKeypress( arguements ):
  24.         #print("Keypress:\n" + arguements)
  25.         time.sleep( key_press_delay_ms * 1.0 / 1000 )
  26.         arguements = arguements.strip().split(" ")
  27.         #print ["xdotool", "type", "--clearmodifiers", "--delay", key_press_delay_ms ] + arguements
  28.         command = ["xdotool", "key", "--clearmodifiers", "--delay", str( key_press_delay_ms )] + arguements
  29.         subprocess.call( command )
  30. if __name__ == "__main__":
  31.         import pyperclip
  32.         contents = pyperclip.getcb()
  33.         keypress_arguements = ""
  34.         typing_arguements = ""
  35.         last_addition = "typing"
  36.         character_checklist = []
  37.         for item in character_mapping:
  38.                 character_checklist.append( item["char"] )
  39.         for character in contents:
  40.                 if character in character_checklist:
  41.                         for item in character_mapping:
  42.                                 if item["char"] == character:
  43.                                         keypress_arguements += " " + item["mapped"]
  44.                                         break
  45.                         if last_addition == 'typing':
  46.                                 flushTyping( typing_arguements )
  47.                                 typing_arguements = ""
  48.                         last_addition = 'keypress'
  49.                 else:
  50.                         typing_arguements += character
  51.                         if last_addition == 'keypress':
  52.                                 flushKeypress( keypress_arguements )
  53.                                 keypress_arguements = ""
  54.                         last_addition = 'typing'
  55.         if last_addition == "typing":
  56.                 flushTyping( typing_arguements )
  57.         else:
  58.                 flushKeypress( keypress_arguements )
Advertisement
Add Comment
Please, Sign In to add comment