2006/12/09

元記事はhttp://winscript.s41.xrea.com/mt/archives/2005/09/post_5.htmlです。

クリップボードを扱う.NET Frameworkのクラスに、Clipboardクラスがあります。そのSetDataObjectメソッドを使うとクリップボードにテキストデータを格納できる、はずです。が、

[void] [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
[System.Windows.Forms.Clipboard]::SetDataObject("あああ")

のようなスクリプトを実行すると、

Exception calling "SetDataObject" with "1" argument(s): "OLE が呼び出される前に、現在のスレッドが Single Thread Apartment (STA) モードに設定されていなければなりません。Main 関数に STAThreadAttribute が設定されていることを確認してください。".

というエラーが出てしまいます。MSHでMain関数にSTAThreadAttributeを指定するにはどうすれば良いのでしょうか? ちなみに

[System.Threading.Thread]::CurrentThread.ApartmentState = 
[System.Threading.ApartmentState]::STA

のようにしてもSTAにはなってくれませんでした。

クリップボードアクセスは、newpopsさんが提案されている、IEのCOMコンポーネントを使う方法で可能になります。

PowerShell Memo - 文字列をクリップボードにコピーする
http://d.hatena.ne.jp/newpops/20061104/p1

元記事:http://blogs.wankuma.com/mutaguchi/archive/2006/12/09/49650.aspx

古い記事へ | 新しい記事へ


プライバシーポリシー

Twitter

Books