Using the latest (4.5.2) version of the O2 Platform
click on the Find an O2 Script search for Inject O2
and invoke/execute the Util - Inject O2 into other processes.h2 script which looks like this when opened:
.
Select the WindowsLiveWriter 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 :) )
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 )
We can use
var form = (Form)Application.OpenForms.first();
return form;
to get strongly typed reference to LiveWriter Form object
which we can use to (for example) change the Form’s title using the PropertyGrid (note the extra AAAAA on the title):
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
We can find the full name of the Form type:
var form = (Form)Application.OpenForms.first();
return form.GetType().FullName;
And its Assembly
var form = (Form)Application.OpenForms.first();
return form.GetType().Assembly.ToString();
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:
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:
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)
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
which can be used
say Hello
or open a C# REPL
with the instance of the PostEditorForm class:
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
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
And batch change the background of all those controls to pink:
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: