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 Document

The following Omni Automation script demonstrates how to save an OmniPlan document to file. The same 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.

// Create File Wrapper and File Type fileTypeID = 'com.omnigroup.omniplan2.planfile' defaultName = "" wrapper = document.fileWrapper(fileTypeID) if(defaultName){wrapper.preferredFilename = defaultName} filetype = new FileType(fileTypeID) // Create and Show File Saver filesaver = new FileSaver() filesaver.message = "Choose the location for the saved file:" // macOS filesaver.nameLabel = "Name:" // macOS filesaver.prompt = "Continue" // macOS filesaver.types = [filetype] fileSaverPromise = filesaver.show(wrapper) // Process File Saver Result fileSaverPromise.then(function(urlObj){ console.log(urlObj.string) }) fileSaverPromise.catch(function(err){ console.log(err.message) })
omniplan://localhost/omnijs-run?script=try%7BfileTypeID%20%3D%20%27com%2Eomnigroup%2Eomniplan2%2Eplanfile%27%0Awrapper%20%3D%20document%2EfileWrapper%28fileTypeID%29%0Awrapper%2EpreferredFilename%20%3D%20%22My%20File%22%0Afiletype%20%3D%20new%20FileType%28fileTypeID%29%0A%0A%2F%2F%20Create%20and%20Show%20File%20Saver%0Afilesaver%20%3D%20new%20FileSaver%28%29%0Afilesaver%2Emessage%20%3D%20%22Choose%20the%20location%20for%20the%20saved%20file%3A%22%20%2F%2F%20macOS%0Afilesaver%2EnameLabel%20%3D%20%22Name%3A%22%20%2F%2F%20macOS%0Afilesaver%2Eprompt%20%3D%20%22Continue%22%20%2F%2F%20macOS%0Afilesaver%2Etypes%20%3D%20%5Bfiletype%5D%0AfileSaverPromise%20%3D%20filesaver%2Eshow%28wrapper%29%0A%0A%2F%2F%20Process%20File%20Saver%20Result%0AfileSaverPromise%2Ethen%28function%28url%29%7B%0A%09console%2Elog%28url%2Estring%29%0A%7D%29%0A%0AfileSaverPromise%2Ecatch%28function%28err%29%7B%0A%09console%2Elog%28err%2Emessage%29%0A%7D%29%7Dcatch%28err%29%7Bconsole%2Elog%28err%29%7D

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

 03  An instance of the FileWrapper class is created for the OmniPlan document by calling the fileWrapper(…) method on the document class and passing in the stored FileType identifier string. The resulting file wrapper is stored int eh variable: wrapper

 04  The suggested name to be used for the saved file is assigned as the value of the preferredFilename property of the created file wrapper instance.

 05  The FileType identifier string is again used to create an instance of the FileType class, which is stored in the variable: filetype

 08  A new instance of the FileSaver class is stored in the variable: filesaver

 09-11  On macOS, the save dialog can be customized with a user prompt, name input lable, and approval button title. iOS does not offer these options.

 12  The value of the types property of the FileSaver instance is an array of the file types that will be allowed to be saved. By setting the file type to the previously created file type object, the adding of the correct name extenstion to the saved/exported file name will be done automatically, requiring no input by the user.

 13  To display the save dialog to the user, the show(…) method is called on the FileaSaver instance while passing in the created FileWrapper instance. The result of this command is a JavaScript promise object that represents the completion or cancellation of the dialog by the user.

 16-18  The results of a successful completion of the save dialog by the user will handled through the use of the then(…) function called on the stored promise object. A URL object representing the saved file will be automatically passed as input into the function. Place your statements for processing the passed URL object within this handler. In this example, the URL string is extracted from the URL object and logged in the Console.

 20-22  A cancelation of the save dialog by the user can be handled by the optional catch(…) method called on the stored promise instance. The error object for the save dialog will be passed as input to this function. Place any statements for dealing with the cancellation within this function.

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) })
omnigraffle://localhost/omnijs-run?script=try%7BcnvsName%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Ecanvas%2Ename%0A%0A%2F%2F%20Create%20file%20types%20and%20wrappers%0APNGtypeID%20%3D%20%22public%2Epng%22%0APDFtypeID%20%3D%20%22com%2Eadobe%2Epdf%22%0APNGwrapper%20%3D%20document%2EfileWrapper%28PNGtypeID%29%0APNGwrapper%2EpreferredFilename%20%3D%20cnvsName%20%2B%20%22%2Epng%22%0APNGfileType%20%3D%20FileType%2Epng%0APDFwrapper%20%3D%20document%2EfileWrapper%28PDFtypeID%29%0APDFwrapper%2EpreferredFilename%20%3D%20cnvsName%20%2B%20%22%2Epdf%22%0APDFfileType%20%3D%20FileType%2Epdf%0Atypes%20%3D%20%5BPNGwrapper%2C%20PDFwrapper%5D%0Afldrwrapper%20%3D%20FileWrapper%2EwithChildren%28%22Export%20Folder%22%2Ctypes%29%0AfolderType%20%3D%20new%20FileType%28%22public%2Efolder%22%29%0A%0A%2F%2F%20Create%20and%20Show%20File%20Saver%0Afilesaver%20%3D%20new%20FileSaver%28%29%0Afilesaver%2Emessage%20%3D%20%22Choose%20the%20location%20for%20the%20export%20folder%3A%22%20%2F%2F%20macOS%0Afilesaver%2EnameLabel%20%3D%20%22Name%3A%22%20%2F%2F%20macOS%0Afilesaver%2Eprompt%20%3D%20%22Continue%22%20%2F%2F%20macOS%0Afilesaver%2Etypes%20%3D%20%5BfolderType%2CPNGfileType%2CPDFfileType%5D%0AfileSaverPromise%20%3D%20filesaver%2Eshow%28fldrwrapper%29%0A%0A%2F%2F%20Process%20File%20Saver%20Result%0AfileSaverPromise%2Ethen%28function%28urlObj%29%7B%0A%09console%2Elog%28urlObj%2Estring%29%0A%7D%29%0A%0AfileSaverPromise%2Ecatch%28function%28err%29%7B%0A%09console%2Elog%28err%2Emessage%29%0A%7D%29%7Dcatch%28err%29%7Bconsole%2Elog%28err%29%7D

 01  Get the name of the current canvas in the OmniGraffle document. NOTE: if this example script was wrapped within an action plugIn template, use the canvas property of the passed-in selection object: selection.canvas

 04-14  Create file wrappers and file types for the desired export items.

 07   10  Since the canvas title is to be used as the name for each of the exported files, include the name extension in the name to avoid a naming conflict error. Usually, file extensions are not required in the name and are automatically added by the file save instance.

 14  Note the inclusion of the folder file type, which will be added to the beginning of the types array passed to the file saver instance.

 17  Create a new instance of the FileSaver class.

 18-20  Set the values of the disdplay properties of the file saver instance.

 21  Assign the array of types as the value of the file saver’s types property. Note that the list of types begins with the file type of the folder that is to contain the exported images.

 22  Display the FileSaver dialog for the user to choose the location in which the new folder will be created. When the selection is made by the user, the dialog will close and the folder containing the exported items will be created.

Using the FileSaver with Shortcuts App

Here is an example Omni Automation action plug-in that calls a Shortcuts workflow to process a saved OmniPlan file. You can adapt this action to work with any of the Omni suite of applications.

/*{ "type": "action", "targets": ["omniplan"], "author": "Action Author", "identifier": "com.youOrCompany.actionName", "version": "1.0", "description": "Action Description", "label": "Menu Item Title", "paletteLabel": "Toolbar Item Title" }*/ var _ = function(){ var action = new PlugIn.Action(function(selection, sender){ // action code // selection options: project, tasks, resources // Change this value to match the file type to export var aFileTypeIdentifer = 'com.omnigroup.omniplan2.planfile' // Create File Wrapper and File Type wrapper = document.fileWrapper(aFileTypeIdentifer) filetype = new FileType(aFileTypeIdentifer) // Create and Show File Saver filesaver = new FileSaver() filesaver.message = "Choose the location for the saved file:" // macOS filesaver.nameLabel = "Name:" // macOS filesaver.prompt = "Continue" // macOS filesaver.types = [filetype] fileSaverPromise = filesaver.show(wrapper) // Process File Saver Result fileSaverPromise.then(function(urlObj){ encodedFileURLString = encodeURIComponent(urlObj.string) // change the name to match the name of the targeted Shortcuts workflow workflowName = "Processing Workflow" workflowName = encodeURIComponent(workflowName) callingURLString = "shortcuts://run-workflow?name=" + workflowName + "&input=text&text=" + encodedFileURLString URL.fromString(callingURLString).call(function(reply){ console.log(reply) } }) fileSaverPromise.catch(function(err){ console.log(err.message) }) }); action.validate = function(selection, sender){ // validation code // selection options: project, tasks, resources 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