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


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.2", "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){ if(!err.causedByUserCancelling){ new Alert(err.name, err.message).show() } } }); 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-iCloud", "version": "1.9", "description": "Uses a stored boomark to save the text content of the clipboard to the current application’s iCloud folder (if it exists). To remove the stored Bookmark, hold down the Control key when selecting the plug-in from the Automation menu.", "label": "Clipboard to App iCloud Folder", "shortLabel": "Save to App iCloud", "paletteLabel": "Save to App iCloud", "image": "icloud.and.arrow.up.fill" }*/ (() => { var credentials = new Credentials() var serviceTitle = `${app.name} iCloud Folder` console.log(serviceTitle) const action = new PlugIn.Action(async function(selection, sender){ try { // TO REMOVE BOOKMARK HOLD DOWN CONTROL KEY WHEN SELECTING PLUG-IN if (app.controlKeyDown){ alertMessage = "Remove the stored bookmark?" alert = new Alert("Confirmation Required", alertMessage) alert.addOption("Remove") alert.addOption("Cancel") buttonIndex = await alert.show() if (buttonIndex === 0){credentials.remove(serviceTitle)} throw new Error("Bookmark removed") } // RETRIEVE BOOKMARK var bookmark = credentials.readBookmark(serviceTitle) // IF NO BOOKMARK, ASK FOR FOLDER if(!bookmark){ picker = new FilePicker() picker.folders = true picker.multiple = false if (Device.current.mac){ picker.message = `Please select the ${app.name} iCloud folder:` } else { alertTitle = "Select Folder" alertMessage = `Close this alert and in the forthcoming picker, select ${serviceTitle}.` await new Alert(alertTitle, alertMessage).show() } chosenDirectoryURLs = await picker.show() bookmark = URL.Bookmark.fromURL(chosenDirectoryURLs[0]) credentials.writeBookmark(serviceTitle, bookmark) bookmark = credentials.readBookmark(serviceTitle) } 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 ${serviceTitle}` 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"] } // RETRIEVE BOOKMARK bookmark = credentials.readBookmark(serviceTitle) // IF NO BOOKMARK, ASK FOR FOLDER if(!bookmark){ picker = new FilePicker() picker.folders = true picker.multiple = false if (Device.current.mac){ picker.message = `Please select the ${app.name} iCloud folder:` } else { alertTitle = "Select Folder" alertMessage = `Close this alert and in the forthcoming picker, select ${serviceTitle}.` await new Alert(alertTitle, alertMessage).show() } chosenDirectoryURLs = await picker.show() bookmark = URL.Bookmark.fromURL(chosenDirectoryURLs[0]) credentials.writeBookmark(serviceTitle, bookmark) bookmark = credentials.readBookmark(serviceTitle) } // RETRIEVE ACCESS TOKEN AND PERFORM ACTION WITH FOLDER token = await bookmark.access() iCloudFldrURL = token.url fileURL = iCloudFldrURL.appendingPathComponent(fileName) data = Data.fromString(Pasteboard.general.string) wrapper = FileWrapper.withContents(fileName, data) wrapper.write(fileURL, [FileWrapper.WritingOptions.Atomic], null) if (Device.current.mac){ // ON macOS OPEN THE FILE if(shouldOpenFile){fileURL.open()} } } catch(err){ if(!err.causedByUserCancelling){ new Alert(err.name, err.message).show() } } }); action.validate = function(selection, sender){ return (Pasteboard.general.hasStrings) }; return action; })();
Action form on iPadOS