Script to send images to email.

Posted on
Fri Aug 08, 2014 2:02 am
haavarda offline
User avatar
Posts: 702
Joined: Aug 18, 2012
Location: Norway

Script to send images to email.

I have been playing with a script that captures some images when someone rings the doorbell. I want it to take 4 images, and then send them to my mail. This is inspired from other examples and modified a bit, apparently to much... :). For some reason I only get one image attached with the mail. Can someone see what is wrong? And could this script me modified to be simpler or bether in any way? I am open for suggestions and improvements!

The reason for deleting the folder first is because I want to send the latest 4 images in the folder. Would it be possible to do this in another way? Overwrite?

Code: Select all
tell application "Finder"
   delete every item of entire contents of folder "Macintosh HD:Users:Server:Documents:Securityspy:Doorbell"
end tell

set theFile to "/Users/Server/Documents/SecuritySpy/Doorbell/picture1.jpg"
tell application "SecuritySpy" to capture image as theFile camera number 0

delay 1

set theFile to "/Users/Server/Documents/SecuritySpy/Doorbell/picture2.jpg"
tell application "SecuritySpy" to capture image as theFile camera number 0

delay 2

set theFile to "/Users/Server/Documents/SecuritySpy/Doorbell/picture3.jpg"
tell application "SecuritySpy" to capture image as theFile camera number 0

delay 2

set theFile to "/Users/Server/Documents/SecuritySpy/Doorbell/picture4.jpg"
tell application "SecuritySpy" to capture image as theFile camera number 0



delay 2

tell application "Mail"
   
   set theSubject to "Ringeklokke aktivert" -- the subject
   set theContent to "Noen er ved døra" -- the content
   set theAddress to "mymailadress@email.com" -- the receiver
   set theAttachmentFile to "Macintosh HD:Users:Server:Documents:Securityspy:Doorbell:picture1.jpg"
   set theAttachmentFile to "Macintosh HD:Users:Server:Documents:Securityspy:Doorbell:picture2.jpg"
   set theAttachmentFile to "Macintosh HD:Users:Server:Documents:Securityspy:Doorbell:picture3.jpg"
   set theAttachmentFile to "Macintosh HD:Users:Server:Documents:Securityspy:Doorbell:picture4.jpg"
   
   set msg to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
   
   tell msg to make new to recipient at end of every to recipient with properties {address:theAddress}
   tell msg to make new attachment with properties {file name:theAttachmentFile as alias}
   
   
   send msg
end tell


Håvard

Håvard

Posted on
Fri Aug 08, 2014 4:45 am
DaveL17 offline
User avatar
Posts: 6757
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Script to send images to email.

Hello Håvard - I don't know Applescript very well, but am happy to try and help.

Do you see four images in the folder after the script runs? This will help isolate whether it's the SecuritySpy part of the code or the Mail part of the code that's causing the trouble.

Also, I'm guessing that there should be an "end tell" after you take the four pictures, before you "tell application Mail" to do something..

Dave

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Fri Aug 08, 2014 4:55 am
haavarda offline
User avatar
Posts: 702
Joined: Aug 18, 2012
Location: Norway

Re: Script to send images to email.

Hi Dave,
Thank you for your feedback.

The files are put in the correct folder, and they are deleted and replaced when I run the script. So far so good.
It appears as it always sends the last image as attachment.
I tried to insert an end tell, but that results in a error:

Error script error: around characters 773 to 781
Error script error: Expected end of line, etc. but found “end tell”. (-2741)

Håvard

Posted on
Fri Aug 08, 2014 5:02 am
haavarda offline
User avatar
Posts: 702
Joined: Aug 18, 2012
Location: Norway

Re: Script to send images to email.

One solution I found is to just skip the files and email the folder containing the 4 files. Since I delete the folder contents before every run I will always just contain the newest images.

set theAttachmentFile to "Macintosh HD:Users:Server:Documents:Securityspy:Doorbell"

Maybe not the most elegant solution?

Håvard

Posted on
Fri Aug 08, 2014 5:46 am
DaveL17 offline
User avatar
Posts: 6757
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Script to send images to email.

Okay, I think I may have it. You've set "theAttachmentFile" to equal picture1 then picture2, etc. Then the key piece of code:
Code: Select all
tell msg to make new attachment with properties {file name:theAttachmentFile as alias}
which, at the time you run the line, is equal to picture4.

Will this work?
Code: Select all
tell application "Mail"
   
   set theSubject to "Ringeklokke aktivert" -- the subject
   set theContent to "Noen er ved døra" -- the content
   set theAddress to "mymailadress@email.com" -- the receiver
   set theAttachmentFile1 to "Macintosh HD:Users:Server:Documents:Securityspy:Doorbell:picture1.jpg"
   set theAttachmentFile2 to "Macintosh HD:Users:Server:Documents:Securityspy:Doorbell:picture2.jpg"
   set theAttachmentFile3 to "Macintosh HD:Users:Server:Documents:Securityspy:Doorbell:picture3.jpg"
   set theAttachmentFile4 to "Macintosh HD:Users:Server:Documents:Securityspy:Doorbell:picture4.jpg"
   
   set msg to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
   
   tell msg to make new to recipient at end of every to recipient with properties {address:theAddress}
   tell msg to make new attachment with properties {file name:theAttachmentFile1 as alias}
   tell msg to make new attachment with properties {file name:theAttachmentFile2 as alias}
   tell msg to make new attachment with properties {file name:theAttachmentFile3 as alias}
   tell msg to make new attachment with properties {file name:theAttachmentFile4 as alias}   
   
   send msg
end tell

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Fri Aug 08, 2014 6:45 am
haavarda offline
User avatar
Posts: 702
Joined: Aug 18, 2012
Location: Norway

Re: Script to send images to email.

Thank you Dave, that worked perfect!

Håvard

Håvard

Posted on
Fri Aug 08, 2014 7:00 am
DaveL17 offline
User avatar
Posts: 6757
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Script to send images to email.

Cool beans.

Since you asked, there is probably a way to make the code more streamlined (for example, storing the attachment references in a list) but at least you're on your way.
Dave

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Fri Aug 08, 2014 5:42 pm
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Script to send images to email.

Or maybe...

Code: Select all
tell application "SecuritySpy"
   capture image camera number 0 as "/Users/Server/Documents/SecuritySpy/Doorbell/picture1.jpg" with overwrite
   delay 1
   capture image camera number 0 as "/Users/Server/Documents/SecuritySpy/Doorbell/picture2.jpg" with overwrite
   delay 2
   capture image camera number 0 as "/Users/Server/Documents/SecuritySpy/Doorbell/picture3.jpg" with overwrite
   delay 2
   capture image camera number 0 as "/Users/Server/Documents/SecuritySpy/Doorbell/picture4.jpg" with overwrite
end tell

tell application "Mail"
   
   set theSubject to "Ringeklokke aktivert" -- the subject
   set theContent to "Noen er ved døra" -- the content
   set theAddress to "mymailadress@email.com" -- the receiver
   set theAttachmentFile1 to "Macintosh HD:Users:Server:Documents:Securityspy:Doorbell:picture1.jpg"
   set theAttachmentFile2 to "Macintosh HD:Users:Server:Documents:Securityspy:Doorbell:picture2.jpg"
   set theAttachmentFile3 to "Macintosh HD:Users:Server:Documents:Securityspy:Doorbell:picture3.jpg"
   set theAttachmentFile4 to "Macintosh HD:Users:Server:Documents:Securityspy:Doorbell:picture4.jpg"
   
   set msg to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
   
   tell msg to make new to recipient at end of every to recipient with properties {address:theAddress}
   tell msg to make new attachment with properties {file name:theAttachmentFile1 as alias}
   tell msg to make new attachment with properties {file name:theAttachmentFile2 as alias}
   tell msg to make new attachment with properties {file name:theAttachmentFile3 as alias}
   tell msg to make new attachment with properties {file name:theAttachmentFile4 as alias}
   
   send msg
end tell


Carl

Posted on
Sat Aug 09, 2014 12:00 am
haavarda offline
User avatar
Posts: 702
Joined: Aug 18, 2012
Location: Norway

Re: Script to send images to email.

Thank you Carl.
That overwrite function was what I was looking for. This makes the script even more robust.

Håvard

Håvard

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 15 guests