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

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()];
...
}
)
})();

Related

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

Getting Information about e-mail Attachments with Applescript

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

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".

Apple Event Handler Failure while trying to growl

I'm trying to growl from Ruby/Appscript, based on this sample Applescript code:
tell application "GrowlHelperApp"
set the enabledNotificationsList to {"Mail Notification"}
register as application "MailWidgetGrowlHelper" all notifications enabledNotificationsList default notifications enabledNotificationsList icon of application "Mail.app"
notify with name "Mail Notification" title (item 1 of argv) description (item 2 of argv) & return & return & (item 3 of argv) application name "MailWidgetGrowlHelper" icon of application "Mail.app" without sticky
end tell
My code looks like this:
GH = app("GrowlHelperApp.app")
enabledNotifications = "AppscriptMessage"
GH.register(:as_application => "AppscriptHelper",
:all_notifications => enabledNotifications,
:default_notifications => enabledNotifications)
the last call fails with:
Appscript::CommandError: CommandError OSERROR: -10000 MESSAGE: Apple event handler failed.
any ideas what I'm doing wrong?
D'Oh. The enabledNotifications should be an Array, not just a string.

Resources