WSH よりクリップボードにアクセスすることはできない。しかし、調べてみたところ、Internet Explorer がクリップボードにアクセスするためのインターフェースを公開していることがわかった(常套手段?)。
そんなわけで、文字列をコピー & 取得する部分をクラス化した。
使い方はこんな感じで。
var clipboard = new Clipboard (); var s = clipboard.getText (); WScript.Echo (s); clipboard.setText (s.replace (/my/gi, "Your"));
function Clipboard ()
{
// IE のインスタンスを作成
this.internetExplorer = new ActiveXObject ('InternetExplorer.Application');
// IEの初期化
this.internetExplorer.Navigate ('about:blank');
while (this.internetExplorer.Busy)
WScript.Sleep (10);
// クリップボードを取得
this.clipboard = this.internetExplorer.Document.parentWindow.clipboardData;
// クリップボードより文字列を取得するメソッド
this.getText = function ()
{
return this.clipboard.getData ('text');
};
// クリップボードに文字列をコピーするメソッド
this.setText = function (s)
{
this.clipboard.setData ('text', s);
return true;
}
// IE を解放するメソッド
this.release = function ()
{
this.internetExplorer.Quit ();
return true;
}
return this;
}
コメント
コメントする
※ コメントスパム対策のため、コメント本文は、おはよう、こんにちわ、こんばんわ、のいずれかから始めるようにしてください。苦し紛れですけど・・・。