Getting Information about e-mail Attachments with Applescript - macos

I am trying to receive information about attachments in the Mail App. So far I am able to receive Data from the e-mail that is selected. But I would additionally like to receive information about the attachments.
tell application "Mail"
set selectedMessages to selection
set theMessage to item 1 of selectedMessages
set theMailbox to mailbox of theMessage
set mailAddresses to email addresses of account of theMailbox
return theAttachment in theMessage's mail attachments
end tell
The Script works if I use return mailAdresses but I cannot get Information about the attachment. Any hints?

try this, the return value contains the data
theMessage
theMailbox
mailAddresses
name und mime type of all attachments as a list
of each message of all selected messages
tell application "Mail"
set selectedMessages to selection
set mailBoxData to {}
repeat with aMessage in selectedMessages
set theMailbox to mailbox of aMessage
set mailAddresses to email addresses of account of theMailbox
set attachmentData to {}
repeat with anAttachment in (get mail attachments of aMessage)
tell anAttachment to set end of attachmentData to {name, MIME type}
end repeat
set end of mailBoxData to {theMessage, theMailbox, mailAddresses, attachmentData}
end repeat
return mailBoxData
end tell

Related

Process emails from mac using javascript (JXA) instead of applescript

I am trying to make an email parser in JXA. I was able to do something similar in applescript:
tell application "Mail" to set theMessages to every message of mailbox "Inbox" of account "MyAcount" whose subject is equal to "Search Text"
repeat with aMessage in theMessages
tell application "Mail" to set {mContent, mDate} to {content, date received} of aMessage
......process each mail.....
end repeat
How would I replicate this but in javascript?
Perhaps with something like this. Hope this helps.
(() => {
'use strict';
const
appMail = Application('Mail'),
account = appMail.accounts.byName('MyAccount'),
inbox = account.mailboxes.byName('INBOX'),
listMessages = inbox.messages.whose({
subject: 'Search Text'
})();
return listMessages.map(x => {
const [mContent, mDateReceived] = [x.content(), x.dateReceived()];
...
}
)
})();

How do I go about putting a chunk of code into a Variable?

I've been trying to get chunks of code into variables so I could easily just place the variable and shorten the code by a lot.
Here's the code that I want to turn into a variable-
set recipientName to "Joe"
set recipientAddress to "joel#here.com"
set theSubject to "Type your subject here!"
set theContent to "Type your message content here!"
tell application "Mail"
##Create the message
set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
##Set a recipient
tell theMessage
make new to recipient with properties {name:recipientName, address:recipientAddress}
##Send the Message
send
end tell
end tell
You probably want to wrap your Mail code in a reusable handler, like this:
to sendEmail(recipientName, recipientAddress, theSubject, theContent)
tell application "Mail"
##Create the message
set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
##Set a recipient
tell theMessage
make new to recipient with properties {name:recipientName, address:recipientAddress}
##Send the Message
send
end tell
end tell
end sendEmail
You can then send commands with different parameters at any time like this:
sendEmail("Joe", "joel#here.com", "Type your subject here!", "Type your message content here!")
and this:
sendEmail("Sam", "sam#there.com", "Type another subject here!", "Type more message content here!")
etc.

Cannot set outlook message body on compose mode using appleScript

I am trying to get and set outlook message body on compose mode.setting value does not work. is something wrong with script ?. but get value is working fine.
activate application "Microsoft Outlook"
tell application "System Events"
tell process "Microsoft Outlook"
get value of static text 1 of group 1 of UI element 1 of scroll area 4 of splitter group 1 of window 1
set value of static text 1 of group 1 of UI element 1 of scroll area 4 of splitter group 1 of window 1 to "sample text"
end tell
end tell
I would avoid using UI scripting unless there is no other way.
This script shows you how to set the message body.
tell (current date) to get (it's month as integer) & "-" & day & "-" & (it's year as integer)
set MyDay to the result as text
set Mytitle to "Daily Email - " as text
set Mytitle to Mytitle & MyDay
tell application "Microsoft Outlook"
set newMessage to make new outgoing message with properties {subject:Mytitle}
make new recipient at newMessage with properties {email address:{name:"Name", address:"test#example.com"}}
#make new cc recipient at newMessage with properties {email address:{name:"Name", address:"test#example.com"}}
--- SET EMAIL BODY USING PLAIN TEXT ---
set plain text content of newMessage to "This is just a TEST."
open newMessage
end tell

Get reference to an Outook message duplicate

This code will duplicate a Microsoft Outlook message to the drafts folder:
tell application "Microsoft Outlook"
...
-- find the template give an ID
set theTemplate to message id theID
-- duplicate the message
duplicate theTemplate to drafts
...
end tell
I need a reference to the duplicate for additional processing.
Unfortunately, this doesn't work:
...
-- this will create a duplicate
set theDuplicate to (duplicate theTemplate to drafts)
-- produces an error that reads "The variable theDuplicate is not defined."
display dialog (subject of theDuplicate) & " [" & (id of theDuplicate) & "]"
How do I get a reference to the message that was just duplicated? Its ID would be a satisfactory alternative.
There must be a better way but...
--For Testing
set theID to 39110
tell application "Microsoft Outlook"
set oldIds to my getDraftIds()
-- find the template give an ID
set theTemplate to message id theID
--duplicate the message
duplicate theTemplate to drafts
set newIds to my getDraftIds()
set duplicatedMessage to message id (my findNewID(oldIds, newIds))
end tell
on getDraftIds()
set messageIDs to {}
tell application "Microsoft Outlook"
set draftFolders to (every folder whose name = "Drafts")
repeat with dFolder in draftFolders
set messageIDs to messageIDs & id of dFolder's messages
end repeat
end tell
end getDraftIds
on findNewID(oldList, newList)
repeat with mID in newList
if mID is not in oldList then return mID
end repeat
end findNewID
I guess the duplicate method is pretty limited in this regard. I tried the more generic copy method too - the message is copied, but again no ID is returned.
Here's another possible way to do this with no repeat loops:
Create a new, temporary category
Mark the original message with the new category
Duplicate - the duplicate message will also be marked with the category
search for messages marked with the temporary category - you should only get two - the original and the duplicate
Delete the temporary category
Here is the code (rather unfinished):
tell application "Microsoft Outlook"
try
set tempCategory to category "Temporary Category"
on error number -1728
set tempCategory to (make new category with properties {name:"Temporary Category"})
end try
set messageList to selected objects
set origMsg to item 1 of messageList
display dialog "Original Message ID: " & id of origMsg
set msgCat to category of origMsg
set end of msgCat to tempCategory
set category of origMsg to msgCat
duplicate origMsg to drafts
delay 1 -- sigh, it seems to take a bit of time before the category markings are reflected in the spotlight DB
--set msgList to messages whose category contains tempCategory
set currentIdentityFolder to quoted form of POSIX path of (current identity folder as string)
set tempCatMsgs to words of (do shell script "mdfind -onlyin " & currentIdentityFolder & " 'com_microsoft_outlook_categories == " & id of tempCategory & "' | xargs -I % mdls -name com_microsoft_outlook_recordID '%' | cut -d'=' -f2 | sort -u | paste -s -")
if item 1 of tempCatMsgs is (id of origMsg) as text then
set dupMsgId to item 2 of tempCatMsgs
else
set dupMsgId to item 1 of tempCatMsgs
end if
delete tempCategory
display dialog "Original Message ID: " & id of origMsg & return & "Duplicate Message ID: " & dupMsgId
end tell
I thought it would be easier to find messages with a given category using the OL dictionary, but instead had to resort a spotlight search. I'm sure there is a better way to do this.
Also adding a category to a message was harder than I though - again I'm sure this can be done more efficiently.
-- create a temporary folder to hold duplicates
on createFolder(theName)
tell application "Microsoft Outlook"
set theFolder to make new mail folder with properties {name:theName}
end tell
end createFolder
-- find folder by name
on findFolder(theName)
tell application "Microsoft Outlook"
set theFolder to make new mail folder with properties {name:theName}
end tell
end findFolder
-- duplicate the message and get a reference
on duplicateIt(theID)
set theDestination to findFolder("foo bar")
tell application "Microsoft Outlook"
--find the template
set theTemplate to message id theID
-- duplicate it to the temporary location
(duplicate theTemplate to theDestination)
-- get the first item in the folder
set theDuplicate to item 1 of theDestination
-- do something with it
...
end tell
end duplicateIt

Why doesn't this simple applescript work any more in ML mail.app (as a rule)

It works only when you right click on the mail message and choose "run rules", but not on incoming messages (without interaction).
The first dialog is shown both when incoming or running manually, but the second dialog (with the id) is only shown when running manually. Nothing is shown in console.log
Any ideas?
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with theMessage in theMessages
display dialog "inside"
set theId to id of theMessage
display dialog "the id is " & theId
end repeat
end tell
end perform mail action with messages
end using terms from
update: i added a try catch block around
set theId to id of theMessage
and this is the error I get:
Can't get class mssg 1 of class mbxp "Incoming POP messages" of class mact "Telenet". Invalid index. -1719
Any idea what this means? I don't get the error when applying rules manually.
Update 2: OK i found out that incoming messages don't have an ID yet. That's a problem since I want to save the email to disk:
set theEmail to (do shell script "mdfind -onlyin ~/Library/Mail \"kMDItemFSName = '" & theId & ".emlx'\"")
set archiveName to theId & "-" & (extract address from theMessage's sender) & ".emlx"
set saveLocation to "Users:wesley:Documents:Incoming:"
do shell script "cp '" & theEmail & "' '" & POSIX path of saveLocation & "';"
Is there any way around this?
As promised in my comment, here's an AppleScript that logs Mail messages' subject, id, and message id to ~/Desktop/log.txt.
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
try
repeat with theMessage in theMessages
LogText("subject: " & (theMessage's subject as string))
LogText("id: " & (theMessage's id as string))
LogText("message id: " & (theMessage's message id as string))
LogText("")
end repeat
on error errorText number errorNumber
tell application "Mail" to display alert ("Error: " & errorNumber) message errorText
return
end try
end perform mail action with messages
end using terms from
on LogText(theText)
tell application "Finder"
set logPath to (path to the desktop as text) & "log.txt"
try
set writeText to open for access file logPath with write permission
set currentDateAsString to do shell script "date +\"%Y-%m-%d %T\""
write (currentDateAsString & " " & (theText as text) & return) to writeText starting at ((get eof writeText) + 1)
close access writeText
on error errorText number errorNumber
close access writeText
error errorText number errorNumber
end try
end tell
end LogText
I'm also running OS X 10.8.2 and Mail 6.2. As I said, I'm surprised to say this, but triggering the above AppleScript via mail rule works just as well for incoming messages as it does when selecting the menu "Apply Rules".

Resources