XojoScript Question

I’ve never used Xojo script before but now I have an idea where I’d like to implement it if it can do what I’d like it to do.

What objects inside your application are accessible to XojoScript? I’m writing some code that will allow the user to connect to an external device. When certain data comes back from that device, I want specific actions taken. So here’s my specific example, but I want it to be generalized so that I can handle similar cases:

The customer in question has a VoIP phone system. We will connect to that system and register with that system to send us data on any incoming calls. When a call comes in, I want to display that CallerID and Number but I also want to open a window that will allow them to answer the call.

Now that is just for this specific example. I’ve got it figured out how to flexibly extract the caller ID info from the incoming data sent by the system using a RegEx. It’s the window opening that I’m wanting to be flexible. In this case I want a specific window. I could hard code that but it would remove flexibility. In another case for another customer that is not VoIP or phone call related, I may want to open a different window. That’s why I’d like to be able to have some script that can be added at the specific user site that handles what they need based on the data received. Is this possible?

In my reading of the LR regarding XojoScript, I don’t think it is. There appears to be a single context object that you can pass that is supposed to be available to the script, but I don’t always know what that object is going to be.

Thoughts?

That object can represent the app and the various functions available to the script through functions and methods.

How do I access those items though? Is there a good example some place? The included examples in Xojo don’t really show much.

You create a method or function as usual in your class. When you provide that class as the context to the XojoScript, those become available as if they were part of the language. But they may only return or take scalar values.

So if I set the App object as the context to my XojoScript any method in my App object is then accessible to the script?

And do I have access to properties of the class in the context or just the methods and functions?

I’d be responding from possibly faulty memory so best just to try it.

OK. I was able to access a method. Have not tried any properties. I’ll try that next.

So just was playing this - I can access any methods and scalar properties of the object I pass in as context. I cannot directly access properties that are objects however (as the LR specifies). I’ve tried ways around this by doing things like creating a function that returns the class or object. Nope. Doesn’t work. I can create a function that access a method, function or property of a class as long as that property, function or method is scalar and doesn’t return an object. I can call a method that opens a window, but I can’t open the widow directly.

So I think this may work for what I want to do. I’ll just have to add a bunch of code inside the object that will run from the script so the script can access things in the app.

[quote=419503:@Jon Ogden]I’ll just have to add a bunch of code inside the object that will run from the script so the script can access things in the app.
[/quote]

Right. Basically, you are creating an API for the scripts.

Since you have to have the windows present in your project anyway, what does the XojoScript provide that you couldn’t provide as logic inside those windows?

A simple select case on the window name would work.

Maybe I don’t want to open a window every time. I have an “automation” function inside my application where different actions can be automated. Maybe if I receive a certain message, I want to run an automation. Maybe I want to do something else, etc.

If it was just a window, it would be easy. And I could provide all sorts of extra logic. But I thought why not add a script that would allow it to be done on a case by case basis.

I use XojoScript to provide a user interface to a library of math and analysis functions. The user can then write short programs to do various mathematical manipulations or generate plots of experimental data. I have used my MathScriptor program for many years to help in teaching mathematical methods in the physical sciences.

My program is available for free at https://birgegroup.uconn.edu/software/

As Kem mentioned, write your own API to access the functionality of your software. You just need to be creative in passing parameters, because you are limited in types available.

I allow for user designed automation in a desktop app, where one can access other apps with xojoscript, send sql to databases or manipulate data in controls of the UI, and more.

https://www.seminar.pro/ScriptHelp/

@Oliver Osswald : how did you code your webpage ? I do like the side menu bar.
(but I would prefer a flip-menu bar system if anyone knows a simple free one ?)
doesn’t seems to be a xojo web app … :wink:

I’ve done things where commands in the script call through to a context command that creates the corresponding object in the main xojo app and puts it into a dictionary indexed by a unique id string. Then I return that string to the script. I’ve got commands that take that unique id as the first parameter, look up the object and then pass off the rest of the parameters to the object. you also have to provide for a way for the script to tell you it’s done with an object so you can remove it from the dictionary and make sure the scripter knows of the importance of doing that or they will live forever.

So you can do things in the script like:
dim myWindow to makeNewWindow
which just returns the reference, which is really just a string of the unique id, and then do things to it like:

setBackgroundColor( myWindow, “FF4444”)

or something like that, which would just call directly through to a context object handler which is just receiving 2 strings. Look up the window in the first parameter and set the color from the RGB string of the second.

It’s also possible to do it more object oriented by adding some canned xojoscript code at the beginning of the script and just appending the users code to the end. Instead of having the user call directly to the context command you could have them create an actual xojoscript object with the regular new command which in it’s constructor calls to the makeNewWindow command and stores it’s uniqueid in an object property, then provide other local class handlers which would call through to the context class and automatically pass the uniqueid and handle the rest. If you do it that way then you can do things like:

set myWindow to new displayWindow

where displayWindow is a xojoscript class that you pre-pended to the user code before compilation. That class would have a property called uniqueID which you’d set in the constructor like:

sub constructor()
uniqueId = makeNewWindow()
end sub

myWindow.setBackgroundColor( “FF4444”)

where the subroutine in the class would be something like:

sub setBackgroundColor( newColor)
setBackgroundColor( uniqueId, newColor)
end sub

obviously you’d need to use different names for the functions or things would overlap fail :wink: I would start the handlers in the context class that are not meant for regular users with an underscore or something so they wouldn’t get confused, so it really should be _setBackgroundColor( uniqueId, newColor) or something like that.

and now you have an extensible object oriented interface. And you can also create the destructor handler in the script class that would call through to whatever your context class handler is that would remove the object from the dictionary and clean up that memory in the app itself.

[quote=419694:@Jean-Yves Pochez]@Oliver Osswald : how did you code your webpage ? I do like the side menu bar.
(but I would prefer a flip-menu bar system if anyone knows a simple free one ?)
doesn’t seems to be a xojo web app … ;)[/quote]
I think I used Freeway Pro for the helpfiles, and some css:
https://download2.softpress.com/video/moment_9_HQ.mov

[quote=419514:@Jon Ogden]Maybe I don’t want to open a window every time. I have an “automation” function inside my application where different actions can be automated. Maybe if I receive a certain message, I want to run an automation. Maybe I want to do something else, etc.

If it was just a window, it would be easy. And I could provide all sorts of extra logic. But I thought why not add a script that would allow it to be done on a case by case basis.[/quote]

You might want to try the Scripts.xojo_plugin:

It can access any method and property known to Xojo. Really worth trying out. Download the demo plugin (which provide all features of the pro plugin). If you produce an app with the demo plugin it will throw a window asking to purchase the pro plugin. It’s not expensive. Some sample projects require the Lexing plugin (which is currently under review and adapted to light/dark mode for Mojave macOS 10.14 and High contrast for windows 7/10). Don’t want to steal this thread, contact me at “vanhoek at Mac dot com” if you have questions, or other things you want to know.