Banana.IO

This documentation is outdated

The most complete and up-to-date documentation is the one of Banana Accounting Plus: Try it now

In this article

The Banana.IO class is used to read and write to files.

Since: Banana Accounting 8.0.7, only in Banana Experimental

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 writes to files that are first selected by the user though the corresponding dialogs. The script has no direct access to files on the file system. After the script finished, 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 readen, 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 8.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 readen 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 (*)")

Since: Banana Accounting 8.0.7, only in Banana Experimental

getLocalFile(path)

The method getLocalFile(path) return an object of type Banana.IO.LocalFile that represent the requested file. This method always return a valid Banana.IO.LocalFile object.

The parameter path to the file. 

Since: Banana Accounting 8.0.7, only in Banana Experimental

openUrl(path)

The method openUrl(path) open the file referred by path in the system default application.

The parameter path to the file. 

Since: Banana Accounting 8.0.7, only in Banana Experimental

openPath(path)

The method openPath(path) show the folder containing the file referred by path in the system file manager.

The parameter path to the file. 

Since: Banana Accounting 8.0.7, only in Banana Experimental

Share this article: Twitter | Facebook | LinkedIn | Email