Create Applescript to email a file - applescript

I want to have the Apple mail application accept and email from me and then email a file to me. For example, I want to send an email with the subject "#FILE myfile.doc" and have the script trigger because of the #FILE tag in the subject, and then email me back the file called "myfile.doc".
The files will always be in the same path, but it would be nice to be able to specify the path in the script so I can create different ones for different directories.
I know nothing about Applescript and have dabbled in Automator. I only see in Mail that you can trigger an Applescript from a rule. So I don't know if this can be done in Automator.
Please be basic with your responses because I am a newbie to this.
The purpose is to get a file off my computer when I am away.
Thanks,
ROY

You can do it using a mail rule in Mail. Specify your email address as sender and the subject starting with #FILE. Let the mail rule trigger a script like this:
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with thisMessage in theMessages
set theSender to sender of thisMessage
set theSubject to subject of thisMessage
if theSubject begins with "#FILE " then
set theAnswer to make new outgoing message with properties {visible:true}
tell theAnswer
try
tell current application
set theFilePathToSend to (POSIX file (text 7 thru -1 of theSubject)) as alias
end tell
set content to "And here is your file: " & rich text 7 thru -1 of theSubject & return & return
tell content to make new attachment with properties {file name:theFilePathToSend} at after the last paragraph
on error errStr
set content to "An error occured: " & return & errStr
end try
set subject to "Re: " & theSubject
make new to recipient at end of to recipients with properties {address:theSender}
send
end tell
end if
end repeat
end tell
end perform mail action with messages
end using terms from
Using the reply-handler didn't work, but the make new outgoing message did! Specify the full path to the file as subject.
Be aware that this IS a security hole! Sender's email addresses can be faked and somebody knowing this can get nearly every file he (or she) wants. You could specify the allowed folders in this script, i.e. if theSubject begins with "#FILE /Users/rbarr/Desktop/Available files/" then or just by adjusting the conditions your mail rule is triggered.
Enjoy, Michael / Hamburg

Related

apple script- email transmission containing groups of 20 attachments taken from a folder containing 1000 files

sorry I didn't paste my piece of code but I don't know where to start... I'm talking about apple script. I've found some code to send emails and I would like to implement a script that does what I'm about to write.
I have a folder that contains about 1000 pdf files that I have to send in groups of 20 at a time via email.
I have a bit of confusion in my head but conceptually I think it can be done by counting all the files in the folder, then every 20 files I take the respective file names and set a variable with these names, including the path and I send the first email, then I proceed with the other 20 and so on until it has completed the number of files in the folder. Is there someone who can help me? A thousand thanks!!
I am attaching the portion of code relating to sending via email...
I tried the given code and it works. it sends the email but i didn't check if it sends attachment
`
tell application "Mail"
set theFrom to ""
set theTos to {""}
set theCcs to {}
set theBccs to {}
set theSubject to ""
set theContent to ""
set theSignature to ""
set theAttachments to {}
set theDelay to 1
set theMessage to make new outgoing message with properties {sender:theFrom, subject:theSubject, content:theContent, visible:false}
tell theMessage
repeat with theTo in theTos
make new recipient at end of to recipients with properties {address:theTo}
end repeat
repeat with theCc in theCcs
make new cc recipient at end of cc recipients with properties {address:theCc}
end repeat
repeat with theBcc in theBccs
make new bcc recipient at end of bcc recipients with properties {address:theBcc}
end repeat
repeat with theAttachment in theAttachments
make new attachment with properties {file name:theAttachment as alias} at after last paragraph
delay theDelay
end repeat
end tell
# macOS 10.12+ know bug
# https://stackoverflow.com/questions/39860859/setting-message-signature-of-outgoing-message-using-applescript
# set message signature of theMessage to signature theSignature
send theMessage
end tell
`

Applescript for mail rules

I have created a rule that messages move into a subfolder when they according to certain criteria.
Additionally, I have saved in this rule an AppleScript that this mail copied subsequently into an DMS.
It is working.
My problem is, that the name of the subfolder is (for now) not passed to the script. It contains only "INBOX":
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with theMessage in theMessages
set nameAccount to name of mailbox of theMessage # here is the problem !
try
tell theMessage
— at this point get the data out of the mail
end tell
tell application id „DMS“
— here the data will save to the dms
end tell
end try
end repeat
end tell
# end try
# end tell
end perform mail action with messages
end using terms from
How do I get the name of the destination folder from the active mail rule?
Instead of the name get the whole mailbox object, it contains the actual reference in the folder hierarchy, to create a subfolder you need the reference anyway.
set theMailbox to mailbox of theMessage
PS: the tell application "Mail" block is not needed, the using terms block is sufficient to evaluate the terminology.

Select a type of sender for email with wildcard

I would like to send an email with applescript. This script will be used on a few computers and the email should be sent by a certain type of account containing "mynetwork.com" in the email address.
Is there a way to automatically select the computer email account containing "my network.com" ? Obviously the wildcard * is not working see code below
property faxboxEmail : {"fax#opilbox.com"}
property theNumber : ""
property theContent : ""
property theSender : "*#mynetwork.com"
tell application "Mail"
set newMessage to make new outgoing message with properties {visible:true, subject:theNumber, content:theContent, sender:theSender}
tell newMessage
make new to recipient with properties {address:faxboxEmail}
end tell
end tell
The sender address for accounts in Mac OS X Mail are stored in the sender addresses property in an account. So, technically what you want is something like get account whose email addresses ends with "#mynetwork.com". However, the email addresses property is a simple list, and AppleScript doesn’t have dynamic searches into lists like that.
However, the number of accounts on any personal computer should be fairly small, so looping through them shouldn’t be a problem.
property faxboxEmail : {"fax#opilbox.com"}
property theNumber : "Nine"
property theContent : "Hello, World"
set foundAddress to false
tell application "Mail"
repeat with potentialSender in accounts
tell potentialSender
repeat with potentialAddress in email addresses as list
if potentialAddress ends with "#mynetwork.com" then
set foundAddress to true
exit repeat
end if
end repeat
end tell
if foundAddress then
exit repeat
end if
end repeat
if foundAddress then
set senderName to full name of potentialSender
set senderAddress to senderName & " <" & potentialAddress & ">"
set newMessage to make new outgoing message with properties {visible:true, subject:theNumber, content:theContent, sender:senderAddress}
tell newMessage
make new to recipient with properties {address:faxboxEmail}
end tell
end if
end tell
You may also find it useful to look at the properties of each account, using something like:
tell application "Mail"
--or “account 1”, “account 3”, etc., up to the number of accounts
get properties of account 2
end tell
If you run that and then look in the results pane, you’ll see all of the properties, including the email addresses property. If your script isn’t doing what you expect it to do, post not just the script, but also the values of the property you’re looking at, in this case the email addresses property. You will probably want to use a test computer with fake example.com, example.org, and example.net email addresses, so as not to expose real email addresses to harvesters.
You also may find it easier to build your scripts from the ground up. For example, in this case, you would want to write a script that sends a hard-coded email from a hard-coded sender. Only once that aspect of the script is working, would you want to add the wrinkle about searching for a dynamic sender. This will simplify your programming process by making each task smaller, and will also make it more obvious where the code is going astray.

How to copy a file from a selected message to an outgoing message in outlook through applescript?

I have been trying to figure out how to copy an attachment from an incoming message in outlook to a new outgoing message through applescript.
The code below is what I've got so far, but it's definitely wrong. Outlook throws and error at me stating: "Microsoft Outlook got an error: Can’t make missing value into type file."
tell application "Microsoft Outlook"
-- get the incoming message
set objMessage to item 1 of (get current messages)
set myMessage to make new outgoing message
-- iterate through attachments
-- for each attachment make a new attachment in the message to send identical properties
-- ???
repeat with _attachment in attachments of objMessage
set n to name of _attachment
set ct to content type of _attachment
set f to file of _attachment
set fs to file size of _attachment
tell myMessage
make new attachment with properties {name:n, content type:ct, file:f, file size:fs}
end tell
end repeat
open myMessage
end tell
I've tried a couple of other things, but I can't seem to get at the file property of the incoming messages attachment.
Does anybody have any experience working with files in Outlook and Applescript in this way?
Dylan
It appears that just passing the attachment between messages doesn't work but saving the attachment first and then passing the file name to the file property for the new attachment does.
tell application "Microsoft Outlook"
local mynames, msg1, msg2, saveToFolder, saveAsName
set msg1 to last item of incoming messages
set msg2 to make new outgoing message with properties {subject:"Testing this thing"}
set saveToFolder to POSIX path of (path to desktop)
repeat with atts in attachments of msg1
set saveAsName to saveToFolder & (name of atts)
log saveAsName
save atts in saveAsName
tell msg2
make new attachment with properties {file:saveAsName} -- with properties {file:atts}
end tell
end repeat
open msg2
end tell
Note 1: You will probably want to add logic to remove the files (original attachments) that were saved from the incoming message.
Note 2: Another option would be to find the location of the file from the incoming message and just use that alias instead of saving them first just to add them to the outbound message'

I need to convert an email address to a recipient

Need to convert an email address from Contacts into a recipient in Mail.
tell application "Contacts"
set sendTo to every person's email 1
end tell
tell application "Mail"
set mesg to make new outgoing message
set recipient of mesg to sendTo -- Need to convert here.
set subject of mesg to "A title"
set content of mesg to "A body"
send mesg
end tell
You've got several problems in your code...
When you get "every person's email 1" you get a list of references, not a list of email addresses. To get the actual email address from a reference you get its "value".
sendTo will be a list, so you have to loop over the list and add each address one at a time in mail.
there's different kinds of recipients. You need to use a "to recipient".
Try this:
tell application "Contacts"
set sendToList to value of every person's email 1
end tell
set emailSender to "me#me.com"
set theSubject to "The subject of the mail"
set theContent to "message body"
tell application "Mail"
set mesg to make new outgoing message with properties {sender:emailSender, subject:theSubject, content:theContent, visible:true}
tell mesg
repeat with i from 1 to count of sendToList
set thisEmail to item i of sendToList
if thisEmail is not missing value then
make new to recipient at end of to recipients with properties {address:thisEmail}
end if
end repeat
--send
end tell
end tell

Resources