Hemme's Blog

Stories from behind the keyboard

  • RSS
  • Twitter
  • Linkedin

ScriptCs.Gui is now a standard script pack for ScriptCS. You can just grab it from NuGet with

C:\scriptcs -install ScriptCs.Gui

..and use it in REPL with:

> var gui = Require<Gui>();
>

ScriptCS-GUI has evolved a little bit. At the moment the following helpers are declared inside the static class Gui.

  • Gui.OpenFileDialog(): open a classic Open File Dialog and return the path of the selected file.
  • Gui.SaveFileDialog(): act similar to OpenFileDialog()...
  • Gui.PopUp(): open a pop-up and returns a form object. You can use its Sta.Enqueue(action) method to inject code in the form's main thread (e.g. to add controls at runtime) while working inside the console!
  • Gui.Drawing(): open a drawing form and returns the Graphics object you can use to draw on the form (the console stays responsive in this case too!)

Probably the most useful thing is that I've managed to open Windows Forms from the console while leaving the console "alive responsive" (tipically the console would (in-)act like a zombie after any Application.Run() over the form!). This means that now you can go on using the ScriptCS console and send any command to any window you created FROM the console. This was not obvious and I've got lots of ideas about how to implement ScriptCS-GUI further! - Stay tuned :)

I'm going to write more about these in my next post. For now you can just launch the interactive script "gui-demo.csx", run it and - *hopefully* - send me some feedback :)

The interactive script "gui-demo.csx" lets you play with classic Windows Forms while the console stays responsive!

ScriptCs is a great yet simple to use framework for writing and executing C# scripts.

TL;DR
Chocolatey + NuGet + Roslyn = ScriptCs
ScriptCs + Command Prompt = C# REPL

Beeing able to write and freely execute C# scripts is a great opportunity and can be a time saver. What I lack more at this stage of development of ScriptCs is a simple toolset to implement basic user interactions. I've sterted a new project on GitHub called ScriptCS-GUI. At the moment it holds some vanilla C# scripts but I wish to evolve it to a full featured Script Pack.

As a starter I implemented classic interactions such showing an OpenFileDialog and a SaveFileDialog. In the following example the Console asks the user to select a file and then writes the selected file name.

> #load "gui.csx" // <-- load gui.csx
> var file = Gui.OpenFileDialog(); // <-- show a classic Open File dialog and wait the user to pick a file
> file // <-- write the selected file name on console
C:\temp\foobar.txt

The following script shows you how to read a folder name from the command argument and use it with the Gui.OpenFileDialog helper.

#load "../script/gui.csx"
// Run with:
// scriptcs -script "{filename}.csx" -- c:\some-path

var folder = ScriptArgs.FirstOrDefault(); // <-- the starting folder name
var path = Gui.OpenFileDialog(folder);
Console.WriteLine("You selected: '{0}'.", path ?? "nothing");

Launch the script as shown in the picture and enjoy your interaction... :)