Scripting Guide > Display Trees > Construct Custom Windows > Set Function and Set Script
Publication date: 08/13/2020

Set Function and Set Script

You can use the Set Script message to have a display box control (for example, a button box or combo box) run a script when it is clicked with the mouse.

win = New Window( "Set Script Example",
	ex = Button Box( "Press me" )
ex << Set Script( Print( "Pressed" ) );

The preceding script prints the following text to the log when the button box is clicked:


Alternatively, you can use the Set Function message to have a display box control run a specific function where the first argument is the specific display box. Set Function enables you to build more object-oriented scripts and to create a larger system.

win = New Window( "Set Function Example",
	Button Box( "Press me",

// functions specified with Set Function get 'this' display box

		<< Set Function(
				this << Set Button Name( "Thanks" )

The preceding script creates a button box with the name “Press me”. When the button is clicked, the function called by Set Function changes the name to “Thanks”.

If you have multiple buttons that can be serviced by the same script, Set Function is much simpler than Set Script because it tells you which button was pressed. In this example, both check boxes use the script that begins on the first line.

f = Function( {this, idx}, // idx just changed

/* <<Get Items returns a list of all items; indexing that list with idx

is the name of the item that is changing */

		"\!n changing item=" || (this << Get Items())[idx] || " to " || Char(

// <<Get returns the new value of that item

	this << Get( idx ) ) ||
		"\!n new selection=" || Char( this << Get Selected ) || "\!n"
	)// <<Get Selected returns a list of the currently checked (selected) items
New Window( "Example",
	H List Box( // 'f' is the 'named' function, used twice
		V List Box( Text Box( "Column 1" ), Check Box( {"a", "b"}, <<Set Function( f ) ) ),
		Spacer Box( size( 50, 50 ) ),
		V List Box( Text Box( "Column 2" ), Check Box( {"c", "d"}, <<Set Function( f ) ) )

In the following example of Set Function, the same function is used to create the buttons.

New Window( "Cash Box",
	V List Box(
		H Center Box( TB = Text Box() ), // TB will show total of coins
		LB = Lineup Box( N Col( 3 ) ), // LB will hold buttons, added below
		H Center Box( // CLEAR button will reset the total
			Button Box( "CLEAR",
				total = 0;
				TB << Set Text( Char( total ) );
coins = {1, 5, 10, 25, 50, 100}; // coins is a list of the button labels
total = 0;

/* loop creates the buttons and shows that the same function

is used for each */

For( iButton = 1, iButton <= N Items( coins ), iButton++,
	LB << Append( Button Box( Char( coins[iButton] ), Set Function( buttonFunction ) ) )
buttonFunction = Function( {this},
	total = total + Num( this << Get Button Name );
	TB << Set Text( Char( total ) );

The function called for any button, except CLEAR, uses the button’s name to determine what to do. Rather than using the button’s name, you might want to use a sibling display box by this<<sib or perhaps (this<<parent)<<child to find the first sibling of this button.


The Set Script and Set Function messages work for button boxes, calendar boxes, check boxes, combo boxes, list boxes, popup boxes, radio boxes, range slider boxes, slider boxes, and spin boxes.

You cannot use both Set Script and Set Function at the same time. Use Set Function if you need to reference a specific display box object.

Want more information? Have questions? Get answers in the JMP User Community (