def copyToClip(text: str, notify: Optional[bool] = False) -> bool: """Copies the given text to the windows clipboard. @returns: True if it succeeds, False otherwise. @param text: the text which will be copied to the clipboard @param notify: whether to emit a confirmation message """ if not isinstance(text, str) or len(text) == 0: return False import gui try: with winUser.openClipboard(gui.mainFrame.Handle): winUser.emptyClipboard() winUser.setClipboardData(winUser.CF_UNICODETEXT, text) got = getClipData() except OSError: if notify: ui.reportTextCopiedToClipboard() # No argument reports a failure. return False if got == text: if notify: ui.reportTextCopiedToClipboard(text) return True if notify: ui.reportTextCopiedToClipboard() # No argument reports a failure. return False
def getClipData(): """Receives text from the windows clipboard. @returns: Clipboard text @rtype: string """ import gui with winUser.openClipboard(gui.mainFrame.Handle): return winUser.getClipboardData(winUser.CF_UNICODETEXT) or u""
def copyToClip(text): if not isinstance(text, str): raise TypeError("str required") import gui with openClipboard(gui.mainFrame.Handle): emptyClipboard() if text: setClipboardData(CF_UNICODETEXT, text)
def enumerateClipboardFormat(): formats = [] fmt = 0 with winUser.openClipboard(gui.mainFrame.Handle): while True: fmt = windll.user32.EnumClipboardFormats(fmt) if fmt == 0: break formats.append(fmt) return formats
def clearClipboard(self): try: with winUser.openClipboard(gui.mainFrame.Handle): winUser.emptyClipboard() # Translators: message presented when the clipboard content has been deleted. ui.message(_("Clipboard cleared")) except Exception as e: # Translators: message presented when the clipboard content cannot be deleted. ui.message(_("Clipboard not cleared")) log.debug("Cannot clear clipboard: %s" % e)
def copyToClip(text): """Copies the given text to the windows clipboard. @returns: True if it succeeds, False otherwise. @rtype: boolean @param text: the text which will be copied to the clipboard @type text: string """ if not isinstance(text, str) or len(text) == 0: return False import gui with winUser.openClipboard(gui.mainFrame.Handle): winUser.emptyClipboard() winUser.setClipboardData(winUser.CF_UNICODETEXT, text) got = getClipData() return got == text
def clipboardHasContent(): with winUser.openClipboard(gui.mainFrame.Handle): clipFormat = winUser.windll.user32.EnumClipboardFormats(0) if clipFormat: return True return False
def getImageFromClipboard(cls): CF_DIB = 8 CF_HDROP = 15 CF_UNICODETEXT = 13 clipboardImage = None formats = cls.enumerateClipboardFormat() if CF_DIB in formats: clipboardImage = ImageGrab.grabclipboard() elif CF_HDROP in formats: try: filePathList = [] with winUser.openClipboard(gui.mainFrame.Handle): rawData = windll.user32.GetClipboardData(CF_HDROP) if not rawData: ui.message(_("Error occurred while getting pasted file.")) rawData = winKernel.HGLOBAL(rawData, autoFree=False) with rawData.lock() as addr: fileCount = windll.shell32.DragQueryFileW( c_uint32(addr), c_uint32(0xFFFFFFFF), c_uint32(0), c_uint32(0) ) for c in range(fileCount): BUFFER_SIZE = 4096 filePath = create_unicode_buffer(BUFFER_SIZE) windll.shell32.DragQueryFileW( c_uint32(addr), c_uint32(c), c_uint32(filePath), c_uint32(BUFFER_SIZE) ) filePathList.append(wstring_at(filePath, size=BUFFER_SIZE).rstrip('\x00')) log.debug("filePathList\n{0}".format(filePathList)) for fileName in filePathList: # TODO Add a prompt for users to choose from import os if os.path.isfile(fileName): clipboardImage = Image.open(rawData[0]) clipboardImage = clipboardImage.convert("RGB") return clipboardImage except TypeError as e: log.io(e) elif CF_UNICODETEXT in formats: # TODO extract url or file path from text then grab an image from it. try: from api import getClipData import os text = getClipData() if os.path.exists(text): if os.path.isfile(text): clipboardImage = Image.open(text) else: # Translators: Reported when text in clipboard is not a valid path ui.message(_(u"Text in clipboard is the name of a directory.")) else: # Translators: Reported when text in clipboard is not a valid path ui.message(_(u"Text in clipboard is not a valid path.")) except IOError: # Translators: Reported when cannot get content of the path specified errMsg = _("The file specified in clipboard is not an image") ui.message(errMsg) return clipboardImage
def clearClipboard(): with winUser.openClipboard(gui.mainFrame.Handle): winUser.emptyClipboard()