Skip to content

Instantly share code, notes, and snippets.

@symmetriq
Last active August 29, 2015 14:08
Show Gist options
  • Save symmetriq/80def83928fb91def58f to your computer and use it in GitHub Desktop.
Save symmetriq/80def83928fb91def58f to your computer and use it in GitHub Desktop.
JavaScript Automation in OS X Yosemite
var Mail = Application('Mail')
, matches
, pad;
pad = function (input, length, fill) {
if (!fill || fill.length > 1) {
fill = (typeof input === 'string') ? ' ' : '0';
}
input = String(input);
return (
input.length >= length
? input
: new Array(length - input.length + 1).join(fill) + input
);
}
matches = Mail.inbox.messages.whose({subject: {_contains: 'Friday'}})();
matches.forEach(function (message) {
var date = message.dateReceived()
, dateString = date.getFullYear() + '-' + pad(date.getMonth() + 1, 2) + '-' + pad(date.getDate(), 2);
console.log(dateString + ' ' + message.subject());
});
@symmetriq
Copy link
Author

This gets every message from your inbox(es) in Mail with a subject that contains the word "Friday", and prints them in the "Messages" pane at the bottom of the Script Editor window.

The correct extension for OSA (Open Scripting Architecture) scripts is .scpt, but GitHub keeps changing it back to AppleScript when I set it to JavaScript, so I gave up and renamed it .js. Either way, you can just copy and paste this into the Script Editor app in OS X (requires Yosemite) and try it out for yourself.

Some basic info here:
https://developer.apple.com/library/mac/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/

WWDC 2014 session:
https://developer.apple.com/videos/wwdc/2014/?id=306

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment