# 2.5.1 Running Scripts

The following sections document various ways to execute and/or store LabTalk scripts. We begin by examining the relationship between scripts and the objects they work on.

 This section covers the following topics:

Active Window Default

When working on an Origin Object, like a workbook or graph page, a script always operates on the active window by default. If the window is inactive, you may use win -a to activate it.

win -a book2;		// Activate the window named book2
col(b) = {1:10};	// Fill 1 to 10 on column B of book2

However, working on active windows with win -a may not be stable. In the execution sequence of the script, switching active windows or layers may have delay and may lead to unpredictable outcome.

It is preferable to always use win -o winName {script} to enclose the script, then Origin will temporarily set the window you specified as the active window (internally) and execute the enclosed script exclusively on that window. For example, the following code will create a new project, fill the default book with some data, and make a plot and then go back to add a new sheet into that book and make a second plot with the data from the second sheet:

doc -s;doc -n;//new project with default worksheet
string bk$=%H;//save its book short name //fill some data and make new plot wks.ncols=2;col(1)=data(1,10);col(2)=normal(10); plotxy (1,2) o:=<new>; //now the newly created graph is the active window //but we want to run some script on the original workbook win -o bk$ {
newsheet xy:="XYY";
col(1)=data(0,1,0.1);col(2)=col(1)*2;col(3)=col(1)*3;
plotxy (1,2:3) plot:=200 o:=<new>;
}

Please note that win -o is the only LabTalk command that allows a string variable to be used. As seen above, we did not have to write

win -o %(bk\$)

as this particular command is used so often that it has been modified since Origin 8.0 to allow string variables. In all other places you must use the %( ) substitution notation if a string variable is used as an argument to a LabTalk command.

Where to Run LabTalk Scripts

While there are many places in Origin that scripts can be stored and run, they are not all equally likely. The following sub-sections have been arranged in an assumed order of prevalence based on typical use.

The first two, on (1) Running Scripts from the Script and Command Windows and (2) Running Scripts from Files, will be used much more often than the others for those who primarily script. If you only read two sub-sections in this chapter, it should be those. The others can be read on an as-needed basis.