- Print
- PDF
ActiveXObject
Description
The ActiveXObject class is used to instantiate simple ActiveX objects in script. It is not possible to describe properties and methods since these vary from one object to another.
It is not possible to use properties and methods that return another ActiveXObject. Only the more simple types are supported.
Constructors
ActiveXObject | |
Description | Returns a new ActiveXObject based on the classID. If servername is specified the ActiveX object will be instantiated on that server. This presumes that Lasernet runs under an account that has the required privileges. |
Parameters | classID : String [servername : String] |
Returns | ActiveXObject |
Example | var ax = new ActiveXObject('Microsoft.Word'); |
File
Description
The File class is used to access files in the file system. It is possible to both read and write files.
function FileWriteTest()
{
var f = new File('c:\\file.txt');
if (f.open(IO_WriteOnly, false))
{
f.write('This is a test \n');
f.write('This is a test \n');
f.write('This is a test \n');
f.write('This is a test \n');
f.close();
}
}As can be seen in the above example two \ (backslashes) are used as a separator in the filename. The reason for this is that strings in JavaScript can have special symbols encoded using backslash. As a result, two backslashes in a row evaluates to one backslash, which gives us the right path.
Properties
type | |
Description | Returns the value ‘File’. |
Access | Read |
Returns | String |
Example | |
atEnd | |
Description | Returns true if the file position is at the end of the file. This is primarily used when reading files. Using this property it is possible to continue reading the contents of a file until no more data is available. The return value of this property is undefined before the file is opened. |
Access | Read |
Returns | Boolean |
Example | function fileReadTest() { var f = new File('c:\\file.txt'); if (f.open(IO_ReadOnly, false)) { // Continue reading until we reach end of file while (!f.atEnd()) { var line = f.readLine(1000); job.setJobInfo('Lines', line, false); job.setJobInfo('LineLength', f.bytesRead, false); } f.close(); } } |
bytesRead | |
Description | Returns the number of bytes that the last call to readLine read from the file after a call to readLine(). If readLine() has not been called the property returns 0 (zero). |
Access | Read |
Returns | Number |
Example | function fileReadTest() { var f = new File('c:\\file.txt'); if (f.open(IO_ReadOnly, false)) { // Continue reading until we reach end of file while (!f.atEnd()) { var line = f.readLine(1000); job.setJobInfo('Lines', line, false); job.setJobInfo('LineLength', f.bytesRead, false); } f.close(); } } |
created | |
Description | Gets the created date time for the file. |
Access | Read |
Returns | Date |
Example | var f = new File('c:\\file.txt'); var t = f.created; |
isDir | |
Description | Returns true if the file referred to is in fact a directory, false otherwise. |
Access | Read |
Returns | Boolean |
Example | var f = new File('c:\\windows'); if (f.isDir) { // Do some magic } |
isFile | |
Description | Returns true if the file referred to is in fact a file, false otherwise. |
Access | Read |
Returns | Boolean |
Example | var f = new File('c:\\file.txt'); if (f.isFile) { // Do some magic } |
isReadable | |
Description | Returns true if the file is readable, false otherwise. |
Access | Read |
Returns | Boolean |
Example | var f = new File('c:\\file.txt'); if (f.isReadable) { // Do some magic } |
isWritable | |
Description | Returns true if the file is writable, false otherwise. |
Access | Read |
Returns | Boolean |
Example | var f = new File('c:\\file.txt'); if (f.isWritable) { // Do some magic } |
lastModified | |
Description | Gets the last modified date time for the file. |
Access | Read |
Returns | Date |
Example | var f = new File('c:\\file.txt'); var lastModifiedDate = f.lastModified; |
lastRead | |
Description | Gets the last read date time for the file. |
Access | Read |
Returns | Date |
Example | var f = new File('c:\\file.txt'); var lastReadDate = f.lastRead; |
name | |
Description | Sets and gets the filename of the file being handled. Do not set the filename after the file has been opened – the behaviour is undefined. In general you should always use the full path of the file. Not using a full path will most likely succeed, but the path will then be dependent on which directory is the current directory for the entire Lasernet application. This directory may change at any time. |
Access | Read, Write |
Returns | String |
Example | |
size | |
Description | Returns the size in bytes of the file. The file does not have to be open to get the size. |
Access | Read |
Returns | Number |
Example | var f = new File('c:\\file.txt'); var fileSizeInBytes = f.size; |
Static Methods
copy | |
Description | Copies a source file to a new location and/or filename. Function returns true on success and false on error. |
Parameters | sourceFilename : String destinationFilename : String [allowOverwrite : Boolean = true] |
Returns | Boolean |
Example | File.copy('c:\\file1.txt', 'c:\\file2.txt', true); |
exists | |
Description | Returns true if the file with filename, which should be the full path to the file, exists. |
Parameters | filename : String |
Returns | Boolean |
Examples | if (File.exists("c:\\myfile.txt")) { // Do your magic with that file. } |
isFile | |
Description | Returns true if the file with filename, which should be the full path to the file, is a file. |
Parameters | filename : String |
Returns | Boolean |
Examples | if (File.isFile("c:\\myfile.txt")) { // Do your magic now you know it’s a file. } |
isDir | |
Description | Returns true if the file with filename, which should be the full path to the file, is a directory. |
Parameters | filename : String |
Returns | Boolean |
Examples | if (File.isDir("c:\\myfolder")) { // Do your magic now you know it’s a folder. } |
move | |
Description | Moves a source file to a new location and/or filename. Can also be used to rename a file. Function returns true on success and false on error. |
Parameters | sourceFilename : String destinationFilename : String [allowOverwrite : Boolean = true] |
Returns | Boolean |
Example | File.move('c:\\file1.txt', 'c:\\file2.txt', true); |
read | |
Description | Reads the entire contents of the file with the given filename into a String and returns that. Throws a File.read() exception if it for some reason fails to read the file. |
Parameters | filename : String |
Returns | String |
Example | var content = File.read("c:\\myfile.txt"); Note that the file has to be in a normal ANSI code page. If you need more a more advanced code page like UTF8, you must open the file and read the contents manually. var f = new File('c:\\test.txt'); f.setCodec('UTF8'); if (f.open(IO_ReadOnly)) { var text = new String (f.readAllText()); f.close(); } |
readBinary | |
Description | Reads the entire contents of the file with the given filename into a ByteArray and returns that. Throws a File.readBinary() exception if it for some reason fails to read the file. |
Parameters | filename : String |
Returns | ByteArray |
Example | var content = File.readBinary("c:\\myfile.txt"); |
remove | |
Description | Returns true if the file with filename, which should be the full path to the file, was removed. |
Parameters | filename : String |
Returns | Boolean |
Examples | if (File.remove("c:\\myfile.txt")) { // Do your magic now you know the file was removed. } |
size | |
Description | Reads the size of the file with the filename given and returns the value. Throws a File.size() exception and -1 as value if it for some reason fails to read the file. |
Parameters | filename : String |
Returns | Number |
Example | var long = File.size("c:\\myfile.txt"); |
write | |
Description | Writes a string to a file. Returns true if content was successfully written to file. |
Parameters | filename : String content : String |
Returns | Boolean |
Examples | if (File.write("c:\\myfile.txt", “Lasernet Scripting”)) { // Do your magic now you know content was written. } |
writeBinary | |
Description | Writes an array of bytes to a file. Returns true if data was successfully written to file. |
Parameters | filename : String data : ByteArray |
Returns | Boolean |
Examples | if (File.writeBinary("c:\\myfile.txt", job.jobdata)) { // Do your magic now you know jobdata was written. } |
Methods
exists | |
Description | Returns true if the file with filename exists. If no filename is given the filename set by the constructor or the name property is used. Useful for making sure that a file actually exists before opening it. Usually you would choose to use the static method File.exists() instead. |
Parameters | [Filename : String = “”] |
Returns | Boolean |
Example | var f = new File('c:\\test.txt'); if (f.exists()) { // Handle the file } |
close | |
Description | Closes the file so no more data can be read from or written to it until it has been opened again by a call to open(). |
Parameters | None |
Returns | None |
Example | var f = new File('c:\\test.txt'); if (f.open(IO_ReadOnly)) { var text = new String (f.readAllText()); f.close(); } |
flush | |
Description | Flushes any buffered data to the file. Returns true if successful; otherwise returns false. |
Parameters | None |
Returns | Boolean |
Example | |
open | |
Description | Opens the file with the given mode and exclusive settings. mode can be one of the following: IO_RAW // raw access (not buffered) IO_Async // asynchronous mode IO_ReadOnly // readable device IO_WriteOnly // writable device IO_ReadWrite // read+write device IO_Append // write+append device IO_Truncate // truncate device IO_Translate // translate CR+LF Set exclusive to true to make sure that you have exclusive access to the file. This only works on existing files. If the file does not already exist the exclusive setting does not have any effect. The exclusiveTimeout value is used to set how long to wait for exclusive access to the file. The value is in milliseconds. If exclusive access cannot be obtained before this timeout open returns false. |
Parameters | mode : Number [exclusive : Boolean = true] [exclusiveTimeout : Number = 10000] |
Returns | Boolean |
Example | var f = new File('c:\\test.txt'); if (f.open(IO_ReadOnly)) { // read the file f.close(); } |
readAll | |
Description | Reads the entire file into a ByteArray and returns the ByteArray. This can be used to load data from a file and put it into a JobInfo. |
Parameters | None |
Returns | ByteArray |
Example | var f = new File('c:\\test.txt'); if (f.open(IO_ReadOnly)) { job.setJobInfoBinary('ContentsOfTheFile', f.readAll()); f.close(); } |
readAllText | |
Description | Reads the entire file into a String and returns the string. If a codec has been set with setCodec() this codec is used to read the file. If not it is assumed that the file is a standard ANSI file. The property bytesRead reflects how many characters have been read when a codec has been set. Otherwise it returns how many bytes have been read, which will correspond to the number of characters. |
Parameters | None |
Returns | String |
Example | var f = new File('c:\\test.txt'); if (f.open(IO_ReadOnly)) { job.setJobInfo('ContentsOfTheFile', f.readAllText()); f.close(); } |
readLine | |
Description | Reads a line from the file. The returned String will include the CR/LF characters if any are read and a codec has not been set with setCodec. If an end of line is not found before maxLengthcharacters has been read, only a maxlength character is read. The property bytesRead will contain the number of bytes actually read. To make sure that you always read to the end of a line – or end of line, if no end of line exists you can use the size property. |
Parameters | maxLength : String |
Returns | String |
Example | var f = new File('c:\\test.txt'); if (f.open(IO_ReadOnly)) { var line = new String (f.readLine(f.size)); f.close(); } |
remove | |
Description | Tries to remove (delete) the file with filename and returns true if it succeeds. If no filename is given the filename set by the constructor or the name property is used. The file should not be open when trying to remove it. The filename must contain the full path to the file. |
Parameters | [filename : String = “”] |
Returns | Boolean |
Example | var f = new File('c:\\test.txt'); if (f.remove()) logger.logEvent(0, 'File deleted'); |
reset | |
Description | Seeks to the start of the file. Returns true on success; otherwise returns false (for example, if the file is not open). |
Parameters | None |
Returns | Boolean |
Example | |
seek | |
Description | This function sets the current position to pos, returning true on success, or false if an error occurred. |
Parameters | pos : Integer |
Returns | Boolean |
Example | |
setCodec | |
Description | Sets the codec to use in the readLine(), write() and readAllText() methods. |
Parameters | codecname : String |
Returns | Boolean |
Example | var f = new File('c:\\test.txt'); f.setCodec('UTF8'); if (f.open(IO_ReadOnly)) { var text = new String (f.readAllText()); f.close(); } |
write | |
Description | Writes the given text to the file. The file must be open to be able to write to it. |
Parameters | text : String |
Returns | None |
Example | var f = new File('c:\\test.txt'); if (f.open(IO_WriteOnly)) { f.write('Text to write in my file'); f.close(); } |
Example | /* (f.open(IO_Append)) was required to append text to a file, in older versions of Lasernet. f.open(IO_WriteOnly+IO_Append)) is required to append text to a file in Lasernet 10 and newer */ var f = new File('c:\\test.txt'); if (f.open(IO_WriteOnly+IO_Append)) { f.write('Text to append in end of my file'); f.close(); } |
writeBinary | |
Description | Writes the given data to the file. The file must be open to be able to write to it. This function is usually used to write the contents of a job to a file. |
Parameters | data : ByteArray |
Returns | None |
Example | var f = new File('c:\\test.txt'); if (f.open(IO_WriteOnly)) { f.writeBinary(job.getJobInfoBinary("JobData")); f.close(); } This set of scripts can be easily replaced by a global script function; saveJobInfoToFile(jobInfoName, fileName); Or a static function call; File.writeBinary('c:\\test.txt', job.getJobInfoBinary('JobData')); |
Zip
Description
The Zip class is used to create and manipulate zipfiles from script and is meant as an alternative or supplement to the Zip modifier, which is somewhat limited in its functionality. The zip file is created and manipulated in memory, which sets some limitations on file size.
Properties
count | |
Description | The number of files in the zip file. |
Access | Read |
Returns | Number |
Example | |
type | |
Description | Always returns the value ‘Zip’. |
Access | Read |
Returns | String |
Example | |
Methods
addFile | |
Description | Adds the given data to the zip file with the given filename. |
Parameters | Data : ByteArray Filename : String |
Returns | none |
Example | var zipper = new Zip(); zipper.newZipFile(); zipper.addFile(job.jobdata, "preview.pdf"); |
addFileFromDisk | |
Description | Adds a file with the given filename from disk into the zip file. It is given the filenameInZip in the zipfile. |
Parameters | filename : String filenameInZip: String |
Returns | None |
Example | var zipper = new Zip(); zipper.newZipFile(); zipper.addFileInZip("c:\\preview.pdf", "preview.pdf"); |
extractArchiveToDisk | |
Description | Extracts all the files in the archive to disk. The given filepath is used as root for the files, which all are extracted with full path. |
Parameters | filepath : String |
Returns | Boolean |
Example | var zipper = new Zip(); if (zipper.loadZipFileFromDisk("d:\MyZipFile.zip")) { if (zipper.extractArchiveToDisk("c:\\extractfolder")) // extract OK } |
extractFileToDisk | |
Description | Extracts a file from the archive onto disk. All the files are numbered from 0 to count -1. |
Parameters | Index : Number filepath : String |
Returns | Boolean |
Example | ... if (zipper.extractFileToDisk(1, "c:\\extractfolder")) // extract OK |
getZipFile | |
Description | Returns the entire zip file as a ByteArray. You can use this to put the zip file into a JobInfo. |
Parameters | None |
Returns | ByteArray |
Example | var zipper = new Zip(); ... job.setJobData(zipper.getZipFile()); |
loadZipFileFromDisk | |
Description | Loads the zip file from disk with the given filename. Returns true if the zip file was successfully loaded. |
Parameters | Filename : String |
Returns | Boolean |
Example | var zipper = new Zip(); zipper.loadZipFileFromDisk("d:\MyZipFile.zip"); |
newZipFile | |
Description | Prepares a new zip file. This function should always be called before adding files to a new archive. |
Parameters | None |
Returns | None |
Example | var zipper = new Zip(); zipper.newZipFile(); |
saveZipFile | |
Description | Saves the zip file to disk. Uses the given filename for the zip file. |
Parameters | Filename : String |
Returns | None |
Example | var zipper = new Zip(); zipper.newZipFile(); zipper.addFile(job.getJobInfoBinary("JobData"), "preview.pdf"); zipper.saveZipFile("d:\\MyZipFile.zip"); |