Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for shareImage #457

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
56 changes: 56 additions & 0 deletions drawBot/drawBotDrawingTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
Binary file added tests/data/example_shareImage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions tests/testExamples.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ def mockVariable(definitions, namespace):
def mockPrintImage(pdf=None):
pass

def mockShareImage(path, **kwargs):
pass

def mockInstallFont(path):
return "Helvetica"

Expand Down Expand Up @@ -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
Expand Down