2.1 Getting Started with LabTalk
Hello WorldHello World
We begin with a classic example to show you how to execute LabTalk script.
- Open Origin, and from the Window menu, select the Script Window option. The Script Window will open.
- In this window, type the following text and then press Enter:
type "Hello World"
Here we used LabTalk command type. Commands can be abbreviated down to 2 characters, try:
ty "Hello World"
Origin will output the text Hello World directly beneath your command.
Note that when you press Enter, Origin adds a semicolon, ;, at the end of the line and also executes that line of script.
To repeat the execution of a line of script, place the cursor anywhere within the line and press Enter. If you place the cursor at the end of the line, you need to remove the ; before pressing Enter to execute that line of script.
Now let us see how to execute multiple lines of script from the script window:
- With a workbook window active in Origin, open the Script Window
- Type the following lines of script in the script window. At the end of each line, press Enter after typing the ;. This will prevent execution of the line. We will later execute all lines together.
type "The current workbook is %h";
type "This book has $(page.nlayers) sheet(s)";
type "There are $(wks.ncols) columns in the active sheet";
- Using the mouse, drag and select all lines of script. If using keyboard, place the cursor at the beginning of the script, then hold down the Shift key, and use the arrow keys to highlight all lines.
- Press Enter to execute all selected lines of script. Depending on the workbook that was active, the output in the script window will be similar to the following text:
The current workbook is Book1
This book has 1 sheet(s)
There are 2 columns in the active sheet
In the above example, we used the %H String Register that holds the currently active window name (which could be a workbook, a matrix, or a graph). We then used the page and wks LabTalk Objects to get the number of sheets in the book and the number of columns in the sheet. The $() is a substitution notations which tells Origin to evaluate the expression within the () and return its value.
If you are typing in multiple lines of script in the Script Window, you can add a ; at the end of a line and then press Enter to avoid execution of the line. This allows you to type in multiple lines without executing each line. You can then select all lines and press Enter to execute them all.
Using = to Get Quick OutputQuick Output
The script window can be used as a calculator to return results interactively. Type the following script in the script window and press Enter:
3 + 5 =
Origin computes the result and displays it in the next line:
3 + 5 = 8
The = character is typically used as an assignment operator, with a left- and right-hand side. When the right-hand side is missing, the interpreter will evaluate the expression on the left of the = character and print the result in the script window.
In the following example, we introduce the concept of variables in LabTalk. Entering the following assignment statement in the script window:
double A = 2
creates a variable A and initializes its value to 2. Then you can perform some arithmetic on variable A, such as multiplying by PI (a constant defined in Origin, ) and assign the result back to A:
A = A*PI
To display the current value of A, type:
Press Enter and Origin responds with:
A = 6.2831853071796
In addition, there are List command to view a list of variables and their values. Type the following command and press Enter:
Origin will open the LabTalk Variables and Functions dialog that lists all variables.
You can also get a dump of a specific type of variables, for example
to list the numeric variables.
Simple Useful CommandsSimple Commands
newbook; //create a workbook
wks.ncols=3; //set worksheet has 3 columns
col(B)=2*col(A); //set col(B)'s value by col(A)
//select a Y column firstly
plotxy; //use the selected Y column and its X column to plot
layer.x.from=5; //set X axis' from value
Other Ways to Execute Script
In previous examples, you saw how to execute script from the Script Window. Origin provides several other ways to organize and execute LabTalk script. These are outlined in detail in the Running and Debugging LabTalk Scripts chapter. Here we take a quick look at a few of the methods to execute script: (1) from the Custom Routine toolbar button, (2) from a custom menu item, and (3) from a button in a graph page.
Custom RoutineCustom Routine Button
Origin provides a convenient way to save script and run it with the push of a toolbar button.
- While holding down Ctrl+Shift on your keyboard, press the Custom Routine button ( ) located in the Standard Toolbar.
- This opens Code Builder, Origin's native script editor. The file being edited is called Custom.ogs. The code has one section, [Main], and contains one line of script:
type -b $General.Userbutton;
Replace that line with the following:
type -b "Hello World";
Then press the Save ( ) button in the Code Builder window.
- Now go back to the Origin application window and click the button (keyboard shortcut: ALT + F5).
Origin will again output the text Hello World, but this time, because of the -b switch used with the type command, the text will be presented in a pop-up window.
Custom Menu Item
LabTalk script can be executed from a custom menu item.
- Select the menu item Preferences: Custom Menu Organizer... to open the Custom Menu Organizer dialog.
- Make the Add Custom Menu tab active. Then right-click inside the left panel and select New Main Popup from the context menu.
- In the right panel, enter a name for Popup Text, such as My Menu. then click outside of the edit box.
- Select My Menu from the left panel, and then right click on it, and select Add Item from the context menu.
- In the right panel, change the Item Text to Hello World, then add the following script to the LabTalk Script text box:
type -b "Hello World";
- Click the Close button, and in the window that pops up, press Yes to save the menu changes as Default menu. In the file dialog that opens, press Save to save the file with the default name to the default folder (User Files Folder).
- A new menu named My Menu should now appear in the menu bar, to the left of the Window menu. Click on this new menu item, and then click on the Hello World entry in the drop-down. A Hello World dialog will pop up.
Button in a Graph
Origin also provides the ability to add a button to a graph or worksheet, and then execute LabTalk script by pushing that button. This allows for script to be saved with a specific project or window.
- Press the New Graph button ( ) located in the Standard Toolbar to create a new graph.
- Press the Text Tool button ( ) in the Tools Toolbar, and then click on the newly created graph and type the text My Button. Then click outside the text to finish editing the text.
- Right click on the text to bring up the context menu, and then select Programming Control to open the Programming Control dialog.
- In the dialog, select Button Up from the Script, Run After: drop-down list, and then type the following script in the edit box:
type -b "Hello World";
- Click OK to close the dialog. Now the text label becomes a button. Click the button. A Hello World dialog will pop up.
We now present a script example that walks you through a particular scenario of importing and processing data, and then saving the project. This example uses several LabTalk language features such as Commands, Objects, and X-Functions. You will learn more details about these language features in subsequent chapters.
NOTE: We will use the Script Window to execute these statements. To execute a single line of code, make sure that you leave out the ; at the end before pressing Enter. For multiple lines of code, at the end of each line, press Enter after the ; to continue entering the next line. After you have typed in all lines, select them all and then press Enter to execute.
Let's start with a new project using the doc command and the -n switch. If the current project needs saving, this command will prompt user to save.
Now let's import a data file from the Samples folder. We will first use the dlgfile X-function to locate the desired file:
Then select the file S15-125-03.dat from the \Samples\Import and Export sub folder located in your Origin installation folder, and click Open.
The above process will load the file path and name into a variable named fname$. You can examine the value of this variable by typing:
Now let's import this files into the active workbook. We will use the impasc X-Function with options to control naming, so that the file name does not get assigned to the workbook:
Now we want to perform some data processing of the Position column. We first define a range variable to point to this column.:
range rpos = "Position"
Since the column we select is also the 4th column in the current worksheet, we can also use index number to specify it.
range rpos = 4
You can check what range variables are currently defined, using this command:
We now normalize the column so that the values go from 0 to 100. To check what X-Functions are available for normalization, we can use the command:
The above command will dump X-Functions where the name contains norm. There are several X-Functions for normalizing data. For our current purpose we will use the rnormalize X-Function.
To get help on the syntax for this particular X-Function, you can type:
to dump the information, or type:
to open the help file.
Let us now normalize the position column:
rnormalize irng:=rpos method:=range100 orng:=<input>
The normalized data will be placed in the same column, replacing the original data, as we set the output range variable orng to be <input>.
When using X-Functions, you can leave out the variable names, if they are specified in the correct order. The above line of code can therefore be written as:
rnormalize rpos range100 orng:=<input>
The reason we still specified the name orng is because there are other variables that precede this particular variable, which are not relevant to our current calculation and were therefore not included in the command.
Now let's do some changes to the folder in the project. There are several X-Functions for managing project folders, and we will use some of them:
// Get the name of the current worksheet
string name$ = wks.name$;
// go to root folder
// rename Folder1 to be the same as worksheet
pe_rename Folder1 name$;
Now let's list all the sub folders and workbooks under the root folder:
Finally, let's save the Origin Project to the User Files Folder. The location of the user files folder is stored in the string register %Y. You can examine where your User Files Folder is by checking this variable:
Now let's use the save command to save our project to User Files Folder, with the name MyProject.opj.
In the above command %Y will be replaced with the User Files Folder path, and thus our project will be saved in the correct location.
Using CTRL + SHIFT to capture Menu Command and Toolbar Button Script
Many of the actions of menu commands and toolbar buttons in Origin's graphical user interface (GUI) are implemented in LabTalk script. When this is the case, you can locate and view this script in the following way:
- Press CTRL + SHIFT on your keyboard and hold.
- Click on the menu command or toolbar button.
This, then, becomes a source of usable LabTalk Script that you can incorporate into your custom routines.
- Open a new workbook, select columns A and B, then right-click and choose Fill Columns With: Row Numbers.
- With your worksheet active, press CTRL + SHIFT and click the Scatter toolbar button (or choose Plot > 2D: Scatter: Scatter).
This does two things:
- Opens Code Builder (Origin's Integrated Development Environment) to the line in Plot.ogs (an Origin system script file) to the section that executes the toolbar button or menu command.
- Writes the menu id and run.section command out to the Script Window.
Menu id=33248 (0x81e0)
Information from both of these lines of script can be reused in your own scripts, to create a line plot. To see this at work, make sure that the workbook with your two columns of highlighted row numbers is still active, then go to the Script Window (Window: Script Window), type either of the following, then press Enter:
menu -e 33248
For more information, see these topics:
Where to Go from Here?
The answer to this question is the subject of the rest of the LabTalk Scripting Guide. The examples above only scratch the surface, but have hopefully provided enough information for you to get a quick start and excited to learn more. The next chapter lists various resources available for learning LabTalk.