Created
September 17, 2018 20:51
-
-
Save marcgeld/35bbfc979bbdec754824d659e913b62c to your computer and use it in GitHub Desktop.
jxa: Find and filter emails in macOS Mail.app from info@vasttrafik.se. Create json output with extracted information.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//debugger; | |
'use strict'; | |
var mailboxName = 'your.email@here.com' | |
var mail = new Application("Mail") | |
var account = mail.accounts.whose({ name: { _contains: mailboxName } }, {ignoring: 'case'}) | |
var mailbox = account.mailboxes.whose({ name: { _contains: 'INBOX' } }, {ignoring: 'case'}) | |
// var messages = mail.selection(); | |
var messages = mailbox.messages.whose({ | |
sender: { _contains: 'info@vasttrafik.se' }, | |
subject: { _contains: 'Kvitto - Västtrafik To Go' }, | |
_and: [ | |
{ dateSent: { '>': new Date(2016,12,31) } }, | |
{ dateSent: { '<': new Date(2018,1,1) } }, | |
] | |
}); | |
if ( Object.keys(messages).length > 0 ) { | |
//var lines = ['"datum", "biljett", "pris", "moms", "total"'] | |
var jsonObj = [] | |
for( var i in messages ) { | |
var msgContent = messages[i].content(); | |
var match = null; | |
var obj = {}; | |
if (/Betalningsbekräftelse*/.test(msgContent)){ | |
// Beställnings datum | |
if (match = /gjord den (.*)!/.exec(msgContent)) { | |
//match.reduce(reducer) | |
match.shift() | |
obj.datum = match.join() | |
} | |
// Biljett typ | |
if (match = /Förköpt: (.*)/.exec(msgContent)) { | |
match.shift() | |
obj.biljett = match.join() | |
} | |
// Pris | |
if (match = /([\d,].*) SEK/.exec(msgContent)) { | |
match.shift() | |
obj. pris = match.join() | |
} | |
// Moms | |
if (match = /Varav moms 6 %: ([\d,].*) SEK/.exec(msgContent)) { | |
match.shift() | |
obj.moms = match.join() | |
} | |
// Summa beställning | |
if (match = /Summa betalt: ([\d,].*) SEK/.exec(msgContent)) { | |
match.shift() | |
obj.total = match.join() | |
} | |
jsonObj.push(obj) | |
} | |
} | |
JSON.stringify(jsonObj) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment