Xojoscript basic functions

Hi,
I’m new to Xojo script and I think I maybe missing some important info, not sure where to find it in the docs. Some basic commands like

Dim newF as Xojo.IO.FolderItem
dim d as new date

Throw up errors (no type or module with this name)

print “hello world”

works fine.

Anyone any ideas?

I don’t think XojoScript supports the new framework. (I could be wrong on that, I’m avoiding the new framework until it’s done) Here’s the documentation for XojoScript: http://documentation.xojo.com/index.php/XojoScript

You might not be able to do anything more than what’s listed here: http://developer.xojo.com/scripting-functions

You can’t access any internal classes from Xojoscript. However you can access a single context class into which you can have as many functions accessible as you like, as long as you don’t pass objects back and forth.

Yes, you have to create a wrapper function or class to pass such data back and forth between your main Xojo app and your Xojoscript. Xojoscript only supports the basic language functions (if-then, etc.) and datatypes (no objects like date or folderitem).

If you just need basic functionality, you can just create your own functions that run the code inside the Xojo app. For instance, you could write a Today routine that returns today’s date to the script by using a date object inside a Xojo method.

If you need full date object functionality, you can create a clone of the entire class inside Xojoscript. I’ve done this and it’s pretty cool. I create a class in my script that has all the same functions as the original class, so regular Xojo code works in my Xojoscript. But behind the scenes the class is actually calling my context object and having Xojo do the date stuff there. I store a date object there in a local dictionary so each each clone date object has a unique ID that corresponds to the date object in the dictionary.

A bit of work to set up, but once you’ve done it, it works great. You can use the same technique for folderitems and other Xojo objects.

(I can post some code for this if you want.)

[quote=381533:@dave duke]Yes, sounds good, posting some could would be even better!
Thanks
Dave[/quote]

Sure. Here’s the script from my XojoScript presentation in at the Berlin conference last year:

[code]class dateClass

private dateID as integer
sub constructor()
dateID = date_newDate
end sub

property year as integer
get
return date_getYear(dateID)
end get
set
date_setYear(dateID, value)
end set
end property

property month as integer
get
return date_getMonth(dateID)
end get
set
date_setMonth(dateID, value)
end set
end property

property day as integer
get
return date_getDay(dateID)
end get
set
date_setDay(dateID, value)
end set
end property

function SQLDate() as string
return date_SQLDate(dateID)
end function

function SQLDateTime() as string
return date_SQLDateTime(dateID)
end function

function abbreviatedDate() as string
return date_abbreviatedDate(dateID)
end function

function longDate() as string
return date_longDate(dateID)
end function

function shortDate() as string
return date_shortDate(dateID)
end function

end class

dim d as dateClass
d = new dateClass

print “Default date:”
print d.SQLDate

d.year = 2013
d.month = 6
d.day = 30

print “”
print “New date:”

print d.SQLDate
[/code]

Note that the class definition is the main part – the code after that just demonstrates using the new date class in the script, so you can experiment with that to test different things.

For this to work, you’ll need a Private Property dateList as dictionary property added to your context object and the following context methods:

[code]Public Sub date_setMonth(dateID as integer, month as integer)
dim d as date = returnDateFromID(dateID)
d.month = month
setDateFromID(dateID, d)
End Sub

Public Function date_abbreviatedDate(dateID as integer) as string
return returnDateFromID(dateID).abbreviatedDate
End Function

Public Function date_getDay(dateID as integer) as integer
return returnDateFromID(dateID).day
End Function

Public Function date_getMonth(dateID as integer) as integer
return returnDateFromID(dateID).month
End Function

Public Function date_getYear(dateID as integer) as integer
return returnDateFromID(dateID).year
End Function

Public Function date_longDate(dateID as integer) as string
return returnDateFromID(dateID).longDate
End Function

Public Function date_newDate() as integer
if dateList = nil then dateList = new dictionary

dim id as integer = dateList.count + 1
dateList.value(id) = new date

return id
End Function

Public Sub date_setDay(dateID as integer, day as integer)
dim d as date = returnDateFromID(dateID)
d.day = day
setDateFromID(dateID, d)
End Sub

Public Sub date_setYear(dateID as integer, year as integer)
dim d as date = returnDateFromID(dateID)
d.year = year
setDateFromID(dateID, d)
End Sub

Public Function date_shortDate(dateID as integer) as string
return returnDateFromID(dateID).shortDate
End Function

Public Function date_SQLDate(dateID as integer) as string
return returnDateFromID(dateID).SQLDate
End Function

Public Function date_SQLDateTime(dateID as integer) as string
return returnDateFromID(dateID).SQLDateTime
End Function

Public Sub init()
dateList = new dictionary
End Sub

Private Function returnDateFromID(dateID as integer) as date
if dateList = nil then dateList = new dictionary
return dateList.lookup(dateID, new date)
End Function

Private Sub setDateFromID(dateID as integer, aDate as date)
if dateList = nil then dateList = new dictionary
dateList.value(dateID) = aDate
End Sub
[/code]

As you can probably figure out, these are the context methods the script methods call. Because the class we set up in out script mirrors the normal date class, once you’ve built this, you can copy/paste normal (classic framework) date code right into your script and it will run since the syntax is the same. The actual date objects and calculations are all being managed inside the main Xojo program, but your script doesn’t need to know that. From the perspective of the script’s code, you’re working with a date object in the script and it just works.

You can do the same thing with other Xojo objects, such as folderitems, but it is some work to set it up and you probably don’t need all that power in the script. (Like for folderitems, I usually just set up simple load/save text file context routines and that’s all I need in my script.) But the date class is pretty simple and it can be useful to have access to all its functions within your scripts.

Let me know if you have questions.