Talk:Python as a macro language

From Apache OpenOffice Wiki
Revision as of 06:52, 8 June 2007 by Jim hill au (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

I want to improve some info on this page, but as i never edited a wiki before, i prefer to discuss it first.

Recently it took me 3 whole days to discover some very simple facts about writing a python macro, because they are not clear in the documentation. Also, some things seem to have changed since this was written; it is much simpler to do in OOo 2.2.

Here is the simple information that i want to convey:

To write a simple macro:

  • in Python
  • stored in the document
  • called from a toolbar button (not a dialog)

This is what you need to know that is different from the existing documentation:

  • your function must accept an argument.
  • it does not have to import uno or unohelper, or anything at all.
  • you do not have to modify manifest.xml, it's done automatically.

This is what you need to know that i could not find for ages:
(it's there, but in a different context,
so it's hard to know the relevance of it when you first see it)

Within your function, to get handles on (context of) the UNO API, similar to "ThisDocument" and "StarDesktop" in OO Basic, do this:

doc = XSCRIPTCONTEXT.getDocument()
desktop = XSCRIPTCONTEXT.getDesktop()

These aspects of the task were easy enough to understand from the documentation:

  • putting the file in the document (unzip/zip)
  • making a toolbar button and connecting it to the function


code example:

def test_toolbar_button( *tpl_args ):
	"""Test of basic text insertion.
	Prints whatever args are passed to this function."""

	doc = XSCRIPTCONTEXT.getDocument()
	Text = doc.Text
	cursor = text.createTextCursor()

	s_text_1 = 'These args were passed:\n%r\n' % ( tpl_args, )
	Text.insertString( cursor, s_text_1, 0 )

	return 0


that's all for now
jh
jim_hill_au 08:52, 8 June 2007 (CEST)

Personal tools