Banana.IO

Documentation •
Dans cet 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, 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)

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)

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.

The path can be relative to the script's folder:
file:script/<relative_path_to_script_folder>/<file_name>

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. 

Aidez-nous à améliorer la documentation

Nous serions heureux de recevoir des commentaires sur l'amélioration de cette page.

Dites-nous quel thème doit être mieux expliqué ou comment clarifier un sujet.

Partager cet article: Twitter | Facebook | LinkedIn | Email