2022-06-04 12:32:02 +02:00
|
|
|
from typing import Optional
|
2018-07-24 00:35:28 +02:00
|
|
|
|
2022-06-04 12:32:02 +02:00
|
|
|
from platypush.context import get_bus
|
|
|
|
from platypush.message.event.clipboard import ClipboardEvent
|
|
|
|
from platypush.plugins import RunnablePlugin, action
|
2018-07-24 00:35:28 +02:00
|
|
|
|
2022-06-04 12:32:02 +02:00
|
|
|
|
|
|
|
class ClipboardPlugin(RunnablePlugin):
|
2018-07-24 00:35:28 +02:00
|
|
|
"""
|
2022-06-04 12:32:02 +02:00
|
|
|
Plugin to programmatically copy strings to your system clipboard,
|
|
|
|
monitor and get the current clipboard content.
|
2018-07-24 00:35:28 +02:00
|
|
|
|
|
|
|
Requires:
|
2022-06-04 12:32:02 +02:00
|
|
|
- **pyperclip** (``pip install pyperclip``)
|
|
|
|
|
|
|
|
Triggers:
|
|
|
|
|
|
|
|
- :class:`platypush.message.event.clipboard.ClipboardEvent` on clipboard update.
|
|
|
|
|
2018-07-24 00:35:28 +02:00
|
|
|
"""
|
|
|
|
|
2022-06-04 12:32:02 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self._last_text: Optional[str] = None
|
|
|
|
|
2018-07-24 00:35:28 +02:00
|
|
|
@action
|
|
|
|
def copy(self, text):
|
|
|
|
"""
|
|
|
|
Copies a text to the OS clipboard
|
|
|
|
|
|
|
|
:param text: Text to copy
|
|
|
|
:type text: str
|
|
|
|
"""
|
2019-12-01 23:35:05 +01:00
|
|
|
import pyperclip
|
2018-07-24 00:35:28 +02:00
|
|
|
|
2022-06-04 12:32:02 +02:00
|
|
|
pyperclip.copy(text)
|
2018-07-24 00:35:28 +02:00
|
|
|
|
|
|
|
@action
|
|
|
|
def paste(self):
|
|
|
|
"""
|
|
|
|
Get the current content of the clipboard
|
|
|
|
"""
|
2019-12-01 23:35:05 +01:00
|
|
|
import pyperclip
|
2022-06-04 12:32:02 +02:00
|
|
|
|
2018-07-24 00:35:28 +02:00
|
|
|
return pyperclip.paste()
|
|
|
|
|
2022-06-04 12:32:02 +02:00
|
|
|
def main(self):
|
|
|
|
import pyperclip
|
|
|
|
|
|
|
|
while not self.should_stop():
|
|
|
|
text = pyperclip.paste()
|
|
|
|
if text and text != self._last_text:
|
|
|
|
get_bus().post(ClipboardEvent(text=text))
|
|
|
|
self._last_text = text
|
2018-07-24 00:35:28 +02:00
|
|
|
|
2022-06-04 12:32:02 +02:00
|
|
|
self._should_stop.wait(0.1)
|
2018-07-24 00:35:28 +02:00
|
|
|
|
2022-06-04 12:32:02 +02:00
|
|
|
self.logger.info('Stopped clipboard monitor backend')
|
|
|
|
|
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|