Integrated eMail Support
Using Omni Automation you can construct and manipulate outline documents, apply styles and import data from a variety of sources. And with integrated scripting support for creating and sending email, it becomes easy to automate the sending of content and documents. The following documentation demonstrate how to access and use this ability, beginning with a set of parameters for generating an email.
Constructor
Omni Automation in OmniFocus supports an Email class, design for creating and sending electronic mail messages. To instantiate an instance of this class, it is preceded by the new constructor.
new Email() --> Email • Creates a new eMail object.
Instance Properties
As with most scriptable objects, an instance of the Email class has properties that define its design and usage. Here is the list of properties for this class:
blindCarbonCopy (String or Array of String or null) • Blind carbon copy allows the sender of a message to conceal the person entered in the Bcc: field from the other recipients. This concept originally applied to paper correspondence and now also applies to email. (Wikipedia)
body (String or nil) • The main text content of the mail message.
carbonCopy (String or Array of String or null) • In the past, a carbon copy was the under-copy of a document created when carbon paper was placed between the original and the under-copy during the production of a document. Nowadays "carbon copy" is often used metaphorically to refer simply to an exact copy or the inclusion of a recipient of an electronic message.
fileWrappers (Array of FileWrapper) • A Filewrapper is a representation of a node (a file, directory, or symbolic link) in the file system. File wrappers represent a file system node as an object that can be displayed as an image (and possibly edited in place), saved to the file system, or transmitted to another application. In Omni Automation they are used during export to identify the format in which the outline document is to saved to file and added to the outgoing message as an attachment.
receiver (String or Array of String or null) • The person or identity to receive the mail message, represented by a standard email address string.
subject (String or nil) • The title of the mail message.
Instance Functions
The Email class contains a single command generate() for processing instances of the class.
generate() • Presents the generated email to the user for them to send (or discard). NOTE: on iOS, any included attachment FileWrappers that are directories will be converted to Zip files.
Basic eMail Script
email = new Email()
email.subject = "Omni Automation"
email.body = "Greetings!\n\nHere are my suggestions:"
email.receiver = "support@omnigroup.com"
email.generate()
NOTE: the value of the receiver can be a single email address or an array of email addresses.
Here’s a plug-in that creates a new outgoing mail message containing the contents of the note of the selected task:
Note to eMail
/*{
"type": "action",
"targets": ["omnifocus"],
"author": "Otto Automator",
"identifier": "com.omni-automation.of.note-to-email",
"version": "1.1",
"description": "This action will add the note text of the selected task to a new outgoing mail message.",
"label": "Note to Email",
"shortLabel": "Note to Email",
"paletteLabel": "Note to Email",
"image": "mail.fill"
}*/
(() => {
const action = new PlugIn.Action(async function(selection, sender){
task = selection.tasks[0]
taskTitle = task.name
noteText = task.note
if(noteText === ""){
alertMsg = "The note of the selected task contains no text."
new Alert("Missing Data", alertMsg).show()
} else {
email = new Email()
email.subject = "Note of task “" + taskTitle + "”"
email.body = noteText
email.receiver = ""
email.generate()
}
});
action.validate = function(selection, sender){
return (selection.tasks.length === 1)
};
return action;
})();
Here’s an example action plug-in that creates a new mail message containing a OmniFocus URL for generating a copy of the selected task:
Send Task Link
/*{
"type": "action",
"targets": ["omnifocus"],
"author": "Otto Automator",
"identifier": "com.omni-automation.of.email-task-link",
"version": "1.5",
"description": "This action will create an email containing a link for generating a copy of the selected task.",
"label": "Send Task Link",
"shortLabel": "Task Link"
"paletteLabel": "Task Link",
"image": "mail.fill"
}*/
(() => {
const action = new PlugIn.Action(async function(selection, sender){
task = selection.tasks[0]
linkStr = "omnifocus:///add?name="
taskName = encodeURIComponent(task.name)
linkStr = linkStr + taskName
if (task.note.length > 0){
linkStr = linkStr + "¬e=" + encodeURIComponent(task.note)
}
if (task.dueDate){
linkStr = linkStr + "&due=" + encodeURIComponent(task.dueDate.toLocaleString())
}
if (task.deferDate){
linkStr = linkStr + "&defer=" + encodeURIComponent(task.deferDate.toLocaleString())
}
if (task.tags.length > 0){
tagNames = task.tags.map(tag => {return tag.name})
tagStr = tagNames.join(",")
linkStr = linkStr + "&tags=" + encodeURIComponent(tagStr)
}
if (task.estimatedMinutes != undefined && task.estimatedMinutes != null){
taskDuration = task.estimatedMinutes
linkStr = linkStr + "&estimate=" + String(taskDuration) + "m"
}
if (task.flagged){
linkStr = linkStr + "&flag=" + "true"
}
var email = new Email()
email.subject = "OmniFocus Task URL"
email.body = "<" + linkStr + ">"
//email.receiver = "recipient@recipientcompany.com"
email.generate()
});
action.validate = function(selection, sender){
return (selection.tasks.length === 1 && selection.tasks[0].hasChildren === false)
};
return action;
})();
Including Attachments
The following are examples of creating and sending mail messages whose contents are derived from the current document.
Send Document Backup
/*{
"type": "action",
"targets": ["omnifocus"],
"author": "Otto Automator",
"identifier": "com.omni-automation.send-omnifocus-backup",
"version": "1.1",
"description": "This action will create a new email message containing a backup copy of the OmniFocus database.",
"label": "Send Backup",
"shortLabel": "Send Backup",
"paletteLabel": "Send Backup",
"image": "mail.fill"
}*/
(() => {
const action = new PlugIn.Action(async function(selection, sender){
fileTypeID = "com.omnigroup.omnifocus.filetype.ofocus-backup"
baseName = "OF Backup"
wrapper = await document.makeFileWrapper(baseName, fileTypeID)
email = new Email()
email.subject = "Export of " + baseName
email.body = "Here is a backup of my OmniFocus database:\n\n"
email.fileWrappers = [wrapper]
email.generate()
});
action.validate = function(selection, sender){
return true
};
return action;
})();