Page 1 of 2

Send picture files via iMessage?

PostPosted: Sat Sep 09, 2017 1:27 pm
by racarter
Does anyone know of a Python way to send CCTV snapshot files via iMessage? Currently I do it with AppleScript, but since support for this is being withdrawn I need to change this.

I know I can use py-applescript, but a pure Python solution would be nice.

Re: Send picture files via iMessage?

PostPosted: Sat Sep 09, 2017 1:50 pm
by jay (support)
Python has the smtplib module to send emails. The iMessage question has been asked on reddit without a clear answer.

Re: Send picture files via iMessage?

PostPosted: Sat Sep 09, 2017 1:51 pm
by Different Computers
Being withdrawn? I've only heard strong admonitions from Jay and Matt to move to Python because they're uncertain what Apple is going to do with AppleScript. From what I can tell, it's still supported in High Sierra.

Also, please share these scripts.

Re: Send picture files via iMessage?

PostPosted: Sat Sep 09, 2017 2:05 pm
by jay (support)
Different Computers wrote:
Being withdrawn? I've only heard strong admonitions from Jay and Matt to move to Python because they're uncertain what Apple is going to do with AppleScript. From what I can tell, it's still supported in High Sierra.


viewtopic.php?f=4&t=19013

Re: Send picture files via iMessage?

PostPosted: Sat Sep 09, 2017 3:10 pm
by racarter
@Different Computers

Here's one of my AppleScripts. When the doorbell rings I run a Python script to grab three CCTV snapshots 5 seconds apart, then run this script to send the images via Messages.

Code: Select all
set theAttachment1 to POSIX file "/Snapshots/snap1.jpg"
set theAttachment2 to POSIX file "/Snapshots/snap2.jpg"
set theAttachment3 to POSIX file "/Snapshots/snap3.jpg"
tell application "Messages"
   send theAttachment1 to buddy "+441234567890" of service "E:mailaddress@mailserver.com"
   send theAttachment2 to buddy "+441234567890" of service "E:mailaddress@mailserver.com"
   send theAttachment3 to buddy "+441234567890" of service "E:mailaddress@mailserver.com"
end tell

Re: Send picture files via iMessage?

PostPosted: Sat Sep 09, 2017 3:12 pm
by racarter
Thanks Jay -I saw the Reddit discussion before I posted here. I guess it's the wrapper solution for now...

Re: Send picture files via iMessage?

PostPosted: Sat Sep 09, 2017 5:21 pm
by jay (support)
Not sure I'd change anything on the script above since it isn't talking to Indigo...

Re: Send picture files via iMessage?

PostPosted: Sun Sep 10, 2017 1:57 am
by racarter
The script is currently embedded in a trigger though - so I'll have to wrap it (or change it to external).

Re: Send picture files via iMessage?

PostPosted: Sun Sep 10, 2017 9:13 am
by Gusten
@racarter would you mind sharing the python script that collects the snapshots from the camera?

Re: Send picture files via iMessage?

PostPosted: Sun Sep 10, 2017 11:10 am
by matt (support)
racarter wrote:
The script is currently embedded in a trigger though - so I'll have to wrap it (or change it to external).

Yes, easiest approach would probably be to make it an external script file (via the Script Editor app). Indigo 7.2+ will be able to execute external AppleScript files. At this point we don't think we'll support embedded AppleScripts (even if they don't target Indigo Server).

Re: Send picture files via iMessage?

PostPosted: Sun Sep 10, 2017 11:45 am
by racarter
Thanks Matt.


@Gusten

Here's a script which works with my Hikvision DVR. I run three such scripts at 5 second intervals when the doorbell rings or when the side gate is opened, then send the images via iMessage.

Code: Select all
import urllib2
import base64
img = "/<path to image folder>/snap1.jpg"
devIP = "<DVR IP address>"
username = "<DVR user name>"
password = "<DVR password>"
request = urllib2.Request("http://%s/PSIA/Streaming/channels/101/picture" % devIP)
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)   
buffer = urllib2.urlopen(request).read()
f = open(img, 'wb')
f.write(buffer)
f.close()

Re: Send picture files via iMessage?

PostPosted: Mon Sep 11, 2017 11:56 pm
by Gusten
Thanx, this wont work with my camera, mayby someone can help me

I have an Axis IP camera, and the download link for an snapshot is

Code: Select all
http://192.168.213.238/jpg/image.jpg


So i tried this

Code: Select all
import urllib2
import base64
devIP = "192.168.213.238"
username = "xxxxx"
password = "xxxxx"
request = urllib2.Request("http://%s/jpg/image.jpg" % devIP)
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)   
buffer = urllib2.urlopen(request).read()
f = open(img, 'wb')
f.write(buffer)
f.close()


A get an HTTP Error 401: Unauthorized

if i change the path of the image to something wrong that do not exist i get

HTTP Error 404: Not found

so it seems like the script do go to the correct place, but is not able to log in?

If a use a browser, this way will work to se the image

Code: Select all
http://username:password@192.168.213.238/jpg/image.jpg


Can anyone figure out why this is not working?

/Martin

Re: Send picture files via iMessage?

PostPosted: Tue Sep 12, 2017 1:29 am
by racarter
@Gusten

I guess the authorisation for Axis different to Hikvision.

Try something like:

Code: Select all
base64.encodestring('%s:%s' % ('user', 'password'))[:-1]

Re: Send picture files via iMessage?

PostPosted: Tue Sep 12, 2017 2:35 am
by Gusten
Thanx, this didn't seems to work

Code: Select all
  Script Error                    embedded script: invalid syntax
   Script Error                    around line 5 - "buffer = urllib2.urlopen(username:password@192.168.213.238/jpg/image.jpg).read()"

Re: Send picture files via iMessage?

PostPosted: Tue Sep 12, 2017 3:46 am
by racarter
Working blind here! I've edited the code, which should remove the syntax error.