Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<p><table class="wysiwyg-macro" data-macro-name="excerpt" data-macro-parameters="atlassian-macro-output-type=INLINE|hidden=true" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2V4Y2VycHQ6aGlkZGVuPXRydWV8YXRsYXNzaWFuLW1hY3JvLW91dHB1dC10eXBlPUlOTElORX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="RICH_TEXT"><tr><td class="wysiwyg-macro-body"><p>allows you to script any ActiveX or COM Windows component from within your Groovy scripts </p></td></tr></table> </p> <table class="wysiwyg-macro" data-macro-name="warning" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e3dhcm5pbmd9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="RICH_TEXT"><tr><td class="wysiwyg-macro-body"><p>This is the page of the old version of Scriptom. As the new version, <a class="confluence-link" href="/display/GROOVY/COM+Scripting" data-linked-resource-id="24576222" data-linked-resource-type="page" data-linked-resource-default-alias="COM Scripting" data-base-url="http://docs.codehaus.org">Scriptom 2.0</a>, brings with it a couple breaking changes, you may still want to use this version. However, we highly encourage you to switch to the <a class="confluence-link" href="/display/GROOVY/COM+Scripting" data-linked-resource-id="24576222" data-linked-resource-type="page" data-linked-resource-default-alias="COM Scripting" data-base-url="http://docs.codehaus.org">newer version</a> which solves a certain number of bugs and provide a much better integration to COM / ActiveX components. </p></td></tr></table> <h1>Introduction</h1> <p><strong>Scriptom</strong> is an optional Groovy module developed by <a href="http://glaforge.free.fr/weblog">Guillaume Laforge</a> leveraging the <a href="http://danadler.com/jacob/">Jacob library (JAva COm Bridge)</a>. Once installed in your Groovy installation, it allows you to script any ActiveX or COM Windows component from within your Groovy script. Of course, this module can be used on Windows only.</p> <p><strong>Scriptom</strong> is especially interesting if you are developing Groovy shell scripts under Windows. You can combine both Groovy code and any Java library with the platform-specific features available to Windows Scripting Host or OLE COM automation from Office. <br class="atl-forced-newline" /></p> <h1>Installation</h1> <h2>Zip bundle</h2> <p>The easiest way for installing Scriptom is to unzip the <a class="confluence-link unresolved" data-filename="scriptom-1.2.zip" data-linked-resource-default-alias="scriptom-1.2.zip" href="#">Zip bundle</a> in your <strong>%GROOVY_HOME%</strong> directory.<br /> The distribution contains the jacob.jar and jacob.dll, and the scriptom.jar. The DLL needs to be in the bin directory, or in your <strong>java.library.path</strong> to be loaded by jacob.jar.</p> <h2>Building from sources</h2> <p>If you are brave enough and prefer using the very latest fresh version from CVS Head, you can build <strong>Scriptom</strong> from sources. Checkout modules/scriptom, and use Maven to do the installation automatically. If your <strong>%GROOVY_HOME%</strong> points at the target/install directory of your groovy-core source tree, just type:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> maven </pre></td></tr></table> <p>Otherwise, if you have installed Groovy in a different directory, you have two possibilities, either you change the property <strong>groovy.install.staging.dest</strong> to your <strong>%GROOVY_HOME%</strong> directory in the <strong>project.properties</strong> file, and run maven, or you can type:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> maven -Dgroovy.install.staging.dest=%GROOVY_HOME% </pre></td></tr></table> <h1>Usage</h1> <p>Let's say we want to script Internet Explorer. First, we're going to import the ActiveX proxy class.<br /> Then, we're going to create a GroovyObjectSupport wrapper around the ActiveXComponent class of Jacob. And now, we're ready to use properties or methods from the component:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> import org.codehaus.groovy.scriptom.ActiveXProxy // instantiate Internet Explorer def 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") </pre></td></tr></table> <p>Note however that explorer.Visible returns a proxy, if you want to get the real value of that property, you will have to use the expression <strong>explorer.Visible.value</strong> or <strong>explorer.Visible.getValue()</strong>.</p> <h2>Limitations</h2> <p>For the moment, <strong>Scriptom</strong> is in a beta stage, so you may encounter some bugs or limitations with certain ActiveX or COM component, so don't hesitate to post bugs either in JIRA or on the mailing lists. There may be some issues with the mappings of certain objects returned by the component and the Java/Groovy counterpart.</p> <p>An important limitation for the first release is that it is not yet possible to subscribe to events generated by the components you are scripting. In the next releases, I hope I will be able to let you define your own event handlers with closures, with something like:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> import org.codehaus.groovy.scriptom.ActiveXProxy def explorer = new ActiveXProxy("InternetExplorer.Application") explorer.events.OnQuit = { println "Quit" } explorer.events.listen() </pre></td></tr></table> <p>But for the moment, event callbacks are not supported.<br /> There is an experimental implementation currently in CVS Head,it does not work with the groovy command, but it does work when launching a script from a Java program with the GroovyShell object. There is perhaps a problem with Classworlds or Jacob, and the different classloaders. If anyone has a clue, I'm game!</p> <h1>Samples</h1> <p>If you checkout the <strong>Scriptom</strong> sources, you will find a few samples in the <strong>src/script</strong> directory.<br /> I will show you some samples in the following sub-sections.</p> <h2>Scripting Internet Explorer</h2> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> import org.codehaus.groovy.scriptom.ActiveXProxy // instantiate Internet Explorer def 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() </pre></td></tr></table> <h2>Scripting Excel</h2> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> import org.codehaus.groovy.scriptom.ActiveXProxy // create a proxy for Excel def xls = new ActiveXProxy("Excel.Application") xls.Visible = true Thread.sleep(1000) // get the workbooks object def workbooks = xls.Workbooks // add a new workbook def workbook = workbooks.Add() // select the active sheet def 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() </pre></td></tr></table> <p>Warning: on my machine (WinXP Home), there is still an Excel.exe process running. I have no clue why Excel is still running.</p> <h2>Mixing VBScript or JScript with Groovy</h2> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> import org.codehaus.groovy.scriptom.ActiveXProxy // invoke some VBScript from Groovy and get the results! def sc = new ActiveXProxy("ScriptControl") sc.Language = "VBScript" println sc.Eval("1 + 1").value </pre></td></tr></table> <h2>Scripting the Windows Shell object</h2> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> import org.codehaus.groovy.scriptom.ActiveXProxy // showing the current directory def 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 def folder = sh.BrowseForFolder(0, "Choose a folder", 0) println folder.Items().Item().Path.value def wshell = new ActiveXProxy("WScript.Shell") // create a popup wshell.popup("Groovy popup") // show some key from the registry def key = "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\User Agent" println wshell.RegRead(key).value def net = new ActiveXProxy("WScript.Network") // prints the computer name println net.ComputerName.value </pre></td></tr></table> <h2>Scripting Windows Media Player</h2> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> import org.codehaus.groovy.scriptom.ActiveXProxy import java.io.File // create a proxy for the Shell object def sh = new ActiveXProxy("Shell.Application") // use a Windows standard folder chooser def folder = sh.BrowseForFolder(0, "Choose a folder with wav files", 0) // get the folder chosen def folderName = folder.Items().Item().Path.value println "Playing Wav files from: ${folderName}" // create a Windows Media Player (from its Class ID) def 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() </pre></td></tr></table> <p>When event callbacks are supported, you will be able to subscribe to the player.statusChange event, so that you can play the wav entirely, before loading a new sample (instead of listening only to the first second of each sample).</p> <h2>Converting a Word document into HTML</h2> <p>This program takes a Word document as first parameter, and generate an HTML file with the same name, but with the .html extension.</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> import org.codehaus.groovy.scriptom.ActiveXProxy import java.io.File def 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() </pre></td></tr></table> <h2>Printing the contents of your Outlook Inbox</h2> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> import org.codehaus.groovy.scriptom.ActiveXProxy def outlook = new ActiveXProxy("Outlook.Application") def namespace = outlook.GetNamespace("MAPI") // There is only "MAPI" // 6 == Inbox; other values in Outlook's VBA documentation def inbox = namespace.GetDefaultFolder(6) def mails = inbox.Items println "Elements in your Inbox: " + mails.Count.value for (i in 1..mails.Count.value) { def mail = mails.Item(i) println i + ": " + mail.Subject.value + " (" + mail.Size.value + " bytes)" } </pre></td></tr></table>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced