blob: 4dfd75faef2d15be16c5be2095c39fe1a0be8435 [file] [log] [blame]
<?xml version="1.0"?>
<!--
$RCSfile: Writer.CountWordsOfTheSelectedTextWithOoRexx.snip,v $
last change: $Revision: 1.1 $ $Author: tomsontom $ $Date: 2008/10/20 11:29:28 $
(c)2003 by the copyright holders listed with the author-tags.
If no explicit copyright holder is mentioned with a certain author,
the author him-/herself is the copyright holder. All rights reserved.
Public Documentation License Notice:
The contents of this Documentation are subject to the
Public Documentation License Version 1.0 (the "License");
you may only use this Documentation if you comply with
the terms of this License. A copy of the License is
available at http://www.openoffice.org/licenses/PDL.html
The Original Documentation can be found in the CVS archives
of openoffice.org at the place specified by RCSfile: in this header.
The Initial Writer(s) of the Original Documentation are listed
with the author-tags below.
The Contributor(s) are listed with the author-tags below
without the marker for being an initial author.
All Rights Reserved.
-->
<snippet language="ooRexx" application="Writer">
<keywords>
<keyword>count</keyword>
<keyword>words</keyword>
<keyword>ooRexx</keyword>
</keywords>
<authors>
<author id="1" initial="true" email="" copyright="Josef Frysak">Josef Frysak</author>
</authors>
<question heading="Count words of the selected text with ooRexx">How to count the words of selected text?
</question>
<answer>
<p>First get the selected text. Now iterate trough the selection parts and</p>
<p>get each of them as a string. Use this string with the &quot;WORDS()&quot;</p>
<p>function of ooRexx to count its words. Finally sum up all counts.</p>
<p>For further details see http://wi.wu-wien.ac.at/rgf/diplomarbeiten/BakkStuff/2008/200809_Frysak/200809_Frysak_Automating_OOo_ooRexx_Nutshells.pdf.</p>
<listing>-- try to get a script context, will be .nil, if script was not invoked by OOo
x_ScriptContext = uno.getScriptContext()
if (x_ScriptContext &lt;&gt; .nil) then
do
-- invoked by OOo as a macro
-- get context
x_ComponentContext = x_ScriptContext~getComponentContext
-- get desktop (an XDesktop)
x_Desktop = x_ScriptContext~getDesktop
-- get current document
x_Document = x_ScriptContext~getDocument
end
else
do
-- called from outside of OOo, create a connection
-- connect to Open Office and get component context
x_ComponentContext = UNO.connect()
-- create a desktop service and its interface
service = &quot;{%see com.sun.star.frame.Desktop}&quot;
s_Desktop = x_ComponentContext~getServiceManager~{%see com.sun.star.lang.XMultiServiceFactory%XMultiServiceFactory}~createInstance(service)
x_Desktop = s_Desktop~{%see com.sun.star.frame.XDesktop%XDesktop}
-- get the last active document
x_Document = x_Desktop~getCurrentComponent()
end
-- really easy example, using ooRexx!!!
-- counter variable
wordcount = 0
-- get access to the current selection
x_Model = x_Document~{%see com.sun.star.frame.XModel%XModel}
s_Container = x_Model~getCurrentSelection()
if s_Container &lt;&gt; .nil then
do
-- if at least one selection has been made:
-- get an index list of the selection
x_IndexAccess = s_Container~{%see com.sun.star.container.XIndexAccess%XIndexAccess}
size = x_IndexAccess~getCount()
-- iterate trough the selections and retrieve the selected strings
-- count the words within the strings by using ooRexx builtin function WORDS
do counter = 1 to size
s_text = x_IndexAccess~getByIndex(counter - 1)
x_TextRange = s_text~{%see com.sun.star.text.XTextRange%XTextRange}
wordstring = x_TextRange~getString()
wordcount = wordcount + WORDS(wordstring)
end
end
-- output of counted words
.bsf.dialog~messageBox(&quot;Counted Words: &quot; || wordcount, &quot;WordCount&quot;, &quot;information&quot;)
::requires UNO.CLS</listing>
</answer>
<versions>
<version number="2.4.1" status="tested"/>
</versions>
<operating-systems>
<operating-system name="All"/>
</operating-systems>
<changelog>
<change author-id="1" date="2008-10-14">Initial version</change>
</changelog>
</snippet>