COM Scripting

COM Scripting

Introduction

Scriptom是由Guillaume     Laforge开发的一个可选Groovy模块。一旦在的Groovy环境中安装上,你就能够在Groovy中应用一个包装器来为任何ActiveX或者COM组件写脚本。当然了,这个模块只能在Windows下用。

Installation

安装Scripttom的最简单方式是将这个ZIP包解压到你的GROOVY_HOME目录下。
这个包内含了jacob.jar、jacob.dll和scriptom.jar.  DLL文件要放在bin目录下,亦或是你的java.libray.path下,以备jacob.jar调用装载。

Building from sources

如果你足够勇敢并倾向于从CVS Head中获取最新版本,那么你可以从源代码构建。检出 /scriptom 模块并用Maven自动完成安装。

如果你的GROOVY_HOME正指向你的groovy-core的源代码安装目录,仅键入:

maven

另外,若已将Groovy安装在了一个不同的目录,你有两种选择,在文件project.properties里将属性groovy.install.staging.dest指向你的GROOVY_HOME目录,并运行Maven,或者键入:

maven -Dgroovy.install.staging.dest=%GROOVY_HOME%

Usage

让我们看一下如何针对IE浏览器写脚本。首先,我们当然是要将ActiveX代理类引入进来。
然后,我们将创建一个jacob中ActiveXComponent类的GroovyObjectSupport包装器。现在,我们即将应用(调用)这个组件的属性或者是方法。

import org.codehaus.groovy.scriptom.ActiveXProxy

//  instanciate Internet Explorer
explorer  =   new  ActiveXProxy( " InternetExplorer.Application " )

//  set its properties
explorer.Visible  =   true
explorer.AddressBar 
=   true

//  navigate to a site by calling the Navigate() method
explorer.Navigate( " http://glaforge.free.fr/weblog " )


注意explorer.Visible返回的是一个代理,如果你想获取属性的真正的值,你要用这样的表达式:explorer.Visible.value 或explorer.Visible.getValue().

Limitations


目前,Scriptom处于beta阶段,故你在应用ActiveX或COM组件时可能会遇到一些bug和限制,不要犹豫,请将bug发表到JIRA或是邮件列表上。

第一个版本最大的一个不足(局限)是仍然还不能订阅你正在应用组件的事件。在下一个版本中,我希望能够让你能用闭包来定义自己的事件捕捉。象下面这样:

import org.codehaus.groovy.scriptom.ActiveXProxy

explorer 
=   new  ActiveXProxy( " InternetExplorer.Application " )
explorer.events.OnQuit 
=   { println "Quit" }
explorer.events.listen()

但是现在仍然不支持事件回调。在目前的CVS里有一个实验性的参考实现。它不能通过Groovy命令执行,但是可以在Java程序里透过GroovyShell对象来运行。

Samples

如果你检出了Scriptom的源代码,你会在src/script目录下发现几个例子。
我将下面的几个小节中向你展示一些例子。

Scripting Internet Explorer

import org.codehaus.groovy.scriptom.ActiveXProxy

//  instanciate Internet Explorer
explorer  =   new  ActiveXProxy( " InternetExplorer.Application " )

//  set its properties
explorer.Visible  =   true
explorer.AddressBar 
=   true

//  navigate to a site
explorer.Navigate( " http://glaforge.free.fr/weblog " )
Thread.sleep(
1000 )
explorer.StatusText 
=   " Guillaume Laforge's weblog "
Thread.sleep(
2000 )

//  quit Internet Explorer
explorer.Quit()

Scripting Excel

import org.codehaus.groovy.scriptom.ActiveXProxy

//  create a proxy for Excel
xls  =   new  ActiveXProxy( " Excel.Application " )
xls.Visible 
=   true

Thread.sleep(
1000 )

//  get the workbooks object
workbooks  =  xls.Workbooks
//  add a new workbook
workbook   =  workbooks.Add()

//  select the active sheet
sheet  =  workbook.ActiveSheet

//  get a handle on two cells
a1  =  sheet.Range( ' A1 ' )
a2 
=  sheet.Range( ' A2 ' )

//  sets a value for A1
a1.Value    =   123.456
//  defines a formula in A2
a2.Formula  =   ' =A1*2 '

println 
" a1: ${a1.Value.value} "
println 
" a2: ${a2.Value.getValue()} "

//  close the workbook without asking for saving the file
workbook.Close( false null false )
//  quits excel
xls.Quit()

警告:在我的机器(WinXP Home),仍然会有一个Excel.exe进程在运行,我对此仍无线索。:(

Mixing VBScript or JScript with Groovy

import org.codehaus.groovy.scriptom.ActiveXProxy

//  invoke some VBScript from Groovy and get the results!
sc  =   new  ActiveXProxy( " ScriptControl " )
sc.Language 
=   " VBScript "
println sc.Eval(
" 1 + 1 " ).value

Scripting the Windows Shell object

import org.codehaus.groovy.scriptom.ActiveXProxy

//  showing the current directory
cmd  =   new  ActiveXProxy( " Scripting.FileSystemObject " )
println cmd.GetAbsolutePathName(
" . " ).value

sh 
=   new  ActiveXProxy( " Shell.Application " )

//  minimizing all opened windows
sh.MinimizeAll()

//  opens an Explorer at the current location
sh.Explore(cmd.GetAbsolutePathName( " . " ).value)

//  choosing a folder from a native windows directory chooser
folder  =  sh.BrowseForFolder( 0 " Choose a folder " 0 )
println folder.Items().Item().Path.value

wshell 
=   new  ActiveXProxy( " WScript.Shell " )
//  create a popup
wshell.popup( " Groovy popup " )

//  show some key from the registry
key  =   " HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\User Agent "
println wshell.RegRead(key).value

net 
=   new  ActiveXProxy( " WScript.Network " )
//  prints the computer name
println net.ComputerName.value


Scripting Windows Media Player

import org.codehaus.groovy.scriptom.ActiveXProxy
import java.io.File

//  create a proxy for the Shell object
sh  =   new  ActiveXProxy( " Shell.Application " )

//  use a Windows standard folder chooser
folder  =  sh.BrowseForFolder( 0 " Choose a folder with wav files " 0 )

//  get the folder chosen
folderName  =  folder.Items().Item().Path.value
println 
" Playing Wav files from: ${folderName} "

//  create a Windows Media Player (from its Class ID)
player  =   new  ActiveXProxy( " clsid:{6BF52A52-394A-11D3-B153-00C04F79FAA6} " )

//  for each file in the folder
new  File(folderName).eachFile |file|
    
if (file.name.endsWith("wav")) {
        println file
        player.URL 
= file.absolutePath
        
// play the wav for one second
        control = player.controls.play()
        Thread.sleep(
1000)
    }

}


//  close the player
player.close()

当事件回调被支持后,你就能够订阅player.statusChange事件,以便你能够完整地播放wav。

Converting a Word document into HTML

这个程序将一个word文件作为第一个参数,并且生成一个同名的HTML文件,只是扩展名为".html"

import org.codehaus.groovy.scriptom.ActiveXProxy
import java.io.File

word 
=   new  ActiveXProxy( " Word.Application " )

word.Documents.Open(
new  File(args[ 0 ]).canonicalPath)
word.ActiveDocument.SaveAs(
new  File(args[ 0 -   " .doc "   +   " .html " ).canonicalPath,  8 )
word.Quit()

注意里面的参数 8,这就是转换后的HTML文件格式,具体为
0 : wdFormatDocument (no conversion) 
1 : wdFormatTemplate 
2 : wdFormatText 
3 : wdFormatTextLineBreaks 
4 : wdFormatDOSText 
5 : wdFormatDOSTextLineBreaks 
6 : wdFormatRTF 
7 : wdFormatUnicodeText 
8 : wdFormatHTML 

你可能感兴趣的:(COM Scripting)