1
#The following is a python module made by Albert Sweigart.
6
ctypes.windll.user32.OpenClipboard(0)
7
pcontents = ctypes.windll.user32.GetClipboardData(1) # 1 is CF_TEXT
8
data = ctypes.c_char_p(pcontents).value
9
#ctypes.windll.kernel32.GlobalUnlock(pcontents)
10
ctypes.windll.user32.CloseClipboard()
13
def winSetClipboard(text):
14
GMEM_DDESHARE = 0x2000
15
ctypes.windll.user32.OpenClipboard(0)
16
ctypes.windll.user32.EmptyClipboard()
18
# works on Python 2 (bytes() only takes one argument)
19
hCd = ctypes.windll.kernel32.GlobalAlloc(GMEM_DDESHARE, len(bytes(text))+1)
21
# works on Python 3 (bytes() requires an encoding)
22
hCd = ctypes.windll.kernel32.GlobalAlloc(GMEM_DDESHARE, len(bytes(text, 'ascii'))+1)
23
pchData = ctypes.windll.kernel32.GlobalLock(hCd)
25
# works on Python 2 (bytes() only takes one argument)
26
ctypes.cdll.msvcrt.strcpy(ctypes.c_char_p(pchData), bytes(text))
28
# works on Python 3 (bytes() requires an encoding)
29
ctypes.cdll.msvcrt.strcpy(ctypes.c_char_p(pchData), bytes(text, 'ascii'))
30
ctypes.windll.kernel32.GlobalUnlock(hCd)
31
ctypes.windll.user32.SetClipboardData(1,hCd)
32
ctypes.windll.user32.CloseClipboard()
34
def macSetClipboard(text):
35
outf = os.popen('pbcopy', 'w')
39
def macGetClipboard():
40
outf = os.popen('pbpaste', 'r')
45
def gtkGetClipboard():
46
return gtk.Clipboard().wait_for_text()
48
def gtkSetClipboard(text):
56
def qtSetClipboard(text):
59
def xclipSetClipboard(text):
60
outf = os.popen('xclip -selection c', 'w')
64
def xclipGetClipboard():
65
outf = os.popen('xclip -selection c -o', 'r')
70
def xselSetClipboard(text):
71
outf = os.popen('xsel -i', 'w')
75
def xselGetClipboard():
76
outf = os.popen('xsel -o', 'r')
82
if os.name == 'nt' or platform.system() == 'Windows':
84
getcb = winGetClipboard
85
setcb = winSetClipboard
86
elif os.name == 'mac' or platform.system() == 'Darwin':
87
getcb = macGetClipboard
88
setcb = macSetClipboard
89
elif os.name == 'posix' or platform.system() == 'Linux':
90
xclipExists = os.system('which xclip') == 0
92
getcb = xclipGetClipboard
93
setcb = xclipSetClipboard
95
xselExists = os.system('which xsel') == 0
97
getcb = xselGetClipboard
98
setcb = xselSetClipboard
101
getcb = gtkGetClipboard
102
setcb = gtkSetClipboard
107
app = QApplication([])
108
cb = PyQt4.QtGui.QApplication.clipboard()
109
getcb = qtGetClipboard
110
setcb = qtSetClipboard
112
raise Exception('Pyperclip requires the gtk or PyQt4 module installed, or the xclip command.')
116
#End of Module. The following is code that was personally written by me.