Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions drawBot/drawBotDrawingTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import math
import os
import tempfile
import random
from collections import namedtuple

Expand Down Expand Up @@ -462,6 +463,39 @@ 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")
"""
path = tempfile.mkstemp(suffix=f".{format}")[1]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like I commented earlier, can we do this without a temp file? It's not being cleaned up, and I don't see how it could be. It's also not necessary as far as I can see: the sharing API works with all kinds of object, such as NSImage. Perhaps in-memory PDF data can be shared, too.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A pdfDocument has no paste board support which is a requirement to send objects over.

The NSSharingService class is used to provide a consistent user experience when sharing items—NSURL objects, NSString objects, NSImage objects, video (through file URLs), or any object that implements the NSPasteboardWriting protocol—in macOS.

--> https://developer.apple.com/documentation/appkit/nssharingservice?language=objc

gif or mp4 has also no paste board support

its indeed not cleaned up, as it needs to be available during the sharing process

I could add a NSSharingServiceDelegate where a callback is received when the sharing is done

https://developer.apple.com/documentation/appkit/nssharingservicedelegate?language=objc

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then it indeed needs to be cleaned up with a delegate.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is silly: the delegate removes the assets to fast and fe mail can not find it anymore...

see https://gist.github.com/typemytype/ea02152e754a5b0225df2df23976afd5


context = getContextForFileExt("pdf")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw. why does this go via the pdf context and not via the regular saveImage()? Would this work for anything but pdf?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cause it needs to be format this is wrong of me :)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question still stands: why not plain saveImage()?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

must be self.saveImage(..) indeed

context.saveImage(path, **kwargs)

serviceMap = dict(
airdrop=AppKit.NSSharingServiceNameSendViaAirDrop,
mail=AppKit.NSSharingServiceNameComposeEmail,
message=AppKit.NSSharingServiceNameComposeMessage,
)

sharingService = AppKit.NSSharingService.sharingServiceNamed_(serviceMap[service])
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