Skip to content

Commit 55471ab

Browse files
committed
Added test for #16
1 parent 841d398 commit 55471ab

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed

src/pasteboard.py

+39-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
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+
"""
211

312
import os
413
import typing as t
@@ -191,6 +200,35 @@ def set_image_data(self, image_data: NSData, format: str):
191200
self.pasteboard.setData_forType_(image_data, format_type)
192201
self._change_count = self.pasteboard.changeCount()
193202

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+
194232
def has_changed(self) -> bool:
195233
"""Return True if clipboard has been changed by another process since last check
196234

tests/pasteboard.py

+39-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
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+
"""
211

312
import os
413
import typing as t
@@ -191,6 +200,35 @@ def set_image_data(self, image_data: NSData, format: str):
191200
self.pasteboard.setData_forType_(image_data, format_type)
192201
self._change_count = self.pasteboard.changeCount()
193202

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+
194232
def has_changed(self) -> bool:
195233
"""Return True if clipboard has been changed by another process since last check
196234

tests/test_textinator.py

+10
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,16 @@ def test_clipboard_basic(pb):
154154
assert pb.get_text() == "Hello World"
155155

156156

157+
def test_clipboard_text_and_image(pb):
158+
"""Test clipboard detection when clipboard has text and image (#16)"""
159+
pb.clear()
160+
with log_file() as log:
161+
pb.set_text_and_image("Alt Text", TEST_FILE_HELLO_WORLD, "PNG")
162+
sleep(5)
163+
assert "clipboard has text, skipping" in log.read()
164+
assert pb.get_text() == "Alt Text"
165+
166+
157167
def test_clipboard_no_clipboard(pb):
158168
"""Test clipboard detection does not run when "Detect text in images on clipboard" is off"""
159169
assert click_menu_item("Detect text in images on clipboard")

0 commit comments

Comments
 (0)