×

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 Plain Text File


(async () => { try { if(Pasteboard.general.hasStrings){ data = Data.fromString(Pasteboard.general.string) wrapper = FileWrapper.withContents('Pasteboard.txt', data) filesaver = new FileSaver() urlObj = await filesaver.show(wrapper) if(Device.current.mac){urlObj.open()} } } catch(err){ new Alert(err.name, err.message).show() } })();
Save Clipboard to RTF File
 

/*{ "type": "action", "targets": ["omnifocus","omnigraffle","omniplan","omnioutliner"], "author": "Otto Automator", "identifier": "com.omni-automation.all.clipboard-to-rtf-file", "version": "1.0", "description": "Plug-in will save the RTF content on the clipboard to file.", "label": "Clipboard to RTF File", "shortLabel": "Clipboard to RTF File", "paletteLabel": "Clipboard to RTF File", "image": "doc.richtext" }*/ (() => { const action = new PlugIn.Action(async function(selection, sender){ try { data = Pasteboard.general.dataForType(TypeIdentifier.rtf) wrapper = FileWrapper.withContents('clipboard-export.rtf', data) urlObj = await new FileSaver().show(wrapper) if(Device.current.mac){ alertTitle = "File Created" alertMsg = "Open the file?" alert = new Alert(alertTitle, alertMsg) alert.addOption("Open") alert.addOption("Skip") alert.show(function(result){ if (result == 0){urlObj.open()} }) } } catch(err){ if(!err.causedByUserCancelling){ console.error(err.name, err.message) new Alert(err.name, err.message).show() } } }); action.validate = function(selection, sender){ return (Pasteboard.general.hasStrings) }; return action; })();