VCOF0041
OmniFocus: Append Mail Link to Note
This Voice Control command will append a mail creation link to the note of the selected project or task.
The appended mailto link provides a shortcut for communicating with others regarding a task or project. Provided settings in the input dialog are remembered between executions of the script.
Scope and Syntax
Language: English (United States 🇺🇸 · Great Britain 🇬🇧 · Australia 🇦🇺 · Canada 🇨🇦) • Scope: OmniFocus
Command phrase variations (words in [brackets] are optional):
- “Append Mail Link [to Note]”
Running the Script
The parameter input settings dialog. NOTE: Only one email address can be entered in each of the recipient, cc, and bcc fields. To target more addresses, create a group in Contacts and enter the group title in the created Mail message window.
The resulting mailto link is appended to the item note, and looks something like the following: (TAP|CLICK to try)
Here's the link URL:
When the link is tapped or clicked, a new Mail message will be generated using the indicated recipients and body text.
Requirements
- Application: OmniFocus 4
- System: macOS 12.3, iOS 15, iPadOS 15, visionOS 1.0
Downloads
- ⇩ COMMANDS-FILE OmniFocus 4.0 (macOS)
- ⇩ SHORTCUT-LINK (“Append Mail Link to Note”) (iOS · iPadOS · visionOS)
Voice Command Code
Here’s the Omni Automation code for the command.
Append Mail Link to Note
(async () => {
try {
function createUtterance(textToSpeak){
langCode = Speech.Voice.currentLanguageCode
voiceObj = Speech.Voice.withLanguage(langCode)
utterance = new Speech.Utterance(textToSpeak)
utterance.voice = voiceObj
utterance.rate = Speech.Utterance.defaultSpeechRate
return utterance
}
var synthesizer = new Speech.Synthesizer()
function validMailAddress(address){
return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(address)
}
var sel = document.windows[0].selection
var selCount = sel.tasks.length + sel.projects.length
if(selCount === 1){
if (sel.tasks.length === 1){
var selectedItem = sel.tasks[0]
var itemType = "Task"
} else {
var selectedItem = sel.projects[0]
var itemType = "Project"
}
} else {
throw {
name: "Selection Issue",
message: "Please select a single project or task."
}
}
var preferences = new Preferences("com.omni-automation.voice-control")
// DEFAULTS
// NOTE: subject is disabled so that the value is the title of the selected task or project
var defaultLinkTitleValue = "(Mail Link)"
var defaultRecipientValue = null
var defaultRecipient2Value = null
var defaultRecipient3Value = null
var defaultCcValue = null
var defaultBccValue = null
//var defaultSubjectValue = null
var defaultBodyValue = null
var defaultShouldPlaceOnClipboard = false
// RETRIEVE STORED FORM PARMATER VALUES
var linkTitleValue = preferences.readString("linkTitleValue")
if(!linkTitleValue){linkTitleValue = defaultLinkTitleValue}
var recipientValue = preferences.readString("recipientValue")
if(!recipientValue){recipientValue = defaultRecipientValue}
var recipient2Value = preferences.readString("recipient2Value")
if(!recipient2Value){recipient2Value = defaultRecipient2Value}
var recipient3Value = preferences.readString("recipient3Value")
if(!recipient3Value){recipient3Value = defaultRecipient3Value}
var ccValue = preferences.readString("ccValue")
if(!ccValue){ccValue = defaultCcValue}
var bccValue = preferences.readString("bccValue")
if(!bccValue){bccValue = defaultBccValue}
//var subjectValue = preferences.readString("subjectValue")
//if(!subjectValue){subjectValue = defaultSubjectValue}
var bodyValue = preferences.readString("bodyValue")
if(!bodyValue){bodyValue = defaultBodyValue}
var shouldPlaceOnClipboardValue = preferences.readBoolean("shouldPlaceOnClipboardValue")
if(!shouldPlaceOnClipboardValue){shouldPlaceOnClipboardValue = defaultShouldPlaceOnClipboard}
// CREATE THE FORM AND ELEMENTS
var mailtoForm = new Form()
var linkTitleInput = new Form.Field.String("linkTitle", "Link Title", linkTitleValue)
var recipientsInput = new Form.Field.String("recipient", "Recipient", recipientValue)
var recipientsInput2 = new Form.Field.String("recipient2", "Recipient", recipient2Value)
var recipientsInput3 = new Form.Field.String("recipient3", "Recipient", recipient3Value)
var ccInput = new Form.Field.String("cc", "Cc", ccValue)
var bccInput = new Form.Field.String("bcc", "Bcc", bccValue)
var subjectInput = new Form.Field.String("subject", "Subject", selectedItem.name)
var bodyInput = new Form.Field.String("body", "Body", bodyValue)
var clipboardInput = new Form.Field.Checkbox(
"shouldReplaceClipboard",
"Put link on clipboard",
shouldPlaceOnClipboardValue
)
mailtoForm.addField(linkTitleInput)
mailtoForm.addField(recipientsInput)
mailtoForm.addField(recipientsInput2)
mailtoForm.addField(recipientsInput3)
mailtoForm.addField(ccInput)
mailtoForm.addField(bccInput)
mailtoForm.addField(subjectInput)
mailtoForm.addField(bodyInput)
mailtoForm.addField(clipboardInput)
var formPrompt = "Mail Parameters:"
var buttonTitle = "OK"
mailtoForm.validate = function(formObject){
var recipient = formObject.values['recipient']
var recipient2 = formObject.values['recipient2']
var recipient3 = formObject.values['recipient3']
var cc = formObject.values['cc']
var bcc = formObject.values['bcc']
if(recipient){
if (!validMailAddress(recipient)){
throw "Not a valid recipient address."
}
} else {
throw "A recipient address is required."
}
if(recipient2){
if (!validMailAddress(recipient2)){
throw "Not a valid second recipient address."
}
}
if(recipient3){
if (!validMailAddress(recipient3)){
throw "Not a valid third recipient address."
}
}
if(cc){
if (!validMailAddress(cc)){
throw "Not a valid carbon copy address"
}
}
if(bcc){
if (!validMailAddress(bcc)){
throw "Not a valid blind carbon copy address"
}
}
return true
}
var formObject = await mailtoForm.show(formPrompt, buttonTitle)
// RETRIEVE VALUES
var linkTitle = formObject.values['linkTitle']
var recipient = formObject.values['recipient']
var recipient2 = formObject.values['recipient2']
var recipient3 = formObject.values['recipient3']
var cc = formObject.values['cc']
var bcc = formObject.values['bcc']
var subject = formObject.values['subject']
var body = formObject.values['body']
var shouldReplaceClipboard = formObject.values['shouldReplaceClipboard']
// CONSRUCT MAILTO: URL
var args = [];
if (cc){
args.push('cc=' + cc);
}
if (bcc){
args.push('bcc=' + bcc);
}
if (subject){
args.push('subject=' + encodeURIComponent(subject));
}
if (body){
args.push('body=' + encodeURIComponent(body).replaceAll("%5Cn","%0A"))
}
recipientArray = [recipient, recipient2, recipient3]
var recipientsString = recipientArray[0]
recipientArray.forEach((item, index) => {
if(item && index > 0){
recipientsString += "," + item
}
})
var url = 'mailto:' + recipientsString
if (args.length > 0){
url += '?' + args.join('&')
}
// CREATE THE LINK OBJECT
linkURL = URL.fromString(url)
linkTitle = (linkTitle) ? linkTitle:defaultLinkTitleValue
// APPEND TO NOTE
noteObj = selectedItem.noteText
linkObj = new Text(linkTitle, noteObj.style)
newLineObj = new Text("\n", noteObj.style)
style = linkObj.styleForRange(linkObj.range)
style.set(Style.Attribute.Link, linkURL)
if (noteObj.string.length > 0){noteObj.append(newLineObj)}
noteObj.append(linkObj)
node = document.windows[0].content.selectedNodes[0]
node.expandNote(false)
// PLACE ON CLIPBOARD
if(shouldReplaceClipboard){Pasteboard.general.string = url}
console.log(url)
// STORE THE NEW VALUES
preferences.write("linkTitleValue", linkTitle)
preferences.write("recipientValue", recipient)
preferences.write("recipient2Value", recipient2)
preferences.write("recipient3Value", recipient3)
preferences.write("ccValue", cc)
preferences.write("bccValue", bcc)
preferences.write("bodyValue", body)
preferences.write("shouldPlaceOnClipboardValue", shouldReplaceClipboard)
utterance = createUtterance("Done!")
synthesizer.speakUtterance(utterance)
}
catch(err){
if (!err.message.includes("cancelled")){
utterance = createUtterance(err.message)
synthesizer.speakUtterance(utterance)
//new Alert(err.name, err.message).show()
}
}
})();
LEGAL
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Mention of third-party websites and products is for informational purposes only and constitutes neither an endorsement nor a recommendation. OMNI-AUTOMATION.COM assumes no responsibility with regard to the selection, performance or use of information or products found at third-party websites. OMNI-AUTOMATION.COM provides this only as a convenience to our users. OMNI-AUTOMATION.COM has not tested the information found on these sites and makes no representations regarding its accuracy or reliability. There are risks inherent in the use of any information or products found on the Internet, and OMNI-AUTOMATION.COM assumes no responsibility in this regard. Please understand that a third-party site is independent from OMNI-AUTOMATION.COM and that OMNI-AUTOMATION.COM has no control over the content on that website. Please contact the vendor for additional information.