×

FileWrapper

A FileWrapper is a representation of a node (a file, directory, or symbolic link) in the file system.

On a document-based app the FileWrapper acts as "middleware" between the file system and the actual models in an application, similar to a JSON API that is retrieved from a data source.

FileWrapper can handle single files or packages, which is a directory with files treated as a single file by the system, these packages are usually custom file formats created by the application. For example, OmniOutliner have a .ooutline and OmniGraffle a .graffle extension for their documents.

Class Functions

Read Text File to Clipboard


var picker = new FilePicker() picker.folders = false picker.multiple = false taskpaperType = new TypeIdentifier("com.taskpaper.text") picker.types = [taskpaperType, TypeIdentifier.plainText] picker.show().then(urls => { url = urls[0] wrapper = FileWrapper.fromURL(url) Pasteboard.general.string = wrapper.contents.toString() })
Wrapper from Text
 

fileName = "Cow.txt" textData = Data.fromString("How now brown cow") wrapper = FileWrapper.withContents(fileName, textData)

Instance Functions

The following documentation contains two plug-ins that incorporate the write(…) function to write text to disk without using saving dialogs.

 

Save Clipboard to Omni App’s Documents Folder (@)

NOTE: the following Omni Automation plug-in uses the documentsDirectory property of the URL class to write a file to the host Omni application’s local Documents folder (the directory displayed with the Omni app’s icon).

Save Clipboard Text to Omni App’s Documents folder
 

/*{ "type": "action", "targets": ["omnifocus", "omnigraffle", "omniplan", "omnioutliner"], "author": "Otto Automator", "identifier": "com.omni-automation.all.write-clipboard-to-app-documents", "version": "1.1", "description": "Writes the text content of the clipboard to a file in the Omni app’s local Documents folder", "label": "Clipboard to App Documents Folder", "shortLabel": "Save to App Documents", "paletteLabel": "Save to App Documents", "image": "doc.on.clipboard.fill" }*/ (() => { var action = new PlugIn.Action(async function(selection, sender){ try { inputForm = new Form() fileNameField = new Form.Field.String( "fileBaseName", "File Name", null, null ) inputForm.addField(fileNameField) fileTypes = ["Text (plain)", "TaskPaper", "HTML", "JavaScript", "SVG"] nameExtensions = ["txt", "taskpaper", "html", "js", "svg"] savingTypeMenu = new Form.Field.Option( "nameExtension", "Text File Type", nameExtensions, fileTypes, nameExtensions[0] ) inputForm.addField(savingTypeMenu) if (Device.current.mac){ shouldOpenFileField = new Form.Field.Checkbox( "shouldOpenFile", "Open file in default application", null ) inputForm.addField(shouldOpenFileField) } inputForm.validate = function(formObject){ textValue = formObject.values['fileBaseName'] return (textValue && textValue.length > 0) ? true:false } formPrompt = `Clipboard to ${app.name} Documents` buttonTitle = "Continue" formObject = await inputForm.show(formPrompt, buttonTitle) fileBaseName = formObject.values["fileBaseName"] nameExtension = formObject.values["nameExtension"] fileName = fileBaseName + "." + nameExtension if (Device.current.mac){ var shouldOpenFile = formObject.values["shouldOpenFile"] } folderURL = URL.documentsDirectory fileURL = folderURL.appendingPathComponent(fileName) textData = Data.fromString(Pasteboard.general.string, StringEncoding.UTF8) wrapper = FileWrapper.withContents(fileName, textData) wrapper.write(fileURL, [FileWrapper.WritingOptions.Atomic], null) if (Device.current.mac){ if(shouldOpenFile){fileURL.open()} } } catch(err){console.error(err.message)} }); action.validate = function(selection, sender){ return (Pasteboard.general.hasStrings) }; return action; })();

Instance Properties

FileWrapper.Type Class Properties

FileWrapper.ReadingOptions Class Properties

FileWrapper.WritingOptions Class Properties

 

Save Clipboard Text to Omni App’s iCloud Folder (@)︎

Here's a plug-in that draws upon the APIs of the Credentials, Bookmark, Pasteboard, and FileWrapper classes to save the current clipboard text to the host Omni application’s iCloud folder without requiring subsequent use of pickers. Text data may be saved in either Plain Text, HTML, TaskPaper, or JavaScript file formats.

The plug-in saves a bookmark to the host application’s iCloud folder selected by the user. That stored bookmark is then used with each subsequent running of the plug-in to save the text contents of the clipboard to the iCloud folder in either: plain text, HTML, TaskPaper, or JavaScript formats.

To reset the stored bookmark, hold down the Control key when selecting the plug-in from the Automation menu.

Use cases:

TIP: This plug-in works with iCloud folders or any folder that requires user-approval for access.

Save Options/Input Dialog
Save Clipboard Text to Omni  App’s iCloud Folder
 

/*{ "type": "action", "targets": ["omnifocus", "omnigraffle", "omniplan", "omnioutliner"], "author": "Otto Automator", "identifier": "com.omni-automation.all.write-clipboard-to-app-documents", "version": "1.1", "description": "Writes the text content of the clipboard to a file in the Omni app’s local Documents folder", "label": "Clipboard to App Documents Folder", "shortLabel": "Save to App Documents", "paletteLabel": "Save to App Documents", "image": "doc.on.clipboard.fill" }*/ (() => { var action = new PlugIn.Action(async function(selection, sender){ try { inputForm = new Form() fileNameField = new Form.Field.String( "fileBaseName", "File Name", null, null ) inputForm.addField(fileNameField) fileTypes = ["Text (plain)", "TaskPaper", "HTML", "JavaScript", "SVG"] nameExtensions = ["txt", "taskpaper", "html", "js", "svg"] savingTypeMenu = new Form.Field.Option( "nameExtension", "Text File Type", nameExtensions, fileTypes, nameExtensions[0] ) inputForm.addField(savingTypeMenu) if (Device.current.mac){ shouldOpenFileField = new Form.Field.Checkbox( "shouldOpenFile", "Open file in default application", null ) inputForm.addField(shouldOpenFileField) } inputForm.validate = function(formObject){ textValue = formObject.values['fileBaseName'] return (textValue && textValue.length > 0) ? true:false } formPrompt = `Clipboard to ${app.name} Documents` buttonTitle = "Continue" formObject = await inputForm.show(formPrompt, buttonTitle) fileBaseName = formObject.values["fileBaseName"] nameExtension = formObject.values["nameExtension"] fileName = fileBaseName + "." + nameExtension if (Device.current.mac){ var shouldOpenFile = formObject.values["shouldOpenFile"] } folderURL = URL.documentsDirectory fileURL = folderURL.appendingPathComponent(fileName) textData = Data.fromString(Pasteboard.general.string, StringEncoding.UTF8) wrapper = FileWrapper.withContents(fileName, textData) wrapper.write(fileURL, [FileWrapper.WritingOptions.Atomic], null) if (Device.current.mac){ if(shouldOpenFile){fileURL.open()} } } catch(err){console.error(err.message)} }); action.validate = function(selection, sender){ return (Pasteboard.general.hasStrings) }; return action; })();
Action form on iPadOS