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.

(async () => { try { wrapper = await document.makeFileWrapper("Default Name") filesaver = new FileSaver() urlObj = await filesaver.show(wrapper) new Alert("FILE URL", urlObj.string).show() } catch(err){ if(!err.causedByUserCancelling){ new Alert(err.name, err.message).show() } } })();
Default Document Export
  

(async () => { try { wrapper = await document.makeFileWrapper("Default Name") filesaver = new FileSaver() urlObj = await filesaver.show(wrapper) new Alert("FILE URL", urlObj.string).show() } catch(err){ if(!err.causedByUserCancelling){ new Alert(err.name, err.message).show() } } })();

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=%28async%20%28%29%20%3D%3E%20%7B%0A%09try%20%7B%0A%09%09%2F%2F%20CREATE%20FILE%20WRAPPER%0A%09%09fileTypeID%20%3D%20%27com%2Eomnigroup%2Eomniplan2%2Eplanfile%27%0A%09%09dc%20%3D%20Calendar%2Ecurrent%2EdateComponentsFromDate%28new%20Date%28%29%29%0A%09%09dateSlug%20%3D%20dc%2Emonth%20%2B%20%22%2D%22%20%2B%20dc%2Eday%20%2B%20%22%2D%22%20%2B%20dc%2Eyear%0A%09%09defaultName%20%3D%20%22Backup%22%20%2B%20%22%2D%22%20%2B%20dateSlug%20%2F%2F%2D%2D%3E%208%2D23%2D21%0A%09%09wrapper%20%3D%20await%20document%2EmakeFileWrapper%28defaultName%2C%20fileTypeID%29%0A%0A%09%09%2F%2F%20CREATE%20AND%20DISPLAY%20FILESAVER%0A%09%09filesaver%20%3D%20new%20FileSaver%28%29%0A%09%09filesaver%2Emessage%20%3D%20%22Choose%20the%20location%20for%20the%20saved%20file%3A%22%20%2F%2F%20macOS%0A%09%09filesaver%2EnameLabel%20%3D%20%22Name%3A%22%20%2F%2F%20macOS%0A%09%09filesaver%2Eprompt%20%3D%20%22Continue%22%20%2F%2F%20macOS%0A%09%09filetype%20%3D%20new%20FileType%28fileTypeID%29%0A%09%09filesaver%2Etypes%20%3D%20%5Bfiletype%5D%0A%09%09urlObj%20%3D%20await%20filesaver%2Eshow%28wrapper%29%0A%09%09%0A%09%09console%2Elog%28urlObj%2Estring%29%0A%09%7D%0A%09catch%28err%29%7B%0A%09%09if%28%21err%2EcausedByUserCancelling%29%7B%0A%09%09%09new%20Alert%28err%2Ename%2C%20err%2Emessage%29%2Eshow%28%29%0A%09%09%7D%0A%09%7D%0A%7D%29%28%29%3B
Save OmniPlan Document
 

(async () => { try { // CREATE FILE WRAPPER fileTypeID = 'com.omnigroup.omniplan2.planfile' dc = Calendar.current.dateComponentsFromDate(new Date()) dateSlug = dc.month + "-" + dc.day + "-" + dc.year defaultName = "Backup" + "-" + dateSlug //--> 8-23-21 wrapper = await document.makeFileWrapper(defaultName, fileTypeID) // CREATE AND DISPLAY FILESAVER 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] urlObj = await filesaver.show(wrapper) console.log(urlObj.string) } catch(err){ if(!err.causedByUserCancelling){ new Alert(err.name, err.message).show() } } })();

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=%28async%20%28%29%20%3D%3E%20%7B%0A%09try%20%7B%0A%09%09cnvsName%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Ecanvas%2Ename%0A%0A%09%09PNGwrapper%20%3D%20await%20document%2EmakeFileWrapper%28cnvsName%2C%20%22public%2Epng%22%29%0A%09%09PDFwrapper%20%3D%20await%20document%2EmakeFileWrapper%28cnvsName%2C%20%22com%2Eadobe%2Epdf%22%29%0A%09%09fileWrappers%20%3D%20%5BPNGwrapper%2C%20PDFwrapper%5D%0A%0A%09%09%2F%2F%20Create%20and%20Show%20File%20Saver%0A%09%09filesaver%20%3D%20new%20FileSaver%28%29%0A%09%09folderType%20%3D%20new%20FileType%28%22public%2Efolder%22%29%0A%09%09filesaver%2Etypes%20%3D%20%5BfolderType%2C%20FileType%2Epng%2C%20FileType%2Epdf%5D%0A%09%09fldrwrapper%20%3D%20FileWrapper%2EwithChildren%28%22Export%20Folder%22%2C%20fileWrappers%29%0A%0A%09%09urlObj%20%3D%20await%20filesaver%2Eshow%28fldrwrapper%29%0A%09%09console%2Elog%28urlObj%2Estring%29%0A%09%7D%0A%09catch%28err%29%7B%0A%09%09if%28%21err%2EcausedByUserCancelling%29%7B%0A%09%09%09new%20Alert%28err%2Ename%2C%20err%2Emessage%29%2Eshow%28%29%0A%09%09%7D%0A%09%7D%0A%7D%29%28%29%3B
Multiple Canvas Export to Folder
 

(async () => { try { cnvsName = document.windows[0].selection.canvas.name PNGwrapper = await document.makeFileWrapper(cnvsName, "public.png") PDFwrapper = await document.makeFileWrapper(cnvsName, "com.adobe.pdf") fileWrappers = [PNGwrapper, PDFwrapper] // 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) urlObj = await filesaver.show(fldrwrapper) console.log(urlObj.string) } catch(err){ if(!err.causedByUserCancelling){ new Alert(err.name, err.message).show() } } })();

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.1", "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": "square.3.layers.3d.top.filled" }*/ (() => { const action = new PlugIn.Action(async function(selection, sender){ try { exportOptions = ["Individual Images","Composited Images"] exportOptionsMenu = new Form.Field.Option( "exportOption", null, [0,1], exportOptions, 0 ) inputForm = new Form() inputForm.addField(exportOptionsMenu) formPrompt = "Choose the export style:" buttonTitle = "Continue" formObject = await inputForm.show(formPrompt,buttonTitle) exportOptionIndex = formObject.values["exportOption"] cnvs = document.windows[0].selection.canvas cnvs.layers.forEach(layer => layer.visible = false) wrappers = new Array() layerSet = cnvs.layers.slice().reverse() counter = 0 for (aLayer of layerSet){ aLayer.visible = true exportName = encodeURIComponent(counter + "-" + aLayer.name) fileTypeID = "public.png" wrapper = await document.makeFileWrapper(exportName,fileTypeID) wrappers.push(wrapper) if (exportOptionIndex === 0){aLayer.visible = false} counter = counter + 1 } cnvs.layers.forEach(layer => layer.visible = true) filesaver = new FileSaver() folderType = new FileType("public.folder") filesaver.types = [folderType, FileType.png, FileType.pdf] docName = document.name.replace(/\.[^/.]+$/, "") fldrwrapper = FileWrapper.withChildren(`${docName} Export Folder`, wrappers) urlObj = await filesaver.show(fldrwrapper) urlObj.open() } catch(err){ if(!err.causedByUserCancelling){ new Alert(err.name, err.message).show() } } }); 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.2", "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", "paletteLabel": "Text to File", "image": "note.text" }*/ (() => { const action = new PlugIn.Action(async function(selection, sender){ shape = selection.solids[0] shapeText = shape.text data = Data.fromString(shapeText) wrapper = FileWrapper.withContents('Shape Text.txt', data) filesaver = new FileSaver() urlObj = await filesaver.show(wrapper) console.log(urlObj.string) urlObj.open() }); action.validate = function(selection, sender){ if (selection.solids.length === 1){ return (selection.solids[0].text !== "") } return false }; return action; })();
FIlesaver iOS FIlesaver iOS FIlesaver iOS