OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

way to call makro like eval("some_makro")

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
zap
General User
General User


Joined: 28 May 2004
Posts: 40

PostPosted: Fri Jun 25, 2004 10:07 am    Post subject: way to call makro like eval("some_makro") Reply with quote

Hi

I searched the documentation in vain for a way to call methods I don't know at script-writing time.
I.e. users should be able to define a new makro, then name it "MAKRO".
Later on they should be able to type
"MAKRO" in the textdocument, then press CTRL+M which invokes a general makro handler
Code:

sub invokeMakro
    word = getLastWord ()
    eval (word)   ' where word is "MAKRO"
end sub

should call the MAKRO.
Is this possible at all?

thx
zap
Back to top
View user's profile Send private message
pitonyak
Administrator
Administrator


Joined: 09 Mar 2004
Posts: 3655
Location: Columbus, Ohio, USA

PostPosted: Fri Jun 25, 2004 2:53 pm    Post subject: Reply with quote

Well, you can call a macro based on its name, assuming that it already exists using code such as:
Code:
Sub TestDispatch
  Dim mNoArgs()
  oDispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
  sMacroURL = "macro:///Gimmicks.AutoText.Main"
  oDispatcher.executeDispatch(StarDesktop, sMacroURL, "", 0, mNoArgs())
End Sub

The previous macro is from section 5.32 of my macro document. I think, however, that you also want to be able to write y our own macros on the fly from code. You will need to modify an existing library or create a new library on the fly. This includes setting the entire contents of the module in a library. You may have to do things such as unload and load the library in question as well. In general, this is a pretty advanced thing to do. I have some of this content in my book, and I think taht DannyB has examples of this as well. I am not certain where his examples are... Sad
_________________
--
Andrew Pitonyak
http://www.pitonyak.org/oo.php
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address
Iannz
OOo Advocate
OOo Advocate


Joined: 14 Feb 2004
Posts: 494
Location: Christchurch, New Zealand

PostPosted: Fri Jun 25, 2004 6:30 pm    Post subject: Reply with quote

Out of curiosity why type the name of the macro rather than having it assigned to a key press?
AltKeyHandler.sxw available from the web site in my signature -does somethig similar but uses Alt key combinations instead of typing in the macro name. It uses a similar approach to that outlined by Andrew.
_________________
Cheers, Ian

http://wiki.services.openoffice.org/wiki/Extensions_development_basic a wiki about writing OpenOffice.org extensions.
Back to top
View user's profile Send private message
zap
General User
General User


Joined: 28 May 2004
Posts: 40

PostPosted: Sat Jun 26, 2004 4:55 am    Post subject: Reply with quote

Hi
The problem is that we have 600+ makros which are integrated into a workflow. I.e. some doctor speaks something like "ank" on tape, then a typist types "ank" and presses CTRL+M to expand it to some action or lenghty text. We have a custom "insertPseudoXML" which replaces the autotext functionality that reads it's information from a central DB.
Now they want to be able to record their own makros which also include actions like "insert text, then go back three letters", but there are too many macros to have a key-shortcut for each. therefore we want to do it the emacs way.
Our favorite way would be to store basic code in a database, but we can live with a netword drive (nfs4), too.
I'll look around in Dannies exaples. thx
zap

p.s. just a thought: since the recorded macros all have the same simple form:

createDispatchHelper ()
createArrayWiithArgs ()
executeDispatch ()

it might possible to wirte my own handler to intercept dispatches (same way the macro recorder works) and store that in some psuedoWhatever (xml) format which can be stored in the DB and then executed with the simple commands above. So another question: Is it possible to write my own macro-recorder in starbasic?
Back to top
View user's profile Send private message
zap
General User
General User


Joined: 28 May 2004
Posts: 40

PostPosted: Sat Jun 26, 2004 7:37 am    Post subject: Reply with quote

In reply to my P.S.:

from Andrews Document:

Quote:

DispatchInterceptor must be implemented and registered this is not possible in OOo Basic, you must use another language such as Python, Java or C++. OOo Basic is suitable, however, for many other dispatch related tasks.


looks like I have to learn java or python bindings for ooo ...
zap
Back to top
View user's profile Send private message
pitonyak
Administrator
Administrator


Joined: 09 Mar 2004
Posts: 3655
Location: Columbus, Ohio, USA

PostPosted: Mon Jun 28, 2004 10:34 am    Post subject: Reply with quote

But....

What if you map CTL+M to call a macro that looks at either the selected word, or the word before it. After finding the word, it then calls a macro based on the previous word. So, you do not have to have a handler for your new macro, just one that can then call the macro that you desire.
_________________
--
Andrew Pitonyak
http://www.pitonyak.org/oo.php
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address
zap
General User
General User


Joined: 28 May 2004
Posts: 40

PostPosted: Mon Jun 28, 2004 1:14 pm    Post subject: Reply with quote

Yes, that is what we currently do. It works alright, but my favourite solution would be to store the dispatch commands in our central database. I do not like the current way of recording macros that much, we have a central network drive with the macros on it, but that also means that users can overwrite important program-logic if we don't make it read-only (occured to us just after it happened Sad )
I'll give it a shot and publish it here if it should work and if anyone is interested.
zap
Back to top
View user's profile Send private message
Cybb20
Super User
Super User


Joined: 02 Mar 2004
Posts: 1569
Location: Frankfurt, Germany

PostPosted: Mon Jun 28, 2004 8:44 pm    Post subject: Reply with quote

I have a few suggestions to this thread:
A) If you absolutely need Starbasic only for what you want:

The following is really just some thoughts, I definately wouldn't recommend this way, it's far off too much of a hack, and I don't even know if it's possible.

The 600 macros you mentioned could usually be stored in one single library. The following approach takes this as the basis:

1.) Know in which module the macro is located:
Unfortunately we have more than one module for storing 600 macros, which is of course natural.
So here is what you could do:
Before you call
Code:
 eval (word)
you parse (with the sax module) each xba file stored in your library folder under <oooinstallationpath>/user/basic/ until you have found the value of the variable word, namely you get it from the text part of the .xba with the _characters routine of your parser. If you have found it you simply stop the parsing and return the filename of the current .xba file, without the file name extension ".xba". The xml parsing we do here, is not really parsing anything it's just searching (kind of silly, but how else do you want to search for a string in a xml file with means of Starbasic only)
General information: Use the ucb module for getting the files in the library folder and use Streams for the xml parsing.

2.) Once we have the module and the procedurename:
I want to expand on what Andrew already said: You can call a macro with means of the UNO dispatchhelper.
To continue from point 1:
Code:

sub invokeMakro
    word = getLastWord ()
    modulename = yoursaxparser(word) 'see 1.)
     eval (modulename, word) 
end sub
'~~~~~~~~~
'~~~~~~~~~
Sub eval(smodule as String ,smacro as String)
  Dim mNoArgs()
  oDispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
  sMacroURL = "macro:///<your library>." & smodule & "." & smacro
  oDispatcher.executeDispatch(StarDesktop, sMacroURL, "", 0, mNoArgs())
End Sub

However this approach lacks so many things.., so it's very error-prone:
1.) What if the value of word is found but does not represent the name of a procedure
2.) What if the procedure takes arguments?
3.) What if the string is not found (add an exception handler to your operating macro)
etc. etc.

And another problem is if the xml parser works fast and or correctly.

B) The approach with another language
For what you want I would absolutely prefer writing the code in Python. That way you can easily search through the xba files with regex (and you don't need a XML parser!!!) and can do other things much cleaner and much faster.
With regular expressions you are able to reduce any logical error to merely 0 and you can check if a procedure or function has one or more parameters

Or as was already suggested you can also write a UNO component and register it into your OpenOffice version. Though I don't quite understand what the sense is with having a DispatchInterceptor (he cannot know if the procedurename the user typed in exists or not, he just catches what you do on the UI).

See my suggestions as ideas, I don't have the exact plan in my head how your issue could be solved in the most efficient way.

Christian
_________________
- Knowledge is Power -
Back to top
View user's profile Send private message Send e-mail
Iannz
OOo Advocate
OOo Advocate


Joined: 14 Feb 2004
Posts: 494
Location: Christchurch, New Zealand

PostPosted: Mon Jun 28, 2004 10:27 pm    Post subject: Reply with quote

For an example of writing yourown macro recorder there is a simple one for Calc available from http://ooomacros.org/dev.php.

OOo has the ability to specify different folders for storing macros specifically to allow for sharing.

What OOo doesn't have is an automatic process for transferring personally recorded/written macros to a shared folder. But shifting them manually is no big deal (depending on frequency I guess).

Here is some code that I think does what you want. The fnDispatch could be better customised to your needs this is my generic one:
Code:
Sub eval
oVC = thisComponent.currentController.viewCursor
oCursor = OVC.text.createTextCursorByRange(oVC)
oCursor.gotoPreviousWord(False)
oCursor.gotoStartOfWord(false)
oCursor.gotoEndOfWord(true)
sMacro = oCursor.string
oCursor.string = ""
fnDispatch("PlayMacro", array("Statement",sMacro, "Asynchron",True))
End Sub


function fnDispatch(sCommand as string, optional mArgs)
oFrame = ThisComponent.CurrentController.Frame
oDispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
'if it is possible that the library isn't loaded then you would also need something like:
'basicLibraries.loadLibrary("MainLibrary")
on error resume next   'You could have an on error goto and have a beep or msgbox or something instead
if isMissing(mArgs) then
   fnDispatch = oDispatcher.executeDispatch(oFrame, ".uno:" & sCommand, "", 0, array())
else
   nArgs = uBound(mArgs) \ 2
   dim Args(nArgs) as new com.sun.star.beans.PropertyValue
   for i = 0 to nArgs
      Args(i).name = mArgs(i * 2)
      Args(i).value = mArgs(i * 2 + 1)
   next
   fnDispatch = oDispatcher.executeDispatch(oFrame, ".uno:" & sCommand, "", 0, Args())
end if
end function

_________________
Cheers, Ian

http://wiki.services.openoffice.org/wiki/Extensions_development_basic a wiki about writing OpenOffice.org extensions.
Back to top
View user's profile Send private message
zap
General User
General User


Joined: 28 May 2004
Posts: 40

PostPosted: Tue Jun 29, 2004 12:16 am    Post subject: Reply with quote

Thanks, Christian and Ian.
The fnDispatch function is really useful, but right now it seems like we can implement OO macro recorder in python. So we go for choice B) of Christians ideas. I'll definatly post the results here.

Christian: Your A) idea would have been my fav. choice before I knew that the python api is not really dificult. But it really looks hackish.

Ian: The macro recorder implemented in basic does not catch dispatches but installs listeners and then interprets the data itself. Therefore we would need to adjust it to writer and even then it would take a lot of work to make it feature complete. Therefore we try the Dispatchrecorder.

see
http://api.openoffice.org/docs/common/ref/com/sun/star/frame/XDispatchRecorder.html

thanks again
zap

p.s. Christian: We need the Interceptor to record the macros the way we need them (url and params only). I would also like to knit another ui around the recording actions since it will be used often and should be more comfortable than the current small (not-resizable ) macro dialog.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group