Data
A generic bag of bytes. Mainly useful to be interpreted / converted to some other type.
Instance Properties
length (Number or nil r/o) • Number of bytes in this data.
Instance Functions
Functions for converting instances of the Data class into instances of the String class:
toString() → (String) • Convert to a String instance, assuming that this Data is encoded as UTF8 Unicode.
toBase64() → (String) • Convert to a Base–64 encoded string.
Class Functions
Functions for converting instances of the String class into instances of the Data class:
fromString(string:String) → (Data) • Convert from a String to the UTF8 encoding of that string as Data.
fromBase64(string:String) → (Data) • No documentation available.
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.
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)
})
}