Skip to content

Commit

Permalink
Fix bugs and improve logics in DarwinClipboard
Browse files Browse the repository at this point in the history
- Add missing execute function call and correctly capturing `stdout`.
- Pass a report when an image is written with a warning (catch by
  checking the saved file instead of `stderr`). 
- Use `pasteboard` to check for filepaths.
- Target TIFF when checking clipboard contents via `pasteboard`.
  • Loading branch information
williamchange authored and thanhph111 committed Jul 21, 2021
1 parent 95b2f38 commit d39cbe0
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions imagepaste/clipboard/darwin/darwin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,21 @@ def push(cls, save_directory: str) -> DarwinClipboard:
holding the images information.
"""
from os.path import join
from .pasteboard._native import Pasteboard
from os.path import isfile
from .pasteboard import _native as pasteboard

pb = pasteboard.Pasteboard()

# Use Pasteboard to get file URLs from the clipboard
pasteboard = Pasteboard()
urls = pasteboard.get_file_urls()
urls = pb.get_file_urls()
if urls is not None:
filepaths = list(urls)
images = [Image(filepath) for filepath in filepaths]
return cls(Report(6, f"Pasted {len(images)} image files: {images}"), images)

# If no images are found, return a report with no images
contents = pasteboard.get_contents()
if contents == "":
return cls(Report(2))

# Check if clipboard doesn't contain any filepaths
# (e.g. if the clipboard contains just a single image)
commands = [
'((clipboard info) as string does not contain "«class furl»") as string'
]
process = Process(cls.get_osascript_args(commands))
if process.stdout == "true":
# Save an image if it is in the clipboard
contents = pb.get_contents(type=pasteboard.TIFF)
if contents is not None:
filename = cls.get_timestamp_filename()
filepath = join(save_directory, filename)
image = Image(filepath, filename)
Expand All @@ -65,10 +58,13 @@ def push(cls, save_directory: str) -> DarwinClipboard:
"close access pastedImage",
]
process = Process.execute(cls.get_osascript_args(commands))
if process.stderr:
if not isfile(filepath):
return cls(Report(3, f"Cannot save image: {image} ({process.stderr})"))
if process.stderr:
report = Report(6, f"Saved 1 image: {image} (WARN: {process.stderr})")
return cls(report, [image])
return cls(Report(6, f"Saved and pasted 1 image: {image}"), [image])
return cls(Report(3))
return cls(Report(2))

@classmethod
def pull(cls, image_path: str) -> DarwinClipboard:
Expand All @@ -86,7 +82,7 @@ def pull(cls, image_path: str) -> DarwinClipboard:
"set the clipboard to "
f'(read file POSIX file "{image_path}" as «class PNGf»)'
]
process = Process(cls.get_osascript_args(commands))
process = Process.execute(cls.get_osascript_args(commands))
if process.stderr:
return cls(Report(4, f"Process failed ({process.stderr})"))
image = Image(image_path)
Expand Down

0 comments on commit d39cbe0

Please sign in to comment.