Write Files Functions

The following functions allow writing to specific files in a predefined folder of the current project directory. The file system location allowed to write files is the folder shared\scripting of the current project. For example, [Installation Drive]:\[Installation Folder]\[Project Name]\shared\scripting.

Such functions can be run synchronously or asynchronously, and in the last case, it is necessary to provide a callback method that will be executed when the write operation is completed.

fileWriteSync

This fileWriteSync function allows writing any given <data> to the file specified by <relativePath> in a predefined folder, with the given <encoding>. If the file does not exist, it is created, otherwise it is replaced.

fileWrite

The fileWrite function allows writing asynchronously the given <data> to the file specified by <relativePath> in the predefined folder, with the given <encoding>. Then it calls the <callback> with the operation result. If the file does not exist, it is created, otherwise it is replaced.

Syntax

 

(Synchronous write)

var result = fileWriteSync(<relativePath>, <data>, <encoding>)

 

(Asynchronous write. The result is provided in the callback method.)

fileWrite(<relativePath>, <data>, <callback>, <encoding>)

Parameters Usage

Parameter

Type

Default

Use

Annotation

relativePath

String

-

Mandatory

Relative path of the file to write in the shared\scripting folder of the current project.

data

String

Object

-

Mandatory

Data to write.

It can be a string or any object to which it makes sense to apply the ToString method (for example, simple data as integer, real or boolean values, or objects as BrowserObject or PropertyValue, and so on).

In case of arrays, the elements are printed as comma-separated values enclosed in square brackets.

callback

Function

-

Mandatory for asynchronous calls

Object that identifies the callback function invoked to provide the result of the operation.

encoding

String

"utf8"

Optional

File encoding. Allowed values are "utf8", "ascii", "Unicode", "utf32", "utf7".

The callback function is declared as follows:

 

function fileWriteCallback(<FileWriteResult>)

{

    //... Do callback stuff here ...

}

 

Where <FileWriteResult> is the result object.

Result

The functions fileWriteSync and fileWriteCallback return the FileWriteResult object.

Error Handling

Errors can occur in case:

  • The specified <relativePath>:
    • Is missing, null, empty or white space
    • Is not a string
    • Is invalid (such as, path containing a root directory, wrong absolute path, path outside of the predefined folder, path or file name containing invalid characters, files or directories that have too long path). Note that invalid characters and the path length depend on the operating system.
    • Does not represent a file
    • Represents a read-only file. For example, the content of result.error is:
      "Access to the path '[Installation Drive]:\[Installation Folder]\[Project Name]\shared\scripting\readonlyFile.txt' is denied."
  • Data to write is not specified or is null.
    • The error message is: "The data to write is invalid."
  • The callback is missing, invalid, null, or empty (for asynchronous function only).
    • The related error is logged only in the Trace Viewer.
  • <encoding> is invalid.
  • Generic error.
    • Any other errors will result in a result.error message like: "Unable to write to file '<relativePath>'."
      In this case, check the Trace Viewer for a detailed explanation about the occurred exception.

Examples of Use

The following are examples of use of fileWriteSync. Note that the use of fileWrite is the same as the fileWriteSync, and the only difference is that the result is returned asynchronously in the specified callback.

 

How to write simple data with UTF8 encoding (default) and print to Console the result of the operation

The following instruction writes "hello world" to the file "[Installation Drive]:\[Company Name]\[Brand Name]\Project1\shared\scripting\SimpleData.txt" with UTF8 encoding.

var result = fileWriteSync("SimpleData.txt", "hello world")

Other examples of simple data that can be written to file (integer, float, boolean, char, and date):

fileWriteSync("SimpleData.txt", 123)

fileWriteSync("SimpleData.txt", -1.23)

fileWriteSync("SimpleData.txt", true)

fileWriteSync("SimpleData.txt", 'a')

fileWriteSync("SimpleData.txt", new Date())

 

The above instructions write the following data, and replace the file content any time the fileWriteSync function is called:

123

-1.23

True

a

8/31/2016 7:03:43 AM

 

How to write arrays of simple data with UTF8 encoding (default)

var filename = "SimpleArrayData.txt"

fileWriteSync(filename, ["hello world 1", "hello world 2", "hello world 3"]);

fileWriteSync(filename, [123, 456, -123, -456]);

fileWriteSync(filename, [12.3, -4.56, -7.89]);

fileWriteSync(filename, [true, false, true]);

fileWriteSync(filename, ['a', 'b', 'c', 'd', 'e']);

fileWriteSync(filename, [new Date(), new Date()]);

fileWriteSync(filename, ["hello", 'b', 1, -2.2, false, new Date()]);

 

The above instructions write the following data to "[Installation Drive]:\[Company Name]\[Brand Name]\Project1\shared\scripting\SimpleArrayData.txt" with UTF8 encoding. The file content is replaced at each call.

 

[hello world 1, hello world 2, hello world 3]

[123, 456, -123, -456]

[12.3, -4.56, -7.89]

{True, False, True}

[a, b, c, d, e]

[8/31/2016 9:08:05 AM, 8/31/2016 9:08:05 AM]

[hello, b, 1, -2.2, False, 8/31/2016 9:08:05 AM]

 

How to write one or multiple BrowserObject items

Declare the following BrowserObjects:

var browserObject1 = new BrowserObject("System1.ManagementView:ManagementView.SystemSettings.OrganizationModes.DayNight");

var browserObject2 = new BrowserObject("System1.ManagementView:ManagementView.SystemSettings.OrganizationModes.OnOff");

 

The instruction:

fileWriteSync("BrowserObjectData.txt", browserObject1);

 

writes the following to [Installation Drive]:\[Installation Folder]\[Project Name]\shared\scripting\ BrowserObjectData.txt: System1.ManagementView:ManagementView.SystemSettings.OrganizationModes.DayNight

 

The instruction:

fileWriteSync("BrowserObjectData.txt", browserObject1, browserObject2]);

 

writes the following:

[System1.ManagementView:ManagementView.SystemSettings.OrganizationModes.DayNight, System1.ManagementView:ManagementView.SystemSettings.OrganizationModes.OnOff]

 

How to write a SubscribeValueResult

var browserObject = new BrowserObject("System1.ManagementView:ManagementView.SystemSettings.OrganizationModes.DayNight");

var subscribeValueResult = subscribeValues(browserObject, ["Value"], onOrganizationModeChanged);

fileWriteSync("MyFile.txt", subscribeValueResult);

 

The above instruction writes Subscription succeeded to [Installation Drive]:\[Installation Folder]\[Project Name]\shared\scripting\MyFile.txt:

 

How to print to Console the result of a fileWriteSync call

var result = fileWriteSync("MyFile.txt", data)

console(result)

 

If data is valid (for example, data = "Hello world"), the Console expander contains: "File written successfully."

If data is not valid (for example, data = null), the Console expander contains: " The data to write is invalid."