×

Data

A generic bag of bytes. Mainly useful to be interpreted / converted to some other type.

Instance Properties

Instance Functions

Functions for converting instances of the Data class into instances of the String class:

Class Functions

Functions for converting instances of the String class into instances of the Data class:

String to Base64 Encoded String


var encodedString = Data.fromString("How Now Brown Cow").toBase64() //--> "SG93IE5vdyBCcm93biBDb3c="

Examples

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.

In this example, the processed Data object is the extracted value of the contents property of the created FileWrapper instance (line 5). Note that the toBase64() function of the Data class to convert the contents of the file wrapper into a format that can be used in a URL.

omnioutliner://localhost/omnijs-run?script=try%7Bvar%20taskName%20%3D%20document%2Ename%0Avar%20wrapperPromise%20%3D%20document%2EmakeFileWrapper%28taskName%29%0A%0AwrapperPromise%2Ethen%28function%28wrapper%29%7B%0A%09var%20attachmentName%20%3D%20encodeURIComponent%28wrapper%2EpreferredFilename%29%0A%09var%20encodedData%20%3D%20wrapper%2Econtents%2EtoBase64%28%29%0A%09taskName%20%3D%20encodeURIComponent%28taskName%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%28err%20%3D%3E%20%7B%0A%09console%2Elog%28err%2Emessage%29%0A%7D%29%7Dcatch%28err%29%7Bconsole%2Elog%28err%29%7D
New OmniFocus Task with Outline Attachment
 

var taskName = document.name var wrapperPromise = document.makeFileWrapper(taskName) wrapperPromise.then(function(wrapper){ var attachmentName = encodeURIComponent(wrapper.preferredFilename) var encodedData = wrapper.contents.toBase64() taskName = encodeURIComponent(taskName) urlStr = "omnifocus://localhost/add?name=" + taskName + "&attachment=" + encodedData + "&attachment-name=" + attachmentName URL.fromString(urlStr).open() }) wrapperPromise.catch(err => { console.log(err.message) })

Here’s an example script that exports the current text on the Pasteboard (clipboard) to a file. The example uses the fromString() of the Data class to convert the passed text to a data object that can be written to file.

NOTE: Since the Pasteboard class is a class shared by all Omni applications, this script will work in any Omni application:

Save the Clipboard Text to File


if(Pasteboard.general.hasStrings){ var data = Data.fromString(Pasteboard.general.string) var wrapper = FileWrapper.withContents('Pasteboard.txt', data) var filesaver = new FileSaver() var fileSaverPromise = filesaver.show(wrapper) fileSaverPromise.then(urlObj => { console.log(urlObj.string) urlObj.open() }) fileSaverPromise.catch(err => { console.error(err.message) }) }