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

Transform string into executable code?

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
Cosmic P
OOo Enthusiast
OOo Enthusiast


Joined: 23 Mar 2005
Posts: 186

PostPosted: Sun Apr 03, 2005 4:01 am    Post subject: Transform string into executable code? Reply with quote

Is it possible in StarBasic to transform a string into executable code? If so: how do I insert line breaks?
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 Apr 04, 2005 6:12 am    Post subject: Reply with quote

Yes, you can. I demonstrate how to do this in my book...
Code:
Sub CreateLib()
  Dim oLib     'The library to use to run the new function.
  Dim oLibs
  Dim s$       'Generic string variable.
  Dim sLName$
  Dim sMName$

  sLName = "MyNewLib"
  sMName = "MyNewModule"
  oLibs = GlobalScope.BasicLibraries
  If NOT oLibs.hasByName(sLName) Then
    oLibs.createLibrary(sLName)
  End If
  oLibs.loadLibrary(sLName)
  oLib = oLibs.getByName(sLName)
  If oLib.hasByName(sMName) Then
    oLib.removeByName(sMName)
  End If

  s = "Option Explicit" & CHR$(10) & _
      "Sub RunMeNow()" & CHR$(10) & _
      "  Print 4" & CHR$(10) & _
      "End Sub"
  oLib.insertByName(sMName, s)
End Sub

_________________
--
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
pitonyak
Administrator
Administrator


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

PostPosted: Mon Apr 04, 2005 6:13 am    Post subject: Reply with quote

A warning..... This macro simply over-writes and replaces any existing module with the provided name. Also, perhaps you should look at an existing module to see how they are stored. You need to use XML safe code.
_________________
--
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
Cosmic P
OOo Enthusiast
OOo Enthusiast


Joined: 23 Mar 2005
Posts: 186

PostPosted: Mon Apr 04, 2005 9:47 am    Post subject: Reply with quote

I have only little experience with XML (I do have some experience XHTML and a hole lot with HTML 4, but forget that last one). What exactly are you referring to with the term "XML safe"?
Back to top
View user's profile Send private message
pitonyak
Administrator
Administrator


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

PostPosted: Tue Apr 05, 2005 5:14 am    Post subject: Reply with quote

Write a little macro, and then retrieve and print the module to see what I mean. Without checking, it will probably look something like this:
Code:
MsgBox oLibs.getByName("somelib").getByName("somemodule")

I thought that I had entered a longer answer to this but I do not see it.... odd.
_________________
--
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
arivas_2005
Power User
Power User


Joined: 10 Oct 2006
Posts: 98
Location: El Salvador CentroAm

PostPosted: Mon May 07, 2007 10:10 am    Post subject: Reply with quote

pitonyak wrote:
Write a little macro, and then retrieve and print the module to see what I mean. Without checking, it will probably look something like this:
Code:
MsgBox oLibs.getByName("somelib").getByName("somemodule")

I thought that I had entered a longer answer to this but I do not see it.... odd.


Hi

This macro (up ) is excelent for my
but,
How it is for a file ?
Example:
file: data1.sxc,
Module: showmessage,
macro: showphrase,
cod.: msbox "Hello word"
Back to top
View user's profile Send private message
uros
Super User
Super User


Joined: 22 May 2003
Posts: 601
Location: Slovenia

PostPosted: Mon May 07, 2007 10:05 pm    Post subject: Reply with quote

Hi arivas

Code:
Sub CreateLib()
  Dim oLib     'The library to use to run the new function.
  Dim oLibs
  Dim s$       'Generic string variable.
  Dim sLName$
  Dim sMName$

  oDesktop = createUnoService("com.sun.star.frame.Desktop")
  sUrl = convertToUrl("file:///home/user/Documents/OO_db/data1.sxc")
  oDoc = oDesktop.loadComponentFromURL(sUrl,"_default",0,Array())   

  sLName = "MyNewLib"
  sMName = "showmessage"
  oLibs = oDoc.BasicLibraries   ''''''      Change GlobalScope to oDoc !!!
  If NOT oLibs.hasByName(sLName) Then
    oLibs.createLibrary(sLName)
  End If
  oLibs.loadLibrary(sLName)
  oLib = oLibs.getByName(sLName)
  If oLib.hasByName(sMName) Then
    oLib.removeByName(sMName)
  End If

  s = "Option Explicit" & CHR$(10) & _
      "Sub showphrase" & CHR$(10) & _
      "  MsgBox ""Hello World"" " & CHR$(10) & _
      "End Sub"
  oLib.insertByName(sMName, s)
End Sub
Change path to your document...
Uros
Back to top
View user's profile Send private message
arivas_2005
Power User
Power User


Joined: 10 Oct 2006
Posts: 98
Location: El Salvador CentroAm

PostPosted: Tue May 08, 2007 5:50 am    Post subject: Reply with quote

uros wrote:
Hi arivas

Code:
Sub CreateLib()
  Dim oLib     'The library to use to run the new function.
  Dim oLibs
  Dim s$       'Generic string variable.
  Dim sLName$
  Dim sMName$

  oDesktop = createUnoService("com.sun.star.frame.Desktop")
  sUrl = convertToUrl("file:///home/user/Documents/OO_db/data1.sxc")
  oDoc = oDesktop.loadComponentFromURL(sUrl,"_default",0,Array())   

  sLName = "MyNewLib"
  sMName = "showmessage"
  oLibs = oDoc.BasicLibraries   ''''''      Change GlobalScope to oDoc !!!
  If NOT oLibs.hasByName(sLName) Then
    oLibs.createLibrary(sLName)
  End If
  oLibs.loadLibrary(sLName)
  oLib = oLibs.getByName(sLName)
  If oLib.hasByName(sMName) Then
    oLib.removeByName(sMName)
  End If

  s = "Option Explicit" & CHR$(10) & _
      "Sub showphrase" & CHR$(10) & _
      "  MsgBox ""Hello World"" " & CHR$(10) & _
      "End Sub"
  oLib.insertByName(sMName, s)
End Sub
Change path to your document...
Uros


Thansk you Uros..

Do you can to help me with this:

a) if document it is open, How is these?
[.......already this open one ]
oDesktop = createUnoService("com.sun.star.frame.Desktop")
sUrl = convertToUrl("file:///home/user/Documents/OO_db/data1.sxc")
oDoc = oDesktop.loadComponentFromURL(sUrl,"_default",0,Array())


b) Make you help me, with these ?
(........ the file it can him .XLS or .SXC )

http://www.oooforum.org/forum/viewtopic.phtml?t=56775



again, thank you
Back to top
View user's profile Send private message
uros
Super User
Super User


Joined: 22 May 2003
Posts: 601
Location: Slovenia

PostPosted: Tue May 08, 2007 9:38 pm    Post subject: Reply with quote

Hi arivas_2005!
arivas_2005 wrote:

Do you can to help me with this:

a) if document it is open, How is these?
[.......already this open one ]
oDesktop = createUnoService("com.sun.star.frame.Desktop")
sUrl = convertToUrl("file:///home/user/Documents/OO_db/data1.sxc")
oDoc = oDesktop.loadComponentFromURL(sUrl,"_default",0,Array())

If file sUrl is already opened, it is activated when loadComponent... is executed. No problem.

arivas_2005 wrote:

b) Make you help me, with these ?
(........ the file it can him .XLS or .SXC )

http://www.oooforum.org/forum/viewtopic.phtml?t=56775
See the answer in the original thread.
Uros
Back to top
View user's profile Send private message
noranthon
Super User
Super User


Joined: 07 Jul 2005
Posts: 3318

PostPosted: Wed May 09, 2007 4:36 am    Post subject: Reply with quote

Sad to relate, but, unless there has been a change since 2.0.2, the command will open the file again without code to prevent that. (Other strange consequences can follow from using the command in menus.) The situation may have been different in 1.1.x.
_________________
search forum by month
Back to top
View user's profile Send private message
arivas_2005
Power User
Power User


Joined: 10 Oct 2006
Posts: 98
Location: El Salvador CentroAm

PostPosted: Wed May 09, 2007 9:47 pm    Post subject: Reply with quote

uros wrote:
Hi arivas_2005!
arivas_2005 wrote:

Do you can to help me with this:

a) if document it is open, How is these?
[.......already this open one ]
oDesktop = CreateUnoService("com.sun.star.frame.Desktop")
sUrl = convertToUrl("file:///home/user/Documents/OO_db/data1.sxc")
oDoc = oDesktop.loadComponentFromURL(sUrl,"_default",0,Array())

If file sUrl is already opened, it is activated when loadComponent... is executed. No problem.

arivas_2005 wrote:

b) Make you help me, with these ?
(........ the file it can him .XLS or .SXC )

http://www.oooforum.org/forum/viewtopic.phtml?t=56775
See the answer in the original thread.
Uros



For a)..................... I have

sub example1
Dim oDesktop1, oDesktop2
Dim sUrl1, sUrl2
Dim oDoc1, oDoc2
Dim oTargetDoc1, oTargetDoc2

oDesktop1 =
createUnoService("com.sun.star.frame.Desktop")
sUrl1 = convertToUrl("file:///home/user/Documents/data1.sxc")
oDoc1 = oDesktop1.loadComponentFromURL(sUrl1,"_default",0,Array())

oDesktop2 =
createUnoService("com.sun.star.frame.Desktop")
sUrl2 = convertToUrl("file:///home/user/Documents/data2.sxc")
oDoc2 = oDesktop2.loadComponentFromURL(sUrl2,"_default",0,Array())

oTargetDoc2.CurrentController.Frame.ContainerWindow.toFront()'
oTargetDoc2.CurrentController.Frame.ContainerWindow.SetFocus '()'
' all this is Ok...

end sub '' example1

''' bring the first open file to the front

sub example2

Dim oDesktop10
Dim sUrl10
Dim oDoc10
Dim oTargetDoc10


oDesktop10 =
createUnoService("com.sun.star.frame.Desktop")
sUrl10 = convertToUrl("file:///home/user/Documents/data1.sxc")
oDoc10 = oDesktop10.loadComponentFromURL(sUrl10,"_default",0,Array())
oTargetDoc10.CurrentController.Frame.ContainerWindow.toFront()'
oTargetDoc10.CurrentController.Frame.ContainerWindow.SetFocus '()'
end sub


Problem with example2

The window does not come to the front. Its button in the bar of tasks only blinks. But if I minimized it, then if it works (if come to the front)

NOTE: the macros are in other file ...(it is not in data1.sxc, neither in data2.sxc)
Back to top
View user's profile Send private message
uros
Super User
Super User


Joined: 22 May 2003
Posts: 601
Location: Slovenia

PostPosted: Wed May 09, 2007 11:53 pm    Post subject: Reply with quote

noranthon wrote:
Sad to relate, but, unless there has been a change since 2.0.2, the command will open the file again without code to prevent that. (Other strange consequences can follow from using the command in menus.) The situation may have been different in 1.1.x.
Embarassed Embarassed Embarassed
My apologies, didn't test with 2.x!
Uros
Back to top
View user's profile Send private message
noranthon
Super User
Super User


Joined: 07 Jul 2005
Posts: 3318

PostPosted: Thu May 10, 2007 5:01 pm    Post subject: Reply with quote

What reasonable person would expect later versions of software to be worse than the earlier versions?

arivas_2005 still has a question. (Unfortunately, I don't understand it.)
_________________
search forum by month
Back to top
View user's profile Send private message
arivas_2005
Power User
Power User


Joined: 10 Oct 2006
Posts: 98
Location: El Salvador CentroAm

PostPosted: Fri May 11, 2007 8:05 am    Post subject: Reply with quote

uros wrote:
noranthon wrote:
Sad to relate, but, unless there has been a change since 2.0.2, the command will open the file again without code to prevent that. (Other strange consequences can follow from using the command in menus.) The situation may have been different in 1.1.x.
Embarassed Embarassed Embarassed
My apologies, didn't test with 2.x!
Uros


thanks very much, Uros.

More questions, and excuseme my english (is very poor- low).
I have to find a solution with OO115, because, in my work
We have pcs olds. It did not supported OO2.xx

When i pass to other window (blinker). This is not accessed.

Only then after of to much clics. It is editable
I can not to traslate the focus active towards there.

It is to say, the command
oTargetDoc10.CurrentController.Frame.ContainerWindow.SetFocus
it does not leave it editable

Shocked Question Idea

So long. And again Thanks
Back to top
View user's profile Send private message
uros
Super User
Super User


Joined: 22 May 2003
Posts: 601
Location: Slovenia

PostPosted: Sun May 13, 2007 11:25 pm    Post subject: Reply with quote

Hi arivas_2005!
Let's try to solve your problem. There is the same mistake in both macros. You want to bring object oTargetDoc2 (or oTargetDoc10).CurrentController.Frame.ContainerWindow to front, but there is no definition of oTargetDoc2 (or 10)!
Use
Code:
oDoc2.CurrentController.Frame.ContainerWindow.toFront()'
oDoc2.CurrentController.Frame.ContainerWindow.SetFocus '()'
or oDoc10.... in example2.
Quote:
Code:

sub example1
Dim oDesktop1, oDesktop2
Dim sUrl1, sUrl2
Dim oDoc1, oDoc2
Dim oTargetDoc1, oTargetDoc2

oDesktop1 =
createUnoService("com.sun.star.frame.Desktop")
sUrl1 = convertToUrl("file:///home/user/Documents/data1.sxc")
oDoc1 = oDesktop1.loadComponentFromURL(sUrl1,"_default",0,Array())

oDesktop2 =
createUnoService("com.sun.star.frame.Desktop")
sUrl2 = convertToUrl("file:///home/user/Documents/data2.sxc")
oDoc2 = oDesktop2.loadComponentFromURL(sUrl2,"_default",0,Array())

oTargetDoc2.CurrentController.Frame.ContainerWindow.toFront()'
oTargetDoc2.CurrentController.Frame.ContainerWindow.SetFocus '()'
' all this is Ok...

end sub '' example1

''' bring the first open file to the front

sub example2

Dim oDesktop10
Dim sUrl10
Dim oDoc10
Dim oTargetDoc10

oDesktop10 =
createUnoService("com.sun.star.frame.Desktop")
sUrl10 = convertToUrl("file:///home/user/Documents/data1.sxc")
oDoc10 = oDesktop10.loadComponentFromURL(sUrl10,"_default",0,Array())
oTargetDoc10.CurrentController.Frame.ContainerWindow.toFront()'
oTargetDoc10.CurrentController.Frame.ContainerWindow.SetFocus '()'
end sub



There is a piece of code that works with both 1.1.x and 2.x. Function OpenOrActivateFile(sUrl) browse thru all opened components (files, help, Basic IDE) for a file with sUrl. If it is already opened, function activates it, else opens it.

Code:
Sub TestOpenOrActivateFile
   oDesktop = createUnoService("com.sun.star.frame.Desktop")
   sUrl = "file:///home/user/Documents/test_Library_1111.sxc"
   oDoc1=oDesktop.loadComponentFromURL(sUrl,"_blank",0,Array())
   sUrl = "file:///home/user/Documents/test_Library_2222.sxc"
   oDocument = OpenOrActivateFile(sUrl)
   Print oDocument.URL
End Sub

Function OpenOrActivateFile(sUrl)
' with a lot of help from Andrew Pitonyak's OpenOffice.org Macros Explained book, page 207
   Dim oDesk As Object, oEnum As Object
   Dim bJeze As Boolean
   bOpened = False
   oDesk = createUnoService("com.sun.star.frame.Desktop")
   oEnum = oDesk.Components.createEnumeration
   Do While oEnum.hasMoreElements
      oDoc = oEnum.nextElement
      If HasUnoInterfaces( oDoc, "com.sun.star.frame.XModel" ) Then
         openedUrl = oDoc.URL
         If openedUrl = sUrl then
            bOpened = True
            oDoc.CurrentController.Frame.ContainerWindow.toFront()
            Exit Do
         End If
      End If
   Loop
   If Not bOpened Then oDoc=oDesk.loadComponentFromURL(sUrl,"_blank",0,Array())
   OpenOrActivateFile = oDoc
End Function

Uros
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