豆沙包

          …… …… 所學 所寫 所想 所做 所悟…… ……

          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(falsenullfalse)
          // 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 

          posted on 2005-02-24 17:53 carob 閱讀(930) 評論(0)  編輯  收藏 所屬分類: Groovy


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 万盛区| 马关县| 江安县| 沁源县| 鄂伦春自治旗| 白朗县| 定陶县| 邵阳市| 基隆市| 通辽市| 营山县| 东乡族自治县| 宜阳县| 社旗县| 樟树市| 唐河县| 南平市| 瑞金市| 潞城市| 隆林| 宕昌县| 弥勒县| 西平县| 巴彦淖尔市| 桓仁| 禄丰县| 台安县| 德钦县| 通榆县| 惠东县| 富川| 青阳县| 绵竹市| 甘南县| 资兴市| 镇坪县| 东乡族自治县| 宁津县| 吉首市| 通江县| 富源县|