2.14.1 Getting Numeric and String Input

This section gives examples of prompting for three types of user input during script execution:

  1. Yes/No response
  2. Single String
  3. Multi-Type Input (GetN)
 The User Interface Module (UIM) alllows users to build complex user interface controls. See the UIM Objects page.

Get a Yes/No Response

The GetYesNo command can be used to get a Yes or No response from the user. The command takes three arguments:

Syntax: getyesno stringMessageToUser numericVariableName windowTitle

For example, entering the following line in the Script Window will generate a pop-up window titled Check Sign of X and ask the user the Yes/No question Should X be positive? with the options Yes, No, and Cancel as clickable buttons. If Yes is selected, xpos will be assigned a value of 1. If No is selected, xpos will be assigned the value 0. If Cancel is selected, xpos will be assigned the value 0, #Command Error! will be printed, and script execution will stop.

getyesno "Should X be positive?" xpos "Check Sign of X"

If additional script processing is required in any event, this command should be called from elsewhere and the numeric value can be tested. In the following example, getyesno is called from its own section of code and the two string inputs are passed as arguments to the section(note, a multi-section LabTalk script will not work if simply pasted to the script window; save to file and run):

[Main]
// Here is the calling code
int iVal = -1;
run.section(,myGetYesNo,"Create a Graph of results?" "Graphing Option");
if( iVal > 0 )
{
   type "Graph generated";        // Yes response 
}
else
{
   type "Graph NOT generated";    // No or Cancel response 
}

// 'myGetYesNo' section 
[myGetYesNo]
getyesno (%1) iVal (%2);

Get a String

GetString can be used for user entry of a single string.

%B = "";
GetString (Enter as Last, First) Last (Your Name);
// Cancel stops here unless using technique as in GetYesNo
if("%B"!="Last")
{
    type User entered %B.;
}
else
{
    type User clicked OK, but did not modify text;
}

Get Multiple Values

The GetN or GetNumber dialog prompts a user for a number, a string or a list entry (in previous versions of Origin only numeric values were possible, hence the name). Starting with Origin 8.1, GetNumber will accept both string variables (i.e., string str1$) and string registers (i.e., %A) for string input. Previous versions support string registers only. GetN currently accepts up to 7 variables in addition to the dialog title.

With the increased functionality of GetN in Origin 8.1, string variables can be used in the command call. In this case, the strings must first be declared. It is always a good practice to create variables by declaration rather than by assignment alone; for more see Scope of (String) Variables. For example:

// First, declare the variables to be used:
double nn = 3.2;
string measurement$="length", units$="inches", event$="Experiment #2";

// Use GetN dialog to collect user data:
getn
(Value) nn 
(Measurement Type) measurement$
(Units) units$
(Event Name) event$
(Dialog Title);

brings up the following dialog, prompting the user for input:

GetN Dialog.png

The values entered in this dialog will be assigned to the declared variables. If the variables have an initial value (before GetN is called), that value will show up in the input box, otherwise the input box will appear blank. In either case, the initial value can be changed or kept.

To check the data entered, run the following line of script:

// Output the data:
type In %(event$), the %(measurement$) was $(nn) %(units$);

This next example script assumes a Graph is the active window and prompts for information then draws a line and labels it. The call to GetN uses string registers and pre-defined lists as inputs.

%A=Minimum;
iColor = 15;
dVal = 2.75;
iStyle = 2;

// Opens the GetN dialog ...
// The extra %-sign in front of %A interprets the string register 
// literally, instead of treating it as a variable name.
getn (Label Quantile) %%A 
(Color) iColor:@C 
(Style) iStyle:@D 
(Value) dVal 
(Set Quantile);
 
draw -n %A -l -h dVal;     // Draws a horzontal, named line
%A.color = iColor;         // Sets the line color
%A.linetype = iStyle;      // Sets the line style

// Creates a text label named QLabel at the right end of the
// line
label -s -a x2 dVal -n QLabel %A; 

%A.Connect(QLabel,1);       // Connects the two objects

Note : The script requires that %A should be a single word and that object QLabel does not exist.

The following character sequences, beginning with the @ character, access pre-defined lists for GetN arguments:

List Description
@B

List of Object Background attributes

@C

Basic Color List

@D

Line Style List

@P

Pattern List

@S

Font Size List

@T

Font List

@W

Line Width List

@Z

Symbol Size List

Note that the value returned when a list item is selected within the GetN dialog is the index of the item in the list. For instance, if one of your GetN entries is:

(Font Size) fs:@S

and you select 18 from the drop-down list in the dialog, the variable fs will hold the value 8, since 18 is the 8th item in the list.



Below is another example script that allows a user to change a Symbol Plot to a Line + Symbol Plot or the reverse:

get %C -z iSymbolSize;  // Get current Symbol Size
get %C -cl iLineColor;  // Get current Line color
iUseLine = 0;
// Now open the dialog to the user
getn (Symbol Size) iSymbolSize
     (Use Line) iUseLine:2s
     (Line Color) iLineColor:@C
     (Set Plot Style);
// If User asked for Line
if(iUseLine == 1)
{
    set %C -l 1;           // Turn on the line
    set %C -cl iLineColor; // Set the line color
}
// .. if not
else
    set %C -l 0;           // Turn off line
set %C -z iSymbolSize;     // Set Symbol size