FileSaver

An instance of the FileSaver class enables the user to save one or more instances of the FileWrapper class to disk using the system-provided file picking interface.

Properties

Here are the properties of the FileSaver class.

Constructors

The mechanism for creating a new instance of the FileSaver class.

Instance Functions

The functions that can be executed on an instance of the FileSaver class.

fileSaver-macOS-dialog

Saving/Exporting a Default Document Type

The following Omni Automation script demonstrates how to save an instance of the default document type to file. Similar procedures are used by all Omni applications to export data and documents to file.

wrapperPromise = document.makeFileWrapper("Default Name") wrapperPromise.then(function(wrapper){ filesaver = new FileSaver() fileSaverPromise = filesaver.show(wrapper) fileSaverPromise.then(function(urlObj){ console.log(urlObj.string) new Alert("FILE URL", urlObj.string).show() }) fileSaverPromise.catch(function(err){ console.log(err.message) }) }).catch(function(err){console.log(err.message)})
omniplan://localhost/omnijs-run?script=try%7BwrapperPromise%20%3D%20document%2EmakeFileWrapper%28%22Default%20Name%22%29%0A%0AwrapperPromise%2Ethen%28function%28wrapper%29%7B%0A%09filesaver%20%3D%20new%20FileSaver%28%29%0A%09fileSaverPromise%20%3D%20filesaver%2Eshow%28wrapper%29%0A%0A%09fileSaverPromise%2Ethen%28function%28urlObj%29%7B%0A%09%09console%2Elog%28urlObj%2Estring%29%0A%09%09new%20Alert%28%22FILE%20URL%22%2C%20urlObj%2Estring%29%2Eshow%28%29%0A%09%7D%29%0A%0A%09fileSaverPromise%2Ecatch%28function%28err%29%7B%0A%09%09console%2Elog%28err%2Emessage%29%0A%09%7D%29%0A%7D%29%2Ecatch%28function%28err%29%7Bconsole%2Elog%28err%2Emessage%29%7D%29%7Dcatch%28err%29%7Bconsole%2Elog%28err%29%7D
omnigraffle://localhost/omnijs-run?script=try%7BwrapperPromise%20%3D%20document%2EmakeFileWrapper%28%22Default%20Name%22%29%0A%0AwrapperPromise%2Ethen%28function%28wrapper%29%7B%0A%09filesaver%20%3D%20new%20FileSaver%28%29%0A%09fileSaverPromise%20%3D%20filesaver%2Eshow%28wrapper%29%0A%0A%09fileSaverPromise%2Ethen%28function%28urlObj%29%7B%0A%09%09console%2Elog%28urlObj%2Estring%29%0A%09%09new%20Alert%28%22FILE%20URL%22%2C%20urlObj%2Estring%29%2Eshow%28%29%0A%09%7D%29%0A%0A%09fileSaverPromise%2Ecatch%28function%28err%29%7B%0A%09%09console%2Elog%28err%2Emessage%29%0A%09%7D%29%0A%7D%29%2Ecatch%28function%28err%29%7Bconsole%2Elog%28err%2Emessage%29%7D%29%7Dcatch%28err%29%7Bconsole%2Elog%28err%29%7D
omnioutliner://localhost/omnijs-run?script=try%7BwrapperPromise%20%3D%20document%2EmakeFileWrapper%28%22Default%20Name%22%29%0A%0AwrapperPromise%2Ethen%28function%28wrapper%29%7B%0A%09filesaver%20%3D%20new%20FileSaver%28%29%0A%09fileSaverPromise%20%3D%20filesaver%2Eshow%28wrapper%29%0A%0A%09fileSaverPromise%2Ethen%28function%28urlObj%29%7B%0A%09%09console%2Elog%28urlObj%2Estring%29%0A%09%09new%20Alert%28%22FILE%20URL%22%2C%20urlObj%2Estring%29%2Eshow%28%29%0A%09%7D%29%0A%0A%09fileSaverPromise%2Ecatch%28function%28err%29%7B%0A%09%09console%2Elog%28err%2Emessage%29%0A%09%7D%29%0A%7D%29%2Ecatch%28function%28err%29%7Bconsole%2Elog%28err%2Emessage%29%7D%29%7Dcatch%28err%29%7Bconsole%2Elog%28err%29%7D
omnifocus://localhost/omnijs-run?script=try%7BwrapperPromise%20%3D%20document%2EmakeFileWrapper%28%22Default%20Name%22%29%0A%0AwrapperPromise%2Ethen%28function%28wrapper%29%7B%0A%09filesaver%20%3D%20new%20FileSaver%28%29%0A%09fileSaverPromise%20%3D%20filesaver%2Eshow%28wrapper%29%0A%0A%09fileSaverPromise%2Ethen%28function%28urlObj%29%7B%0A%09%09console%2Elog%28urlObj%2Estring%29%0A%09%09new%20Alert%28%22FILE%20URL%22%2C%20urlObj%2Estring%29%2Eshow%28%29%0A%09%7D%29%0A%0A%09fileSaverPromise%2Ecatch%28function%28err%29%7B%0A%09%09console%2Elog%28err%2Emessage%29%0A%09%7D%29%0A%7D%29%2Ecatch%28function%28err%29%7Bconsole%2Elog%28err%2Emessage%29%7D%29%7Dcatch%28err%29%7Bconsole%2Elog%28err%29%7D

Saving/Exporting a Specific Document Type

The following Omni Automation script demonstrates how to save an OmniPlan document to file. Similar procedures are used by all Omni applications to export data and documents to file.

Detailed documentation regarding the FileType class, and its use to generate application-specific FileWrappers, can be found in the FileType topic.

var fileTypeID = 'com.omnigroup.omniplan2.planfile' defaultName = "Backup" + "-" + new Date().toString().split(" ").slice(0,4).join("-") wrapperPromise = document.makeFileWrapper(defaultName, fileTypeID) wrapperPromise.then(function(wrapper){ filesaver = new FileSaver() filesaver.message = "Choose the location for the saved file:" // macOS filesaver.nameLabel = "Name:" // macOS filesaver.prompt = "Continue" // macOS filetype = new FileType(fileTypeID) filesaver.types = [filetype] fileSaverPromise = filesaver.show(wrapper) fileSaverPromise.then(function(urlObj){ console.log(urlObj.string) }) fileSaverPromise.catch(function(err){ console.log(err.message) }) }).catch(function(error){ console.error(error.message) })
omniplan://localhost/omnijs-run?script=try%7Bvar%20fileTypeID%20%3D%20%27com%2Eomnigroup%2Eomniplan2%2Eplanfile%27%0AdefaultName%20%3D%20%22Backup%22%20%2B%20%22%2D%22%20%2B%20new%20Date%28%29%2EtoString%28%29%2Esplit%28%22%20%22%29%2Eslice%280%2C4%29%2Ejoin%28%22%2D%22%29%0AwrapperPromise%20%3D%20document%2EmakeFileWrapper%28defaultName%2C%20fileTypeID%29%0A%0AwrapperPromise%2Ethen%28function%28wrapper%29%7B%0A%09filesaver%20%3D%20new%20FileSaver%28%29%0A%09filesaver%2Emessage%20%3D%20%22Choose%20the%20location%20for%20the%20saved%20file%3A%22%20%2F%2F%20macOS%0A%09filesaver%2EnameLabel%20%3D%20%22Name%3A%22%20%2F%2F%20macOS%0A%09filesaver%2Eprompt%20%3D%20%22Continue%22%20%2F%2F%20macOS%0A%09filetype%20%3D%20new%20FileType%28fileTypeID%29%0A%09filesaver%2Etypes%20%3D%20%5Bfiletype%5D%0A%09fileSaverPromise%20%3D%20filesaver%2Eshow%28wrapper%29%0A%0A%09fileSaverPromise%2Ethen%28function%28urlObj%29%7B%0A%09%09console%2Elog%28urlObj%2Estring%29%0A%09%7D%29%0A%0A%09fileSaverPromise%2Ecatch%28function%28err%29%7B%0A%09%09console%2Elog%28err%2Emessage%29%0A%09%7D%29%0A%7D%29%2Ecatch%28function%28error%29%7B%0A%09console%2Eerror%28error%2Emessage%29%0A%7D%29%7Dcatch%28err%29%7Bconsole%2Elog%28err%29%7D

 01  The FileType identifier string of the file type to be saved or exported to file.

 02  In this example, the default name for the saved file will include the current date (ie: Backup Sun-Jun-23-2019). The JavaScript Date class is used to generate a full date string, which is then segmented, sliced, and rejoined to created a dash-delimited date string.

 03  A file wrapper is created using the makeFileWrapper(…) function, which is passed the stored file type identifier and the default file name as parameters. The function returns an instance of the JavaScript Promise class, which when resolved will provide the created file wrapper instance. The generated promise is stored in the variable: wrapperPromise

 05-23  The stored promise is resolved by calling the then(…) function on the stored instance. If the wrapper creation is successful, the method’s call-back function will be passed a reference to the created file wrapper.

 06  With the file wrapper created, the script can now display the file saver dialog to the user so they may provide the file name and saving location. A new instance of the FileSaver class is created and stored in the variable: filesaver

 07-09  On macOS, file saver dialogs can be customized to the include a user prompt and an approval button title. These FileSaver properties will be ignored should the script be executed on a device running iOS or iPadOS, both of which do not offer the ability to customize saving dialogs.

 10  Create and store an instance of the FileType class using the previously stored file type identifier.

 11  The value of the types property of the FileSaver class is an array of file types to be exported. In this example the property value will be an array containing a single file type instance.

 12  The saving dialog is displayed by calling the show(…) function and passing in the stored file wrapper instance as the parameter. The result of the function call is a JavaScript Promise instance, which is stored in the variable: fileSaverPromise

 14-16  When the user completes the saver dialog, the stored fileSaverPromise will be resolved by calling the then(…) function will automatically be passed a URL object referencing the saved file. The passed url can be used to perform any follow-up processing with the created file.

 18-20  Should the user cancel the saving dialog or should a problem occur during the processing of writing the file, the defining error will be passed into the catch(…) function, allowing the script to deal with the issue.

 21-23  The catch(…) is appended to the wrapper promise resolution call, so that any problem in creating the file wrapper can be dealt with by the script.

Multiple Exports

In an application such as OmniGraffle, the need to export multiple instances of a canvas in various formats, in not an unusal occurence. The FileWrapper class includes a function for creating a folder wrapper that contains other file wrappers as its contents. The folder wrapper is passed to the FileSaver instance and when executed, a new directory containing the exported files is created on disk.

In the following script example, the current OmniGraffle canvas is exported as both an image in PNG format, and as a PDF file, both placed within a created directory.

cnvsName = document.windows[0].selection.canvas.name // Create file types and wrappers PNGtypeID = "public.png" PDFtypeID = "com.adobe.pdf" PNGwrapper = document.fileWrapper(PNGtypeID) PNGwrapper.preferredFilename = cnvsName + ".png" PNGfileType = FileType.png PDFwrapper = document.fileWrapper(PDFtypeID) PDFwrapper.preferredFilename = cnvsName + ".pdf" PDFfileType = FileType.pdf types = [PNGwrapper, PDFwrapper] fldrwrapper = FileWrapper.withChildren("Export Folder",types) folderType = new FileType("public.folder") // Create and Show File Saver filesaver = new FileSaver() filesaver.message = "Choose the location for the export folder:" // macOS filesaver.nameLabel = "Name:" // macOS filesaver.prompt = "Continue" // macOS filesaver.types = [folderType,PNGfileType,PDFfileType] fileSaverPromise = filesaver.show(fldrwrapper) // Process File Saver Result fileSaverPromise.then(function(urlObj){ console.log(urlObj.string) }) fileSaverPromise.catch(function(err){ console.log(err.message) })
// EXPORT CANVAS TO TWO FILES IN FOLDER cnvsName = document.windows[0].selection.canvas.name PNGwrapperPromise = document.makeFileWrapper(cnvsName + ".png", "public.png") PDFwrapperPromise = document.makeFileWrapper(cnvsName + ".pdf", "com.adobe.pdf") promises = [PNGwrapperPromise, PDFwrapperPromise] Promise.all(promises).then(function(fileWrappers){ // Create and Show File Saver filesaver = new FileSaver() folderType = new FileType("public.folder") filesaver.types = [folderType, FileType.png, FileType.pdf] fldrwrapper = FileWrapper.withChildren("Export Folder", fileWrappers) fileSaverPromise = filesaver.show(fldrwrapper) // Process File Saver Result fileSaverPromise.then(function(urlObj){ console.log(urlObj.string) }) fileSaverPromise.catch(function(err){ console.log(err.message) }) }).catch(function(error){ console.log("Error", error.message) })
omnigraffle://localhost/omnijs-run?script=try%7BcnvsName%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Ecanvas%2Ename%0A%0APNGwrapperPromise%20%3D%20document%2EmakeFileWrapper%28cnvsName%20%2B%20%22%2Epng%22%2C%20%22public%2Epng%22%29%0APDFwrapperPromise%20%3D%20document%2EmakeFileWrapper%28cnvsName%20%2B%20%22%2Epdf%22%2C%20%22com%2Eadobe%2Epdf%22%29%0Apromises%20%3D%20%5BPNGwrapperPromise%2C%20PDFwrapperPromise%5D%0A%0APromise%2Eall%28promises%29%2Ethen%28function%28fileWrappers%29%7B%0A%0A%09%2F%2F%20Create%20and%20Show%20File%20Saver%0A%09filesaver%20%3D%20new%20FileSaver%28%29%0A%09folderType%20%3D%20new%20FileType%28%22public%2Efolder%22%29%0A%09filesaver%2Etypes%20%3D%20%5BfolderType%2C%20FileType%2Epng%2C%20FileType%2Epdf%5D%0A%09fldrwrapper%20%3D%20FileWrapper%2EwithChildren%28%22Export%20Folder%22%2C%20fileWrappers%29%0A%09fileSaverPromise%20%3D%20filesaver%2Eshow%28fldrwrapper%29%0A%0A%09%2F%2F%20Process%20File%20Saver%20Result%0A%09fileSaverPromise%2Ethen%28function%28urlObj%29%7B%0A%09%09console%2Elog%28urlObj%2Estring%29%0A%09%7D%29%0A%0A%09fileSaverPromise%2Ecatch%28function%28err%29%7B%0A%09%09console%2Elog%28err%2Emessage%29%0A%09%7D%29%0A%09%0A%7D%29%2Ecatch%28function%28error%29%7B%0A%09console%2Elog%28%22Error%22%2C%20error%2Emessage%29%0A%7D%29%7Dcatch%28err%29%7Bconsole%2Elog%28err%29%7D

 02  Store the name of the canvas to be exported in the variable: cnvsName

 04  Use the makeFileWrapper(…) function of the Document class to make a new PNG image file wrapper using the canvas name for the file name. The result of the function is a JavaScript promise which is stored in the variable: PNGPromise

 05  Use the makeFileWrapper(…) function of the Document class to make a new PDF file wrapper using the canvas name for the file name. The result of the function is a JavaScript promise which is stored in the variable: PDFPromise

 06  Store an array of the generated promises in the variable: promises

 08-26  Use the all(…) function of the Promise class to execute once all the passed-in promises have become ready to execute. The then(…) function of the Promise class is appended to the all(…) function in order to process the results of the passed-in promises, which in this example will be file wrappers for the PNG and PDF files to be created, which will be passed as input to the call-back function.

 11  Create and store a new instance of the FileSaver class.

 12  Create and store a new instance of the FileType class that will represent the folder used to contain the exported files.

 13  Set the value of the types property of the FileSaver instance to an array of file types, beginning with the file type for the folder.

 14  Use the withChildren(…) function of the FileWrapper class to create an instance of a file wrapper representing the export folder. The name of the folder, and the file wrappers passed as input to the function, are used as the parameters for the withChildren(…) function.

 15  Use the show(…) function of the FileSaver class to display the system saver dialog. The previously created file wrapper for the export folder is used as the parameter value for this function. The result will be an instance of the JavaScript Promise class, which will be stored in the variable: fileSaverPromise

 18-20  The then(…) function of the Promise class is used to process the stored promise instance, with the resulting URL to the created folder being passed into the processing function.

Exporting Text to File

Here’s an example OmniGraffle action plug-in that will save the text contents of the selected shape to a file on disk:

/*{ "type": "action", "targets": ["omnigraffle"], "author": "Otto Automator", "identifier": "com.omni-automation.og.shape-text-to-file", "version": "1.0", "description": "This action will save the text contents of the selected shape to a file on disk.", "label": "Save Shape Text to File", "shortLabel": "Text to File" }*/ var _ = function(){ var action = new PlugIn.Action(function(selection, sender){ // action code // selection options: canvas, document, graphics, lines, solids, view shape = selection.solids[0] shapeText = shape.text textData = Data.fromString(shapeText wrapper = FileWrapper.withContents('Shape Text.txt', textData) filesaver = new FileSaver() fileSaverPromise = filesaver.show(wrapper) fileSaverPromise.then(function(urlObj){ console.log(urlObj.string) }) fileSaverPromise.catch(function(err){ console.log(err.message) }) }); action.validate = function(selection, sender){ // validation code // selection options: canvas, document, graphics, lines, solids, view if (selection.solids.length === 1){ return (selection.solids[0].text != "") ? true:false } return false }; return action; }(); _;

 16  Identify the selected shape.

 17  Get the text from the shape.

 18  Use the fromString(…) function of the Data class to convert the extracted shape text into a data object.

 19  Use the withContents(…) function of the FileWrapper class to create a file wrapper instance, using the data object and the file name (with file extension) for the new text file as the parameters of the function.

 20  Create a new instance of the FileSaver class.

 21  Use the show(…) function of the FileSaver class to display the system file saver dialog. The result of this is a JavaScript promise object.

FIlesaver iOS FIlesaver iOS FIlesaver iOS
/*{ "type": "action", "targets": ["omnigraffle","omniplan","omnioutliner","omnifocus"], "author": "Otto Automator", "identifier": "com.omni-automation.export-for-shortcuts", "version": "1.0", "description": "This action will export the document in the default format and pass a file URL to the exported file, to a Shortcuts workflow as input.", "label": "Export for Shortcuts", "shortLabel": "Export for Shortcuts" }*/ var _ = function(){ var action = new PlugIn.Action(function(selection, sender){ // Begin file wrapper creation wrapperPromise = document.makeFileWrapper("Default Name") // Process created file wrapper wrapperPromise.then(function(wrapper){ // Create and Show File Saver filesaver = new FileSaver() fileSaverPromise = filesaver.show(wrapper) // Process file saver result fileSaverPromise.then(function(urlObj){ encodedFileURLString = encodeURIComponent(urlObj.string) // change name to match title of targeted Shortcuts workflow workflowName = "Workflow Title" workflowName = encodeURIComponent(workflowName) callingURLString = "shortcuts://run-workflow?name=" + workflowName + "&input=text&text=" + encodedFileURLString console.log(callingURLString) URL.fromString(callingURLString).call(function(reply){ console.log(reply) }) }) fileSaverPromise.catch(function(err){ console.log(err.message) }) }) }); action.validate = function(selection, sender){ return true }; return action; }(); _;
UNDER CONSTRUCTION

This webpage is in the process of being developed. Any content may change and may not be accurate or complete at this time.

DISCLAIMER