SSブログ

perlでIE操作 [perl]

perlのWin32::OLEでInternet Explorerを操作してみる。
[位置情報]お約束の宣言はこれ。文字コード変換もあわせて宣言する。起動時にエラーが出る人は、CPANでインストールしてね。やり方google先生にきくのがはやい。
use utf8;
use Win32::OLE;
use Encode;

[位置情報]Internet Explorerを起動してタイトルを表示してみる。
#Internet Explorerを起動
my $ie=Win32::OLE->new('InternetExplorer.Application') 
      or die Win32::OLE->LastError;

#Internet Explorerを見えるようにする
$ie->{Visible}=1;

#タイトルを表示する。
my $title=decode("cp932",$ie->document->title);
printf("%s\n",encode('utf-8',$title));

[位置情報]起動中のInternet Explorerのタイトルを変えてみる
#shellのOLEを生成
my $shell=Win32::OLE->new('Shell.Application')
     or die Win32::OLE->LastError;
my $ie=0;

#Windowを探索
for(my $i=0;$i<$shell->Windows->Count;$i++){
    next if($shell->Windows($i) !~ /^Win32\:\:OLE.+/);
    # オブジェクトタイプ取得
    my($ln,$cn)=Win32::OLE->QueryObjectType($shell->Windows($i));
    next if($cn ne "IWebBrowser2");
    my $fullname=decode("cp932",$shell->Windows($i)->FullName);
    $fullname=~tr/[A-Z]/[a-z]/;
    next if($fullname !~ /.*iexplore\.exe$/);
    $ie=$shell->Windows($i);
    last;
}
die "Internet Explorer is not found." if($ie==0);

#タイトルを変更
$ie->document->{title}=encode("cp932","てすと");


[位置情報]タイトルから起動中のInternet Explorerを探す。
#shellのOLEを生成
my $shell=Win32::OLE->new('Shell.Application')
     or die Win32::OLE->LastError;
my $ie=0;
my $title="MSN Japan";

#Windowを探索
for(my $i=0;$i<$shell->Windows->Count;$i++){
    next if($shell->Windows($i) !~ /^Win32\:\:OLE.+/);
    # オブジェクトタイプ取得
    my($ln,$cn)=Win32::OLE->QueryObjectType($shell->Windows($i));
    next if($cn ne "IWebBrowser2");
    my $fullname=decode("cp932",$shell->Windows($i)->FullName);
    $fullname=~tr/[A-Z]/[a-z]/;
    next if($fullname !~ /.*iexplore\.exe$/);
    $wintitle=decode("cp932", $shell->Windows($i)->document->title);
    next if($wintitle ne $title);
    $ie=$shell->Windows($i);
    last;
}
die "Internet Explorer is not found." if($ie==0);




初めてのPerl

初めてのPerl

  • 作者: ランダル・L. シュワルツ
  • 出版社/メーカー: オライリージャパン
  • 発売日: 2003/05
  • メディア: 単行本




Perl CPANモジュールガイド

Perl CPANモジュールガイド

  • 作者: 冨田尚樹
  • 出版社/メーカー: ワークスコーポレーション
  • 発売日: 2011/04/08
  • メディア: 単行本(ソフトカバー)



nice!(0)  コメント(0)  トラックバック(0) 

nice! 0

コメント 0

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

トラックバック 0

perlをUTF-8で書くperlでGoogle検索 ブログトップ

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。