×

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.

omniplan://localhost/omnijs-run?script=var%20wrapperPromise%20%3D%20document%2EmakeFileWrapper%28%22Default%20Name%22%29%0A%0AwrapperPromise%2Ethen%28wrapper%20%3D%3E%20%7B%0A%09filesaver%20%3D%20new%20FileSaver%28%29%0A%09fileSaverPromise%20%3D%20filesaver%2Eshow%28wrapper%29%0A%0A%09fileSaverPromise%2Ethen%28urlObj%20%3D%3E%20%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%28err%20%3D%3E%20%7B%0A%09%09console%2Elog%28err%2Emessage%29%0A%09%7D%29%0A%7D%29%0A%0AwrapperPromise%2Ecatch%28err%20%3D%3E%20%7B%0A%09console%2Elog%28err%2Emessage%29%0A%7D%29
omnigraffle://localhost/omnijs-run?script=var%20wrapperPromise%20%3D%20document%2EmakeFileWrapper%28%22Default%20Name%22%29%0A%0AwrapperPromise%2Ethen%28wrapper%20%3D%3E%20%7B%0A%09filesaver%20%3D%20new%20FileSaver%28%29%0A%09fileSaverPromise%20%3D%20filesaver%2Eshow%28wrapper%29%0A%0A%09fileSaverPromise%2Ethen%28urlObj%20%3D%3E%20%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%28err%20%3D%3E%20%7B%0A%09%09console%2Elog%28err%2Emessage%29%0A%09%7D%29%0A%7D%29%0A%0AwrapperPromise%2Ecatch%28err%20%3D%3E%20%7B%0A%09console%2Elog%28err%2Emessage%29%0A%7D%29
omnioutliner://localhost/omnijs-run?script=var%20wrapperPromise%20%3D%20document%2EmakeFileWrapper%28%22Default%20Name%22%29%0A%0AwrapperPromise%2Ethen%28wrapper%20%3D%3E%20%7B%0A%09filesaver%20%3D%20new%20FileSaver%28%29%0A%09fileSaverPromise%20%3D%20filesaver%2Eshow%28wrapper%29%0A%0A%09fileSaverPromise%2Ethen%28urlObj%20%3D%3E%20%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%28err%20%3D%3E%20%7B%0A%09%09console%2Elog%28err%2Emessage%29%0A%09%7D%29%0A%7D%29%0A%0AwrapperPromise%2Ecatch%28err%20%3D%3E%20%7B%0A%09console%2Elog%28err%2Emessage%29%0A%7D%29
omnifocus://localhost/omnijs-run?script=var%20wrapperPromise%20%3D%20document%2EmakeFileWrapper%28%22Default%20Name%22%29%0A%0AwrapperPromise%2Ethen%28wrapper%20%3D%3E%20%7B%0A%09filesaver%20%3D%20new%20FileSaver%28%29%0A%09fileSaverPromise%20%3D%20filesaver%2Eshow%28wrapper%29%0A%0A%09fileSaverPromise%2Ethen%28urlObj%20%3D%3E%20%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%28err%20%3D%3E%20%7B%0A%09%09console%2Elog%28err%2Emessage%29%0A%09%7D%29%0A%7D%29%0A%0AwrapperPromise%2Ecatch%28err%20%3D%3E%20%7B%0A%09console%2Elog%28err%2Emessage%29%0A%7D%29
Default Document Export
     

var wrapperPromise = document.makeFileWrapper("Default Name") wrapperPromise.then(wrapper => { filesaver = new FileSaver() fileSaverPromise = filesaver.show(wrapper) fileSaverPromise.then(urlObj => { console.log(urlObj.string) new Alert("FILE URL", urlObj.string).show() }) fileSaverPromise.catch(err => { console.log(err.message) }) }) wrapperPromise.catch(err => { console.log(err.message) })

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 TypeIdentifer class, and its use to generate application-specific FileWrappers, can be found in the TypeIdentifer topic.

omniplan://localhost/omnijs-run?script=var%20fileTypeID%20%3D%20%27com%2Eomnigroup%2Eomniplan2%2Eplanfile%27%0Avar%20dc%20%3D%20Calendar%2Ecurrent%2EdateComponentsFromDate%28new%20Date%28%29%29%0Avar%20dateSlug%20%3D%20dc%2Emonth%20%2B%20%22%2D%22%20%2B%20dc%2Eday%20%2B%20%22%2D%22%20%2B%20dc%2Eyear%0Avar%20defaultName%20%3D%20%22Backup%22%20%2B%20%22%2D%22%20%2B%20dateSlug%20%2F%2F%2D%2D%3E%208%2D23%2D21%0Avar%20wrapperPromise%20%3D%20document%2EmakeFileWrapper%28defaultName%2C%20fileTypeID%29%0A%0AwrapperPromise%2Ethen%28wrapper%20%3D%3E%20%7B%0A%09var%20filesaver%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%09var%20fileSaverPromise%20%3D%20filesaver%2Eshow%28wrapper%29%0A%0A%09fileSaverPromise%2Ethen%28urlObj%20%3D%3E%20%7B%0A%09%09console%2Elog%28urlObj%2Estring%29%0A%09%7D%29%0A%0A%09fileSaverPromise%2Ecatch%28err%20%3D%3E%20%7B%0A%09%09console%2Elog%28err%2Emessage%29%0A%09%7D%29%0A%7D%29%0A%0AwrapperPromise%2Ecatch%28err%20%3D%3E%20%7B%0A%09console%2Eerror%28err%2Emessage%29%0A%7D%29
Save OmniPlan Document
 

var fileTypeID = 'com.omnigroup.omniplan2.planfile' var dc = Calendar.current.dateComponentsFromDate(new Date()) var dateSlug = dc.month + "-" + dc.day + "-" + dc.year var defaultName = "Backup" + "-" + dateSlug //--> 8-23-21 var wrapperPromise = document.makeFileWrapper(defaultName, fileTypeID) wrapperPromise.then(wrapper => { var 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] var fileSaverPromise = filesaver.show(wrapper) fileSaverPromise.then(urlObj => { console.log(urlObj.string) }) fileSaverPromise.catch(err => { console.log(err.message) }) }) wrapperPromise.catch(err => { console.error(err.message) })

 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.

 25-27  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.

omnigraffle://localhost/omnijs-run?script=%2F%2F%20EXPORT%20CANVAS%20TO%20TWO%20FILES%20IN%20FOLDER%0AcnvsName%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Ecanvas%2Ename%0A%0Avar%20PNGwrapperPromise%20%3D%20document%2EmakeFileWrapper%28cnvsName%2C%20%22public%2Epng%22%29%0Avar%20PDFwrapperPromise%20%3D%20document%2EmakeFileWrapper%28cnvsName%2C%20%22com%2Eadobe%2Epdf%22%29%0Avar%20promises%20%3D%20%5BPNGwrapperPromise%2C%20PDFwrapperPromise%5D%0A%0APromise%2Eall%28promises%29%2Ethen%28fileWrappers%20%3D%3E%20%7B%0A%0A%09%2F%2F%20Create%20and%20Show%20File%20Saver%0A%09var%20filesaver%20%3D%20new%20FileSaver%28%29%0A%09var%20folderType%20%3D%20new%20FileType%28%22public%2Efolder%22%29%0A%09filesaver%2Etypes%20%3D%20%5BfolderType%2C%20FileType%2Epng%2C%20FileType%2Epdf%5D%0A%09var%20fldrwrapper%20%3D%20FileWrapper%2EwithChildren%28%22Export%20Folder%22%2C%20fileWrappers%29%0A%09var%20fileSaverPromise%20%3D%20filesaver%2Eshow%28fldrwrapper%29%0A%0A%09%2F%2F%20Process%20File%20Saver%20Result%0A%09fileSaverPromise%2Ethen%28urlObj%20%3D%3E%20%7B%0A%09%09console%2Elog%28urlObj%2Estring%29%0A%09%7D%29%0A%0A%09fileSaverPromise%2Ecatch%28err%20%3D%3E%20%7B%0A%09%09console%2Elog%28err%2Emessage%29%0A%09%7D%29%0A%09%0A%7D%29%2Ecatch%28err%20%3D%3E%20%7B%0A%09console%2Elog%28%22Error%22%2C%20err%2Emessage%29%0A%7D%29
Multiple Export to Folder
 

// EXPORT CANVAS TO TWO FILES IN FOLDER cnvsName = document.windows[0].selection.canvas.name var PNGwrapperPromise = document.makeFileWrapper(cnvsName, "public.png") var PDFwrapperPromise = document.makeFileWrapper(cnvsName, "com.adobe.pdf") var promises = [PNGwrapperPromise, PDFwrapperPromise] Promise.all(promises).then(fileWrappers => { // Create and Show File Saver var filesaver = new FileSaver() var folderType = new FileType("public.folder") filesaver.types = [folderType, FileType.png, FileType.pdf] var fldrwrapper = FileWrapper.withChildren("Export Folder", fileWrappers) var fileSaverPromise = filesaver.show(fldrwrapper) // Process File Saver Result fileSaverPromise.then(urlObj => { console.log(urlObj.string) }) fileSaverPromise.catch(err => { console.log(err.message) }) }).catch(err => { console.log("Error", err.message) })

 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.

Export Layers

Here’s another example plug-in that exports the layers of the current canvas as either individual PNG files, or as images composited from the bottom to the topmost layer.

Image
Export Layers to Images
 

/*{ "type": "action", "targets": ["omnigraffle"], "author": "Otto Automator", "identifier": "com.omni-automation.og.export-layers-current-vanvas", "version": "1.0", "description": "Exports the layers of the current canvas as either individual PNG files, or as images composited from the bottom to the topmost layer.", "label": "Export Current Canvas Layers", "shortLabel": "Export Layers", "paletteLabel": "Export Layers", "image": "gearshape" }*/ (() => { var action = new PlugIn.Action(function(selection, sender){ var exportOptions = ["Individual Images","Composited Images"] var exportOptionsMenu = new Form.Field.Option( "exportOption", null, [0,1], exportOptions, 0 ) var inputForm = new Form() inputForm.addField(exportOptionsMenu) var formPrompt = "Choose the export style:" var buttonTitle = "Continue" var formPromise = inputForm.show(formPrompt,buttonTitle) formPromise.then(formObject => { var exportOptionIndex = formObject.values["exportOption"] var cnvs = document.windows[0].selection.canvas cnvs.layers.forEach(layer => layer.visible = false) var wrapperPromises = new Array() cnvs.layers.slice().reverse().forEach((layer, index) => { layer.visible = true var exportName = encodeURIComponent(index + "-" + layer.name) var fileTypeID = "public.png" var promise = document.makeFileWrapper(exportName, fileTypeID) wrapperPromises.push(promise) if (exportOptionIndex === 0){layer.visible = false} }) cnvs.layers.forEach(layer => layer.visible = true) Promise.all(wrapperPromises).then(wrappers => { // Create and Show File Saver var filesaver = new FileSaver() var folderType = new FileType("public.folder") filesaver.types = [folderType, FileType.png, FileType.pdf] var docName = document.name var fldrwrapper = FileWrapper.withChildren(`${docName} Export Folder`, wrappers) var fileSaverPromise = filesaver.show(fldrwrapper) // Process File Saver Result fileSaverPromise.then(urlObj => {urlObj.open()}) }) }) }); action.validate = function(selection, sender){ return true }; return action; })();

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:

Save Shape Text to File
 

/*{ "type": "action", "targets": ["omnigraffle"], "author": "Otto Automator", "identifier": "com.omni-automation.og.shape-text-to-file", "version": "1.1", "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 action = new PlugIn.Action(function(selection, sender){ // action code // selection options: canvas, document, graphics, lines, solids, view var shape = selection.solids[0] var shapeText = shape.text var wrapper = FileWrapper.withContents('Shape Text.txt', Data.fromString(shapeText)) var filesaver = new FileSaver() var fileSaverPromise = filesaver.show(wrapper) fileSaverPromise.then(urlObj => { console.log(urlObj.string) urlObj.open() }) fileSaverPromise.catch(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 != "") } 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