New OmniFocus Task with Document Attachment

Those who rely upon computing devices firmly believe that data must be able to be quickly shared between applications and devices. Fortunately, Omni Automation provides a user-enabled mechanism to conveniently move information between apps, such as creating a new OmniFocus task containing the current OmniOutliner document as an attachment.

Here’s an Omni Automation script for creating a new task in OmniFocus with the current OmniOutliner document as an attachment. The script uses the integrated URL support in OmniFocus to create and run a URL link for creating tasks:

wrapperPromise = document.makeFileWrapper(document.name) wrapperPromise.then(function(wrapper){ taskName = encodeURIComponent(document.name) attachmentName = encodeURIComponent(wrapper.preferredFilename) encodedData = wrapper.contents.toBase64() urlStr = "omnifocus://localhost/add?name=" + taskName + "&attachment=" + encodedData + "&attachment-name=" + attachmentName URL.fromString(urlStr).open() }) wrapperPromise.catch(function(err){ console.error(err.message) })
omnioutliner://localhost/omnijs-run?script=try%7BwrapperPromise%20%3D%20document%2EmakeFileWrapper%28document%2Ename%29%0A%0AwrapperPromise%2Ethen%28function%28wrapper%29%7B%0A%09taskName%20%3D%20encodeURIComponent%28document%2Ename%29%0A%09attachmentName%20%3D%20encodeURIComponent%28wrapper%2EpreferredFilename%29%0A%09encodedData%20%3D%20wrapper%2Econtents%2EtoBase64%28%29%0A%09urlStr%20%3D%20%22omnifocus%3A%2F%2Flocalhost%2Fadd%3Fname%3D%22%20%2B%20taskName%20%2B%20%22%26attachment%3D%22%20%2B%20encodedData%20%2B%20%22%26attachment%2Dname%3D%22%20%2B%20attachmentName%0A%09URL%2EfromString%28urlStr%29%2Eopen%28%29%0A%7D%29%0A%0AwrapperPromise%2Ecatch%28function%28err%29%7B%0A%09console%2Eerror%28err%2Emessage%29%0A%7D%29%7Dcatch%28err%29%7Bconsole%2Elog%28err%29%7D

 1  Use the makeFileWrapper(…) function of the Document class to generate a file wrapper, passing-in the name of the document to be used as the name of the export file. Since no export file type has been specified as a second parameter, the default export file format will be used. The result of the function is an instance of a JavaScript promise.

 03-09  The then(…) function of the Promise class is used to process the file wrapper once it has been created, and is passed into the function.

 04  Encode the document name to be used as the name of the task.

 05  Encode the document file name to be used as the attachment name.

 06  Encode the contents of the document in Base64 encoding format, which results in a string of URL approved characters.

 07  Combine the encoded elements into the URL format supported by OmniFocus.

 08  Convert the URL string into a URL object and execute it using the call() method.

Solitary Action PlugIn

And with a slight modification, the previous script can be turned into an Omni Automation action that you can install on your macOS and iOS devices:

/*{ "type": "action", "targets": ["omnioutliner"], "author": "Otto Automator", "identifier": "com.omni-automation.oo-task-with-document", "version": "1.0", "description": "This action will create a new OmniFocus task with the current OmniOutliner document as an attachment.", "label": "Attach Document to New Task", "shortLabel": "Task with Doc" }*/ (() => { var action = new PlugIn.Action(function(selection, sender){ // action code wrapperPromise = document.makeFileWrapper(document.name) wrapperPromise.then(function(wrapper){ taskName = encodeURIComponent(document.name) attachmentName = encodeURIComponent(wrapper.preferredFilename) encodedData = wrapper.contents.toBase64() urlStr = "omnifocus://localhost/add?name=" + taskName + "&attachment=" + encodedData + "&attachment-name=" + attachmentName URL.fromString(urlStr).open() }) wrapperPromise.catch(function(err){ console.error(err.message) }) }); action.validate = function(selection, sender){ // validation code return true }; return action; })();
UNDER CONSTRUCTION

This webpage is in the process of being developed. Any content may change and may not be accurate or complete at this time.

DISCLAIMER