Banana.IO
The Banana.IO is a static class that contains static methods 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, except for files that resides in the script package. The script has no direct access to files on the file system. After the script finishes, the permissions to write or read files are removed.
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 from 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");
}
To read the content of a file contained in the script package:
let file = Banana.IO.getLocalFile("file:script/changelog.md")
let text = file.read()
Methods
fileCompleteBaseName(path)
Static method.
The method fileCompleteBaseName Returns the complete base name of the file without the path. The complete base name consists of all characters in the file up to (but not including) the last '.' character.
The parameter path is path inclusive the file name to be selected.
let path = "file:script/../test/testcases/MyTestFile.csv"
let fileName = Banana.IO.fileCompleteBaseName(path); // fileName = MyTestFile.csv
getOpenFileName(caption, path, filter)
Static method.
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)
Static method.
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)
Static method.
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.
The path can be relative to the script's folder:
file:script/<relative_path_to_script_folder>/<file_name>
openUrl(path)
Static method.
The method openUrl(path) opens the file referred by path in the system default application.
The parameter path to the file.
openPath(path)
Static method.
The method openPath(path) shows the folder containing the file referred by path in the system file manager.
The parameter path to the file.
Banana.IO.LocalFile
The LocalFile class represents a file on the local file system. See Banana.IO for an example.
Properties
codecName
The name of the codec to be used for reading or writing the file. Default is 'UTF-8'.
errorString
Read only. The string of the last occured error. If no error occured it is empty.
Methods
read()
Returns the content of the file. This function has no way of reporting errors. Returning an empty string can mean either that the file is empty, or that an error occurred. Check the content of the property errorString to see if an error occured.
write(text [, append])
Write text to the file. If append is set to true, text is appended to the file. Returns true if the operation was succesfully, false otherwise.