Sunday, 9 December 2012

Adding a C# REPL Script to Windows Live Writer

Let’s give Windows Live Writer 'Copy and Paste of images' feature a test-drive by documenting how to inject a C# REPL script into it

Using the latest (4.5.2) version of the O2 Platform image

click on the Find an O2 Script  image search for Inject O2 image

and invoke/execute the Util - Inject O2 into other processes.h2 script which looks like this when opened:

 image.

Select the WindowsLiveWriter  image  process

This will open the  C# REPL script editor in the selected process, which at the moment is the Live Writer instance that I’m writing this blog post on :) )

image

Since the C# script is running in the same process (and CLR) than LiveWriter, we can use
return Application.OpenForms;

to get a reference to the currently opened Forms (the first is LiveWriter, the 2nd is the O2 C# REPL Editor )

 image

We can use
var form = (Form)Application.OpenForms.first();
return form;

to get strongly typed reference to LiveWriter Form object

image

which we can use to (for example) change the Form’s title using the PropertyGrid (note the extra AAAAA on the title):

image

The title can also be changed programmatically using:
var form = (Form)Application.OpenForms.first();
form.set_Text("We can set the Form title from here too");
return form;

Which when executed will also change the Live Writer title

image

We can find the full name of the Form type:
var form = (Form)Application.OpenForms.first();
return form.GetType().FullName;

image

And its Assembly
var form = (Form)Application.OpenForms.first();
return form.GetType().Assembly.ToString();

image

With this info we can better cast of the OpenForm object:
var form = (PostEditorForm)Application.OpenForms.first();
return form;

//using WindowsLive.Writer.PostEditor
//O2Ref:WindowsLive.Writer.PostEditor.dll 

But before we add a couple more dependencies needed to compile the script:

image

Which when added:
var postEditorForm = (PostEditorForm)Application.OpenForms.first();
return postEditorForm;

//using WindowsLive.Writer.PostEditor
//O2Ref:WindowsLive.Writer.PostEditor.dll
//O2Ref:WindowsLive.Writer.ApplicationFramework.dll
//O2Ref:WindowsLive.Writer.CoreServices.dll
//O2Ref:WindowsLive.Writer.Controls.dll

will compile OK and give us an instance of the PostEditorForm class:

image

Another example of what we can do is to access the MainMenu (note that we have access to the entire GUI and its objects)
var postEditorForm = (PostEditorForm)Application.OpenForms.first();
return postEditorForm.mainMenu().items();

//using WindowsLive.Writer.PostEditor
//O2Ref:WindowsLive.Writer.PostEditor.dll
//O2Ref:WindowsLive.Writer.ApplicationFramework.dll
//O2Ref:WindowsLive.Writer.CoreServices.dll
//O2Ref:WindowsLive.Writer.Controls.dll

Note how (in the screenshot below) the Help menu was hidden (by setting the Visible property to false)

image

We can add a new top-level menu
var postEditorForm = (PostEditorForm)Application.OpenForms.first();
postEditorForm.mainMenu()
    .add_Menu("O2")
        .add_MenuItem("Say hello", ()=> "Hello".alert())
        .add_MenuItem("REPL this form", ()=> postEditorForm.script_Me());
return "done";
 
//using WindowsLive.Writer.PostEditor
//O2Ref:WindowsLive.Writer.PostEditor.dll
//O2Ref:WindowsLive.Writer.ApplicationFramework.dll
//O2Ref:WindowsLive.Writer.CoreServices.dll
//O2Ref:WindowsLive.Writer.Controls.dll


image

which can be used

image

say Hello

image

or open a C# REPL

image

with the instance of the PostEditorForm class:

image

Note that this REPL will also need the extra compile references:
return postEditorForm;

//O2Ref:WindowsLive.Writer.PostEditor.dll
//O2Ref:WindowsLive.Writer.PostEditor.dll
//O2Ref:WindowsLive.Writer.ApplicationFramework.dll
//O2Ref:WindowsLive.Writer.CoreServices.dll
//O2Ref:WindowsLive.Writer.Controls.dll

image

Here is the code snippet to access all controls that current exist in this form:
return postEditorForm.controls(true);

//O2Ref:WindowsLive.Writer.PostEditor.dll
//O2Ref:WindowsLive.Writer.PostEditor.dll
//O2Ref:WindowsLive.Writer.ApplicationFramework.dll
//O2Ref:WindowsLive.Writer.CoreServices.dll
//O2Ref:WindowsLive.Writer.Controls.dll

image

And batch change the background of all those controls to pink:

image

A final example is to add an C# REPL script to the actual GUI (at the bottom) which can be done using:
postEditorForm.insert_Script();

//O2Ref:WindowsLive.Writer.PostEditor.dll
//O2Ref:WindowsLive.Writer.PostEditor.dll
//O2Ref:WindowsLive.Writer.ApplicationFramework.dll
//O2Ref:WindowsLive.Writer.CoreServices.dll
//O2Ref:WindowsLive.Writer.Controls.dll

Giving us a faster way to write the C# scripts:

image
Post a Comment