Skip to content

Instantly share code, notes, and snippets.

@junctionseven
Created January 23, 2015 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junctionseven/5125f3d70ad3905ff4d0 to your computer and use it in GitHub Desktop.
Save junctionseven/5125f3d70ad3905ff4d0 to your computer and use it in GitHub Desktop.
OSX JavaScript for Automation example for parsing email content into a Numbers Spreadsheet.
function splitBody (body)
{
// Edit out some of the unnessesary text
var str = body.replace("(Sent via iInHabit)", "");
str = str.replace("\n\n", "");
// Get rid of the preambles
str = str.replace("Name:", "");
str = str.replace("Email Address:", "");
str = str.replace("I am...:", "");
str = str.replace("Post Code:", "");
str = str.replace("Twitter:", "");
str = str.replace("Instagram:", "");
// Each line is an entry
var entry = str.split("\n");
console.log (entry);
// Populate the sheet
for (c = 0; c < entry.length; c++)
{
var thisRow = selectedTable.rows[rowTracker];
var thisCell = thisRow.cells[c];
thisCell.value = entry[c];
}
rowTracker++;
}
function createSpreadsheet()
{
document = numbers.Document().make();
var sheet = document.activeSheet;
sheet.name = "Sign Ups";
selectedTable = sheet.tables[0];
selectedTable.name = "Sign Ups";
selectedTable.rowCount = messageList.length+1;
selectedTable.columnCount = 7;
// Set the headers up
var headers = ["Name", "Email", "I am", "Postcode", "Twitter", "Instagram"];
var headerRow = selectedTable.rows[0];
for (i = 0; i < headers.length; i++)
{
var thisCell = headerRow.cells[i];
thisCell.value = headers[i];
}
}
/*
-- Example Email --
Name: Joe Bloggs
Email Address: example@example.com
I am...: made up person
Post Code: E17
Twitter: @nope
Instagram: nope
*/
//======================================= =======================================
// Open Numbers ready for input
var numbers = Application('Numbers');
numbers.activate();
var document;
var selectedTable;
var rowTracker = 1;
// Open mail
mail = Application('Mail');
mail.includeStandardAdditions = true;
var messageList = mail.selection();
createSpreadsheet();
for (i = 0; i < messageList.length; i++)
{
var thisMessage = messageList[i];
var messageBody = thisMessage.content();
splitBody(messageBody);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment