Scripting Command Documentation
Introduction
Sportscode and Studiocode have a set of scripting commands available in code and statistical windows. The scripting commands are based on a language specific to Sportscode and Studiocode. These commands work very much the same as the formulas and functions found in Excel or Numbers. Information that is stored in a timeline, button, or statistical window can be called on in a script to generate powerful results. The commands offer a wide variety of features ranging from simple instance and label counting to sophisticated conditional button renaming and color changes. There are even commands that can push buttons down or up to help automate coding!
This documentation provides the definitions of these commands and some basic examples on how to use them.
Requirements
This documentation applies to:
- Sportscode Elite and Pro versions 9.0 and above
- Studiocode versions 5.0 and above
Getting Started
Like any language, learning to script requires a bit of study and a lot of trial and error. It takes time to learn and get comfortable with the array of commands at your disposal, the syntax required to make them work properly, and the data they return. If you have never scripted before, don't worry, it is really easy to achieve some simple, yet effective results.
The most simple command and one you will use quite frequently is the SHOW command. SHOW does exactly what it's name implies, it shows something you tell it to show. Let's go through this command using a most famous example "Hello World!" and a few other fun calculator scripts.
We will do this work in a Code window, so open up a new code window by choosing File > New > Code window from the main menu along the top of the screen. These scripts can be created in the Statistical window also.
We recommend adding the Execute button to the Code window toolbar to make life easier when writing your scripts. To do this, right click on the toolbar and choose Customize Toolbar... from the menu, then click and drag the Execute button into position on the toolbar. Now let's write some scripts.
- Add a new button to the code window by clicking and dragging from the button icon in the toolbar.
- Double click on the button to open the Inspector.
- Select the Appearance panel in the Inspector.
- Tick the Show output option. A question mark will likely appear in the button below its name. This where the script output will appear when the script is executed. You might, also, want to resize the button now to make your output appear in the bounds of the button.
- Select the Script Editor panel in the Inspector.
- In the Script panel, type show "Hello World!"
- Hit the Execute button in the toolbar!
If you followed the steps correctly, you will see Hello World! in the button. Pretty simple, huh? Note that commands are not case sensitive, you can use SHOW or show. It is all a matter of style and what you think is more readable.
An important aspect to understand in the script above is that the text "Hello World!" following the command is what is known as a string. A string is a type of data that contains a sequence of alphanumeric characters. Don't forget to include the quotes around strings, otherwise you will get an error.
Another type of data is a number, there are various types of numbers, but for purposes of this documentation, we will stick with just calling them numbers. Numbers can be used in various operations such as addition, subtraction, multiplication and division. Let's see how numbers work with the show command using the same button as in the example above.
- Remove all the text in the Script Editor in the Inspector.
- Type show 2 + 2
- Hit the Execute button in the toolbar!
Again if you followed the steps correctly, you should see 4 in the button. Look at that, a simple calculator! More operators can be included in the command, so you could have show 10 - 8 * 2 / 2, but make sure to only use numbers. If you mix types incorrectly you will get an error.
Now, let's get a little fancier and put together a sentence using both strings and numbers to provide some nice meaningful output. We will do this using what is known as concatentation. Again, using the same button as above.
- Clear the Script Editor
- Type show "Calculated results = " + (10 - 8 * 2 / 2)
- Hit the Execute button in the toolbar!
The button will show Calculated results = 2 string. Let's examine this script as it introduces a couple of new things.
The first one is the plus symbol following the "Calculated results = ". The plus symbol is how we concatentate strings together. A simple example of this is show "I am " + "enjoying learning " + "scripting.". You will find this to be very valuable when you want to insert variables into the middle of a sentence.
The second is the use of parentheses around the mathemetical expression (10 - 8 * 2 / 2), the parentheses encapsulate the expression, so it is calculated, returned as a string, then concatenated. If these were not there, the script would fail to work.
Now that you have good idea on how the show command works, take a look at all the other commands and dive into scripting!