Skip to content

Commit

Permalink
Fully support macOS
Browse files Browse the repository at this point in the history
Read more at #6.
  • Loading branch information
williamchange authored Jun 18, 2021
1 parent ebeb16a commit 4d1646e
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
2 changes: 2 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
84 changes: 84 additions & 0 deletions darwin/darwin.py
Original file line number Diff line number Diff line change
@@ -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()
16 changes: 16 additions & 0 deletions darwin/pasteboard_py37/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Binary file not shown.
16 changes: 16 additions & 0 deletions darwin/pasteboard_py39/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Binary file not shown.

0 comments on commit 4d1646e

Please sign in to comment.