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, #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 Main section of code and two string inputs are passed as arguments to the section.

// 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 
   type "Graph NOT generated";    // No or Cancel response 

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

Because the above LabTalk Script contains two sections, Main and myGetYesNo, it can't run directly in Script window. Please save it as an OGS file and then type run.section(test.ogs,main) and press ENTER key to run it in Script window. See Sections in an OGS File.

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
    type User entered %B.;
    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, a list entry, or a control selection (in early versions of Origin only numeric values were possible, hence the name). Prior to version 8.1, GetNumber supported only string registers (e.g. %A) for string input. In Origin 8.1 and later versions, GetNumber accepts both string variables (e.g. string str1$) and string registers. GetN currently accepts up to 50 variables in addition to the dialog title.

Note that 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:
(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.

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
    set %C -l 0;           // Turn off line
set %C -z iSymbolSize;     // Set Symbol size