Jscript Tutorial Wolfgang Unger and Tobias Sommer



Yüklə 0,62 Mb.
Pdf görüntüsü
səhifə3/21
tarix07.11.2018
ölçüsü0,62 Mb.
#78679
1   2   3   4   5   6   7   8   9   ...   21

the DoasConsole of DOASIS is started by C:\Programme\DOASIS\DoasConsole.exe and

jscript1.js (stored in C:\scripttest\jscript1.js) is executed. The command >>C:\logfile1.txt

is optional. It stores the output of the program, which is usually displayed in the Output-

Tab of DOASIS in the file C:\logfile1.txt. In case the output is stored in a file, it is not

shown while the program is executed. The suffix “.bat” is the identifier for the “batch”-file.

A batch file contains DOS commands which are executed successively.

Warning:

Make sure that the path name does not contain blanks since DOS does not support them.

Several JScript can be successively run simply by adding more lines to the batch file:

C:\Programme\DOASIS\DoasConsole.exe

C:\scripttest\jscript1.js

>>

C:\logfile1.txt



C:\Programme\DOASIS\DoasConsole.exe

C:\scripttest\jscript2.js

>>

C:\logfile2.txt



4.4

Starting a JScript automatically after a hardware

reboot

Measurements controlled by JScripts in remote areas often don’t have a reliable power



supply. After power is cut, the hardware is often designed in the way to cause a reboot when

power is back. In that case the JScript should start by itself after the reboot. This can be

done by creating a batch file as described in section 4.3.2 which is executed automatically

after the reboot. In the case of Windows XP, a shortcut of the batch file can be placed

in the Startup-folder (Start - Programs - Startup). For other operating systems, it should

work similarly.

4.5

Avoiding inexplicable program crashes



In the past, it happened quite often that a measurement JScript worked well for hours but

then suddenly crashed without any apparent reason. It can be that such crashes are due to

“bad” communication between the computer and the measurement device. Especially if the

electronics of the measurement instrument are fairly old it can happen that the instrument

gets response from the computer, even though it hasn’t yet finished its own sending proce-

dure. This can lead to a system crash.

In order to avoid this sort of error, “wait” functions should be implemented after each data

transfer. This makes the program more stable. How to implement wait functions is de-

scribed in section 8.3. By implementing wait functions in the script of section 12.6 the

script continued measuring for several months without interruption.

10



Part II

The basics of JScript

11



Chapter 5

First steps in JScript

5.1

A first example



Now let’s just start to write a JScript! For beginners, it is common to write a so-called

”Hello World”-program, which simply writes these words in the output. So that is what we

will do first. You can enter the code into the Mini-Script-Window, and then click on start.

Here is the code:

import System;

System.Console.WriteLine("Hello World!");

If you switch to the Output-Window, you’ll see the result. Congratulation to your first

JScript! But how did it work? There are some remarks I want to make here:

• First of all, we ”imported” the ”System”. This just means that we will make use of

certain functions made available by a ”namespace” (a kind of library) by that name.

Later on, we will import other namespaces, which will be then specific to DOASIS.

How else should the interpreter know how to deal with DOASIS-specific commands?!?

• Second, we produced on output by using System.Console.WriteLine(...);. The

object that we used as a parameter was a static string, which is characterized by the

quotation marks. We could also use other parameters, as our next example will show.

• In both commands, we had to finish the line with a semicolon. This is important,

because this tells the interpreter when a command ends. You could put the whole code

into one line, and the interpreter would still get it right because of the semicolons. But

never forget the semicolons, because this most certainly will produce an error!

5.2


A word on ”object-orientation”

You might have noticed the dots between the words of the second line of our first script. This

is typical for so-called object-oriented programming languages. If you don’t know anything

about object-orientation, don’t panic, you won’t need it anyway. The only thing you need

to know is that there is a kind of hierarchy of objects. The dot could be read as ”belongs

to”. In our example, the Console-object belongs to the System, and the WriteLine-Method

(a method is kind of function or sub-routine) belongs to the System.Console-object. You

12



need to mention all the names because there could be a WriteLine-method as part of another

object with different name. The interpreter wouldn’t find the method otherwise. This is all

about object-orientation so far. Wasn’t that difficult, right?

5.3


A second example

Now we want to get more professional, and instead of writing a mini script, we will edit the

code in an external file.

So please open your favorite text editor (e.g. PSPad, freeware, very good!) but not Word

etc., since they don’t save in a plain text format) and enter the following script:

import System;

import DoasCore.Spectra;

var Filename;

Filename = Specbar.CurrentSpectrum.Name;

System.Console.WriteLine(Filename);

Then save it as a ”filename.js”. Notice that ”.js” is the file extension for JScript. Now

you need to specify the file (with the whole path) at the Script-Tab. Alternatively you can

browse the file. Before you start the script, open your favorite spectrum in DOASIS. Now

please start the script. As you can see in the Output-Tab, the script returns the name of

your spectrum. So let us learn some new things from the script:

• This time, we not only imported the System, but also the DoasCore.Spectra names-

pace. In this namespace all the objects and methods are defined that deal with spectra.

• Then we defined a variable. In other program languages, you need to worry a lot

about different types of variables, not so in JScript!!! In the declaration of a variable,

no difference is made between integers or floats or strings. All you need to do is to

tell that you want to declare a variable. This is done by the var command. In our

script, we declared the variable ”Filename”, which is an arbitrary name and you could

have used (almost) anything else. Unfortunately, sometimes you need to worry about

types nevertheless. Once a variable contains data of a certain type (we assigned a

string to ”Filename”), you can only use it as a different type later on if a so-called

type-conversion is possible. We will come back to this point later on.

• Look what we did next: we assigned a value to our new defined variable, more precisely

the name of the current spectrum in the Specbar, i.e. the spectrum that you just

opened before starting the script. Assigning a value to a variable is pretty simple: just

use a =. The right side is then assigned to the left side. The left side therefore always

has to be a variable, the right side can also be a constant, an explicit expression (like

a number) or a more complicated algebraic expression, a string, or an array. We will

see other examples later on.

• Last, we just wrote the value of our variable in the output. And indeed, it is the name

of your spectrum, as it should.

Hint:


Scripts can also be started from the sheet ”Mini Scripts” of the

”console window”. Advantage: this is faster.

13



Yüklə 0,62 Mb.

Dostları ilə paylaş:
1   2   3   4   5   6   7   8   9   ...   21




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə