From 25140f1c87ba2423aa8d51d1f84ec32bde8dc099 Mon Sep 17 00:00:00 2001 From: Frederik Berlaen Date: Tue, 30 Nov 2021 16:42:58 +0100 Subject: [PATCH 1/8] add shareImage --- drawBot/drawBotDrawingTools.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/drawBot/drawBotDrawingTools.py b/drawBot/drawBotDrawingTools.py index 7187cec1..468bfbeb 100644 --- a/drawBot/drawBotDrawingTools.py +++ b/drawBot/drawBotDrawingTools.py @@ -4,6 +4,7 @@ import math import os +import tempfile import random from collections import namedtuple @@ -472,6 +473,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) + + 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): From fd2b5245d61a13d8d0a1d40465a34c46ed94df5e Mon Sep 17 00:00:00 2001 From: Frederik Berlaen Date: Tue, 7 Dec 2021 09:40:18 +0100 Subject: [PATCH 2/8] use the context to generate the image and not the top level api inside shareImage --- drawBot/drawBotDrawingTools.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drawBot/drawBotDrawingTools.py b/drawBot/drawBotDrawingTools.py index 3715796b..4a867a92 100644 --- a/drawBot/drawBotDrawingTools.py +++ b/drawBot/drawBotDrawingTools.py @@ -474,7 +474,7 @@ def shareImage(self, format="pdf", service="airdrop", **kwargs): .. downloadcode:: printImage.py # set A4 page size - size(200, 200) + newPage(200, 200) # draw something text("Foo, bar", (10, 10)) # share it over airdrop @@ -482,7 +482,8 @@ def shareImage(self, format="pdf", service="airdrop", **kwargs): """ path = tempfile.mkstemp(suffix=f".{format}")[1] - self.saveImage(path, **kwargs) + context = getContextForFileExt("pdf") + context.saveImage(path, **kwargs) serviceMap = dict( airdrop=AppKit.NSSharingServiceNameSendViaAirDrop, From d0ebc7a7e48caf9d79684f217c38eed90620fa58 Mon Sep 17 00:00:00 2001 From: Frederik Berlaen Date: Tue, 7 Dec 2021 09:43:36 +0100 Subject: [PATCH 3/8] mocking shareImage --- tests/testExamples.py | 4 ++++ 1 file changed, 4 insertions(+) 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 From ef8fc0cf09956e4d2849edd2e1cff96547553e7f Mon Sep 17 00:00:00 2001 From: Frederik Berlaen Date: Tue, 7 Dec 2021 20:18:44 +0100 Subject: [PATCH 4/8] silly me, rename the download code file name --- drawBot/drawBotDrawingTools.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drawBot/drawBotDrawingTools.py b/drawBot/drawBotDrawingTools.py index 4a867a92..9c5b1c5f 100644 --- a/drawBot/drawBotDrawingTools.py +++ b/drawBot/drawBotDrawingTools.py @@ -471,7 +471,7 @@ def shareImage(self, format="pdf", service="airdrop", **kwargs): `service` options are `airdrop`, `mail` or `message`. - .. downloadcode:: printImage.py + .. downloadcode:: shareImage.py # set A4 page size newPage(200, 200) From 34e29f16ecacfee0e56ce045cf30da52fac7671e Mon Sep 17 00:00:00 2001 From: Frederik Berlaen Date: Tue, 7 Dec 2021 20:34:12 +0100 Subject: [PATCH 5/8] adding test image --- tests/data/example_shareImage.png | Bin 0 -> 2472 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100755 tests/data/example_shareImage.png diff --git a/tests/data/example_shareImage.png b/tests/data/example_shareImage.png new file mode 100755 index 0000000000000000000000000000000000000000..38fa497307d154eab6e1e88a497f63a9e0c65b89 GIT binary patch literal 2472 zcmeH{YdjMQAIG=39+xCcoEDKwxn)8{grT|Qy15%6xeQs6%q=uJE-}q5HYbUV9Bi%; zp{z3JlqC0C&3!Rv#fGCd&-3ni`Mf$We*gdX^ZUR1e}11|rsEZBaS?eD001BkzYKFe z*aJT$EO;>M+@F;L0Q_+hYYUol zA2I|GE}i#i;BH4$okQcxFezo-0B&i{a<-bQn_gqhBqkEqY6%$R; zSpMEz4Sf~ntW%#YIiG076P^o0Co~_M87GwHNty~in3yW5uaacJylcmW$X&$BI@tofK!%KCJ1t=;V42L5fpWw!@fK+w#h`Q$Ir%OT?xdP*Re* z#G8H*P%k=z>lCqf=aURnza%&f9=%?ZdPoo|T_-JfA60Qp_E%=N@#Y0;@nBEa%Ywau z0aWUCD}#DE5NC21+$hh>sRe%s+MuL+2S}MFY8rw}Rpk_qd`=zmg~B;`H=sbJxckQT z9Q?6HsX*4ws7<(~ud(6C?(-}?=c-c6$@A0>lHb1GX*+M1Pqmtdn(ryUJ;fF9>Eyja zHFz&+8ag%4$(6OcF}6|LD?BJigBMykleFNr(GvaI^Ib=yUGHkRJ0=z(j~1K~JYjZ+5T;@19`lZ3FlRtD* zO-wkz+-Wq0P`APb6LrcjLJkEee0|FG?$SmAGr0|*E&DwL!gVRC;1mQIX@9QEOe*W7 zMPOEef$R5HH+F^6Lx(TUA%|UzT%>9mhRq~!O;s@@G>FTCe5{FRbY6ZRFqHuE_;4X& zMSM^u*7iOAs(ih{X+p{JVn+4Rdjobk*IjU9XwP2-SW{cMTcywr2`Mja>qlI%?k>}f zRj&424i!K+mYnihIpXNh8=)zIbL_OS#_=9V-es)l%<%VB&Wj!B?Zn;TrHMXFEmJF* zW@uFQVw!bx#N^UG1rVH_1SAo$2tIF7)MB(o$GG9HS$s)xOm_ByB6Au*5< zk6;gEld;0{ct+>T*z{Km*4ejp~`?Wm@(;E2L?_=G1*{?)k zM{NU(!wfa$a19o2E7tsSGwB^oW$6?7n{71a-FC#JeVaS}oA>*~TEtSM-SU1NJ2$^R zUxx13s9wXq-(4WG)p(amyd84kVftWZQ%J7rER3*$I<@V>UsRO9pG==jQJ6atqo+S{2&-%R*` zDsD!mMo4Yqy!SihP=k^5F5TCELl*s_Dd*khBh{Kp1H@BK$yPh;`cjY?957|-2hH3# z;`oB^nCE#iK`j;+UxzHKNB6Ii<^uS2i5`fUHG#=`W&>3b_|>{Y?}VrjBv{ Date: Fri, 24 Dec 2021 11:28:23 +0100 Subject: [PATCH 6/8] adding a delegate this does not work when sharing over mail. The file is already removed before mail popup up --- drawBot/drawBotDrawingTools.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/drawBot/drawBotDrawingTools.py b/drawBot/drawBotDrawingTools.py index 9c5b1c5f..be22abae 100644 --- a/drawBot/drawBotDrawingTools.py +++ b/drawBot/drawBotDrawingTools.py @@ -480,10 +480,23 @@ def shareImage(self, format="pdf", service="airdrop", **kwargs): # share it over airdrop shareImage('pdf', service="airdrop") """ - path = tempfile.mkstemp(suffix=f".{format}")[1] + class SharingServiceDelegate: - context = getContextForFileExt("pdf") - context.saveImage(path, **kwargs) + 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): + self._removePath() + + def sharingService_didFailToShareItems_error_(self, sharingService, items, error): + self._removePath() + + path = tempfile.mkstemp(suffix=f".{format}")[1] + self.saveImage(path, **kwargs) serviceMap = dict( airdrop=AppKit.NSSharingServiceNameSendViaAirDrop, @@ -492,6 +505,7 @@ def shareImage(self, format="pdf", service="airdrop", **kwargs): ) sharingService = AppKit.NSSharingService.sharingServiceNamed_(serviceMap[service]) + sharingService.setDelegate_(SharingServiceDelegate(path)) sharingService.performWithItems_([ AppKit.NSURL.fileURLWithPath_(path) ]) From 736b932425f6a29191b5fd31d4cdade295429cb2 Mon Sep 17 00:00:00 2001 From: Frederik Berlaen Date: Sun, 26 Dec 2021 11:08:06 +0100 Subject: [PATCH 7/8] simply wait a sec before removing the path --- drawBot/drawBotDrawingTools.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drawBot/drawBotDrawingTools.py b/drawBot/drawBotDrawingTools.py index be22abae..f9fd1359 100644 --- a/drawBot/drawBotDrawingTools.py +++ b/drawBot/drawBotDrawingTools.py @@ -6,6 +6,7 @@ import os import tempfile import random +import time from collections import namedtuple from .context import getContextForFileExt, getContextOptions, getFileExtensions, getContextOptionsDocs @@ -490,6 +491,9 @@ def _removePath(self): 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): From a6e452e66ae1da0036b0ce50ce2e95ef020550af Mon Sep 17 00:00:00 2001 From: Frederik Berlaen Date: Sun, 26 Dec 2021 11:18:51 +0100 Subject: [PATCH 8/8] test incoming service and only pop up when saveImage is succes full --- drawBot/drawBotDrawingTools.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/drawBot/drawBotDrawingTools.py b/drawBot/drawBotDrawingTools.py index f9fd1359..f50eca3e 100644 --- a/drawBot/drawBotDrawingTools.py +++ b/drawBot/drawBotDrawingTools.py @@ -499,20 +499,24 @@ def sharingService_didShareItems_(self, sharingService, items): def sharingService_didFailToShareItems_error_(self, sharingService, items, error): self._removePath() - path = tempfile.mkstemp(suffix=f".{format}")[1] - self.saveImage(path, **kwargs) - 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) - sharingService = AppKit.NSSharingService.sharingServiceNamed_(serviceMap[service]) - sharingService.setDelegate_(SharingServiceDelegate(path)) - sharingService.performWithItems_([ - AppKit.NSURL.fileURLWithPath_(path) - ]) + 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