diff --git a/drawBot/drawBotDrawingTools.py b/drawBot/drawBotDrawingTools.py index 810e6bc3..f50eca3e 100644 --- a/drawBot/drawBotDrawingTools.py +++ b/drawBot/drawBotDrawingTools.py @@ -4,7 +4,9 @@ import math import os +import tempfile import random +import time from collections import namedtuple from .context import getContextForFileExt, getContextOptions, getFileExtensions, getContextOptionsDocs @@ -462,6 +464,60 @@ def pdfImage(self): self._drawInContext(context) return context.getNSPDFDocument() + def shareImage(self, format="pdf", service="airdrop", **kwargs): + """ + Share the canvas to a service with a specified format. + + As default the `format` is `pdf`, any suffix drawBot supports is possible. + + `service` options are `airdrop`, `mail` or `message`. + + .. downloadcode:: shareImage.py + + # set A4 page size + newPage(200, 200) + # draw something + text("Foo, bar", (10, 10)) + # share it over airdrop + shareImage('pdf', service="airdrop") + """ + class SharingServiceDelegate: + + def __init__(self, path): + self.path = path + + def _removePath(self): + if os.path.exists(self.path): + os.remove(self.path) + + def sharingService_didShareItems_(self, sharingService, items): + # wait a sec to be sure the serice (like mail) has started up + # and collected all the assets from disk + time.sleep(1) + self._removePath() + + def sharingService_didFailToShareItems_error_(self, sharingService, items, error): + self._removePath() + + serviceMap = dict( + airdrop=AppKit.NSSharingServiceNameSendViaAirDrop, + mail=AppKit.NSSharingServiceNameComposeEmail, + message=AppKit.NSSharingServiceNameComposeMessage, + ) + if service not in serviceMap: + raise DrawBotError(f"service must be {', '.join(serviceMap.keys())}") + + path = tempfile.mkstemp(suffix=f".{format}")[1] + self.saveImage(path, **kwargs) + + if os.path.exists(path): + # only pop up the sharing service when saveImage is succes full + sharingService = AppKit.NSSharingService.sharingServiceNamed_(serviceMap[service]) + sharingService.setDelegate_(SharingServiceDelegate(path)) + sharingService.performWithItems_([ + AppKit.NSURL.fileURLWithPath_(path) + ]) + # graphics state def save(self): diff --git a/tests/data/example_shareImage.png b/tests/data/example_shareImage.png new file mode 100755 index 00000000..38fa4973 Binary files /dev/null and b/tests/data/example_shareImage.png differ diff --git a/tests/testExamples.py b/tests/testExamples.py index 965016e1..578af4aa 100644 --- a/tests/testExamples.py +++ b/tests/testExamples.py @@ -106,6 +106,9 @@ def mockVariable(definitions, namespace): def mockPrintImage(pdf=None): pass +def mockShareImage(path, **kwargs): + pass + def mockInstallFont(path): return "Helvetica" @@ -139,6 +142,7 @@ def mockSaveImage(path, **options): namespace["imagePixelColor"] = mockImagePixelColor namespace["Variable"] = mockVariable namespace["printImage"] = mockPrintImage + namespace["shareImage"] = mockShareImage namespace["installFont"] = mockInstallFont namespace["uninstallFont"] = mockUninstallFont namespace["randint"] = mockRandInt