-
-
Notifications
You must be signed in to change notification settings - Fork 63
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
25140f1
0dca9f3
fd2b524
d0ebc7a
ef8fc0c
34e29f1
956531b
736b932
a6e452e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
import math | ||
import os | ||
import tempfile | ||
import random | ||
from collections import namedtuple | ||
|
||
|
@@ -462,6 +463,38 @@ 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:: printImage.py | ||
|
||
# set A4 page size | ||
size(200, 200) | ||
# draw something | ||
text("Foo, bar", (10, 10)) | ||
# share it over airdrop | ||
shareImage('pdf', service="airdrop") | ||
""" | ||
path = tempfile.mkstemp(suffix=f".{format}")[1] | ||
|
||
self.saveImage(path, **kwargs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds like a job for Also: shouldn't this be a proper menu item rather than a library function? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the cool thing to have this as library function is that it can be run from code outside the app. |
||
|
||
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): | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
--> 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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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