|
1 |
| -"""macOS Pasteboard/Clipboard access using native APIs""" |
| 1 | +"""macOS Pasteboard/Clipboard access using native APIs |
| 2 | +
|
| 3 | +Author: Rhet Turnbull <[email protected]> |
| 4 | +
|
| 5 | +License: MIT License, copyright 2022 Rhet Turnbull |
| 6 | +
|
| 7 | +Original Source: https://github.com/RhetTbull/textinator |
| 8 | +
|
| 9 | +Version: 1.1.0, 2022-10-26 |
| 10 | +""" |
2 | 11 |
|
3 | 12 | import os
|
4 | 13 | import typing as t
|
@@ -191,6 +200,35 @@ def set_image_data(self, image_data: NSData, format: str):
|
191 | 200 | self.pasteboard.setData_forType_(image_data, format_type)
|
192 | 201 | self._change_count = self.pasteboard.changeCount()
|
193 | 202 |
|
| 203 | + def set_text_and_image( |
| 204 | + self, text: str, filename: t.Union[str, os.PathLike], format: str |
| 205 | + ): |
| 206 | + """Set both text from str and image from file in either PNG or TIFF format |
| 207 | +
|
| 208 | + Args: |
| 209 | + text (str): Text to set on clipboard |
| 210 | + filename (os.PathLike): Filename of image to set on clipboard |
| 211 | + format (str): Format of image to set, "PNG" or "TIFF" |
| 212 | + """ |
| 213 | + if not isinstance(filename, str): |
| 214 | + filename = str(filename) |
| 215 | + data = NSData.dataWithContentsOfFile_(filename) |
| 216 | + self.set_text_and_image_data(text, data, format) |
| 217 | + |
| 218 | + def set_text_and_image_data(self, text: str, image_data: NSData, format: str): |
| 219 | + """Set both text and image data on clipboard from NSData in a supported image format |
| 220 | +
|
| 221 | + Args: |
| 222 | + text (str): Text to set on clipboard |
| 223 | + image_data (NSData): Image data to set on clipboard |
| 224 | + format (str): Format of image to set, "PNG" or "TIFF" |
| 225 | +
|
| 226 | + Raises: PasteboardTypeError if format is not "PNG" or "TIFF" |
| 227 | + """ |
| 228 | + self.set_image_data(image_data, format) |
| 229 | + self.pasteboard.setString_forType_(text, NSPasteboardTypeString) |
| 230 | + self._change_count = self.pasteboard.changeCount() |
| 231 | + |
194 | 232 | def has_changed(self) -> bool:
|
195 | 233 | """Return True if clipboard has been changed by another process since last check
|
196 | 234 |
|
|
0 commit comments