Using IronPython from PowerShell Part 2 : Clipboard (STA mode)
This is the Second part of a 6 part series about using an Hosted IronPython Engine from PowerShell to do some tasks PowerShell does not yet support in V1.
In PowerShell you can very easy host a IronPython engine just by loading the DLL's , see this post on my old blog PowerShell : Hosting IronPython
I use it so solve some shortcomings in PowerShell 1.0 as Async Event watching, Threading support and using controls that need STA mode.
I do use it so much that I added the following lines to my profile to load them standard, so I have always a IronPython engine handy.
[void][System.Reflection.Assembly]::LoadFile("$pshome\IronPython.dll")
[void][System.Reflection.Assembly]::LoadFile("$pshome\IronMath.dll")
$ipe = new-object IronPython.Hosting.PythonEngine
In the last post Using IronPython from PowerShell Part 1 : Watch folder for changes without blocking Console did show how to use IronPython with the Filesystem watcher.
In this post I will Show how to Use IronPython to launce a new Thread in STA mode to use the Clipboard, also I show how we can add a Global variable to the IronPython Runsspace from PowerShell and how the Change it.
this is needed as the Clipboard needs STA mode to work ,for more info about this, see the following posts on my old blog :
Problem description : /\/\o\/\/ PowerShelled: playing with MY-object from MSH
workarounds : /\/\o\/\/ PowerShelled: MSH Clipboard use workaround (workaround hosting VB.NET )
and PowerShelled- MSH Clipboard part 2 (of 3 -) (using VB.NET DLL) )
As you can see in the examples later, as we can set variables in the IronPython runspace very easy, it is easy to interact with the IronPython part to set the text, as we have access to the runtime we can just change the global variable from PowerShell, this makes IronPython very handy for this kind of tasks.
As this enables us to load a static IronPython script, and just change the Global variable Text before executing,
The Code for this (IronPython DLL's must be loaded and $IPE IronPython Engine initialized see code above ) :
$ClipCode = @'
import clr
import System
from System.Threading import Thread
from System.Threading import ThreadStart
clr.AddReferenceByPartialName("System.Windows.Forms")
def thread_proc():
System.Windows.Forms.Clipboard.SetText(text)
t = Thread(ThreadStart(thread_proc))
t.ApartmentState = System.Threading.ApartmentState.STA
t.Start()
'@
$ipe.Globals.Add("text",'')
Function Set-Clipboard ($Text){
$ipe.Globals["text"] = $Text
$ipe.Execute($ClipCode)
}
After loading this code, you can just run
Set-Clipboard "Hello Clip"
to set the Clipboard
PoSH> $code = @'
>> import clr
>> import System
>> from System.Threading import Thread
>> from System.Threading import ThreadStart
>> clr.AddReferenceByPartialName("System.Windows.Forms")
>> def thread_proc():
>> System.Windows.Forms.Clipboard.SetText(text)
>>
>> t = Thread(ThreadStart(thread_proc))
>> t.ApartmentState = System.Threading.ApartmentState.STA
>> t.Start()
>> '@
>>
PoSH> $ipe.Globals.Add("text",'')
PoSH> $ipe.Globals["text"] = "Clip This"
PoSH> $ipe.Execute($code)
PoSH>
PoSH> Function Set-Clipboard ($Text){
>> $ipe.Globals["text"] = $Text
>> $ipe.Execute($ClipCode)
>> }
>>
PoSH> Set-Clipboard "Hello Clip"
PoSH> Hello Clip
In the Next post a bit more about Threading and Forms, in the part after that more information about STA and other Controls that need STA mode.
enjoy,
Greetings /\/\o\/\/