In this article
The Banana.IO class is used to read and write to files.
Introduction
The API Banana.IO and Banana.IO.LocalFile allow a script to read or write to files in a secure way. The script can only read or write to files that are first selected by the user through the corresponding dialogs. The script has no direct access to files on the file system. After the script finishes, the permissions to write or read files are canceled.
For example to write the result of a script to a file:
var fileName = Banana.IO.getSaveFileName("Select save file", "", "Text file (*.txt);;All files (*)"); if (fileName.length) { var file = Banana.IO.getLocalFile(fileName) file.codecName = "latin1"; // Default is UTF-8 file.write("Text to save ..."); if (!file.errorString) { Banana.IO.openPath(fileContent); } else { Banana.Ui.showInformation("Write error", file.errorString); } } else { Banana.Ui.showInformation("Info", "no file selected"); }
To read the content of a file:
var fileName = Banana.IO.getOpenFileName("Select open file", "", "Text file (*.txt);;All files (*)") if (fileName.length) { var file = Banana.IO.getLocalFile(fileName) file.codecName = "latin1"; // Default is UTF-8 var fileContent = file.read(); if (!file.errorString) { Banana.IO.openPath(fileContent); } else { Banana.Ui.showInformation("Read error", file.errorString); } } else { Banana.Ui.showInformation("Info", "no file selected"); }
Methods
getOpenFileName(caption, path, filter)
The method getOpenFileName returns an existing file selected by the user. If the user presses Cancel, it returns an empty string. The file selected by the user is then allowed to be read, but not written.
The parameter caption is the caption of the dialog.
The parameter path is path inclusive the file name to be selected. If the path is relative, the current open document path or the user's document path is used.
The parameter filter set the files types to be showed. If you want multiple filters, separate them with ';;', for example: "Text file (*.txt);;All files (*)".
var fileName = Banana.IO.getOpenFileName("Select file to read", "", "Text file (*.txt);;All files (*)")
Since: Banana Accounting 9.0.7, only in Banana Experimental
getSaveFileName(caption, path, filter)
The method getSaveFileName returns an existing file selected by the user. If the user presses Cancel, it returns an empty string. The file selected by the user is then allowed to be read and written.
The parameter caption is the caption of the dialog.
The parameter path is path inclusive the file name to be selected. If the path is relative, the current open document path or the user's document path is used.
The parameter filter set the files types to be showed. If you want multiple filters, separate them with ';;', for example: "Text file (*.txt);;All files (*)".
var fileName = Banana.IO.getSaveFileName("Select file to write", "", "Text file(*.txt);;All files (*)")
getLocalFile(path)
The method getLocalFile(path) returns an object of type Banana.IO.LocalFile that represents the requested file. This method always returns a valid Banana.IO.LocalFile object.
The parameter path to the file.
openUrl(path)
The method openUrl(path) opens the file referred by path in the system default application.
The parameter path to the file.
openPath(path)
The method openPath(path) shows the folder containing the file referred by path in the system file manager.
The parameter path to the file.