Your First Script
Hello World
Creating your first simple script
Hello world¶
Now, let's make a hello world example as a script for Fast Video Cataloger.
We write the code inside the console window.
First, we need to get hold of the interface to the console window,
In the scripting documentation (open with the help button), you can click on the "Video Cataloger" page to get all interfaces available.
Using the passed-inIScriptinginterface, we want to get the IConsole interface. We can do this with the following line:
csharp
IConsole console = scripting.GetConsole();
The console variable is of the IConsole type, i.e., you can call allIConsolefunctions from the console variable. = is to assign the value, and to the right of the =, we call GetConsole from the scripting interface. GetConsole returns an object that implements the interface. And, as always in C#, we need to end the statement with a semicolon character.
Again, looking at the documentation forIConsole, we see it has a member function called WriteLine.
To write text, we call the function like this:
csharp
console->WriteLine("Hello World");
And the complete script is now:
csharp
using System.Runtime;
using System.Collections.Generic;
using VideoCataloger;
using VideoCataloger.RemoteCatalogService;
class Script
{
static public async System.Threading.Tasks.Task Run ( IScripting scripting, string arguments )
{
IConsole console = scripting.GetConsole();
console->WriteLine("Hello World");
}
}
Click the Run button to execute the script and you should see Hello World printed to the output in the script window.
Let's instead put the text in a popup window.
The scripting environment has access to the .Net 4.8 framework. The .Net framework has tons of functions for string manipulations, OS access, and anything else you might need for desktop application development.
You do not need to do anything special to bring in the .Net framework. We will use the MessageBox that is in the System.Windows namespace. At the following code to your run function
csharp
System.Windows.MessageBox.Show("Hello World");
And click the Run button to run the script. This will show a popup window with the Hello world text.
Now, finally, let's use the argument that gets passed into the run function string arguments. To combine two strings in C#, we can use the + operator. Write the following:
csharp
System.Windows.MessageBox.Show("Hello" + arguments);
Write your name in the text box next to Run and click the Run button. You should now see a "Hello [Your name]" text box.