diff --git a/.gitignore b/.gitignore index 07c73cb..9d660cc 100644 --- a/.gitignore +++ b/.gitignore @@ -51,6 +51,10 @@ __pycache__/ # C extensions *.so +# Darwin pasteboard modules +!_native.cpython-37m-darwin.so +!_native.cpython-39-darwin.so + # Distribution / packaging .Python build/ diff --git a/__init__.py b/__init__.py index a0386cf..8fe10c8 100644 --- a/__init__.py +++ b/__init__.py @@ -30,6 +30,8 @@ from .windows.windows import GrabImage, CopyImage elif platform.system() == "Linux": from .linux.linux import GrabImage, CopyImage +elif platform.system() == "Darwin": + from .darwin.darwin import GrabImage, CopyImage else: raise Exception("Unsupported current platform") diff --git a/darwin/darwin.py b/darwin/darwin.py new file mode 100644 index 0000000..ca59474 --- /dev/null +++ b/darwin/darwin.py @@ -0,0 +1,84 @@ +import bpy +import time +import os +import subprocess + +try: + from . import pasteboard_py37 as pasteboard +except ImportError: + from . import pasteboard_py39 as pasteboard + + +# Check if clipboard doesn't contain any file paths +def no_furls(): + script = [ + "osascript", + "-e", + '((clipboard info) as string does not contain "«class furl»") as string', + ] + popen = subprocess.Popen(script, stdout=subprocess.PIPE) + return popen.communicate()[0].decode("utf-8").strip() == "true" + + +# Save image data directly from clipboard +def save_clipboard(fullpath): + commands = [ + f'set pastedImage to (open for access POSIX file "{fullpath}" with write permission)', + "try", + " write (the clipboard as «class PNGf») to pastedImage", + "end try", + "close access pastedImage", + ] + script = ["osascript"] + for command in commands: + script += ["-e", command] + subprocess.Popen(script).wait() + + +def GrabImage(): + + timestamp = time.strftime("%y%m%d-%H%M%S") + img_name = f"PastedImage{timestamp}.png" + + bpy_addon_prefs = bpy.context.preferences.addons[ + __package__.split(".")[0] + ].preferences + + if bpy.data.filepath and bpy_addon_prefs.force_default_dir is False: + Directory = os.path.join(os.path.split(bpy.data.filepath)[0], "ImagePaste") + + if os.path.isdir(Directory) is False: + os.mkdir(Directory) + + else: + Directory = bpy_addon_prefs.default_img_dir + + img_dir = os.path.join(Directory, img_name) + + pb = pasteboard.Pasteboard() + urls = pb.get_file_urls() + contents = pb.get_contents() + + if urls is not None: + urls = list(urls) + img_dir = urls + img_name = [os.path.basename(current) for current in img_dir] + return img_dir, img_name + elif contents == "": + return 0 + else: + if no_furls(): + save_clipboard(img_dir) + return [img_dir], [img_name] + + return 1 + + +# Function to copy image from given path to clipboard +def CopyImage(img_path): + script = [ + "osascript", + "-e", + f'set the clipboard to (read file POSIX file "{img_path}" as «class PNGf»)', + ] + subprocess.Popen(script).wait() diff --git a/darwin/pasteboard_py37/__init__.py b/darwin/pasteboard_py37/__init__.py new file mode 100644 index 0000000..8466ffb --- /dev/null +++ b/darwin/pasteboard_py37/__init__.py @@ -0,0 +1,16 @@ +# Pasteboard - Python interface for reading from NSPasteboard (macOS clipboard) +# Copyright (C) 2017-2021 Toby Fleming +# This Source Code Form is subject to the terms of the Mozilla Public License, +# v. 2.0. If a copy of the MPL was not distributed with this file, You can +# obtain one at https://mozilla.org/MPL/2.0/. +import sys as _sys + +assert _sys.platform == "darwin", "pasteboard only works on macOS" + +from ._native import * + + +class PasteboardType: + """Make type hints not fail on import - don't use this class""" + + pass diff --git a/darwin/pasteboard_py37/_native.cpython-37m-darwin.so b/darwin/pasteboard_py37/_native.cpython-37m-darwin.so new file mode 100755 index 0000000..fb8384e Binary files /dev/null and b/darwin/pasteboard_py37/_native.cpython-37m-darwin.so differ diff --git a/darwin/pasteboard_py39/__init__.py b/darwin/pasteboard_py39/__init__.py new file mode 100644 index 0000000..8466ffb --- /dev/null +++ b/darwin/pasteboard_py39/__init__.py @@ -0,0 +1,16 @@ +# Pasteboard - Python interface for reading from NSPasteboard (macOS clipboard) +# Copyright (C) 2017-2021 Toby Fleming +# This Source Code Form is subject to the terms of the Mozilla Public License, +# v. 2.0. If a copy of the MPL was not distributed with this file, You can +# obtain one at https://mozilla.org/MPL/2.0/. +import sys as _sys + +assert _sys.platform == "darwin", "pasteboard only works on macOS" + +from ._native import * + + +class PasteboardType: + """Make type hints not fail on import - don't use this class""" + + pass diff --git a/darwin/pasteboard_py39/_native.cpython-39-darwin.so b/darwin/pasteboard_py39/_native.cpython-39-darwin.so new file mode 100755 index 0000000..22e33c3 Binary files /dev/null and b/darwin/pasteboard_py39/_native.cpython-39-darwin.so differ