Create a Class?

Is it possible to define a class? I wish to create a class with multiple property records. The only way I have been able to accomplish thus far is to define a record in my script(s). The applet that I am working on now will be using more than a dozen of these records types. It would be much simplier to define the “class” once and assign that class type to each of the variables using it.
Any help will be greatly appreciated.

Thanks to both you and tet for your responses. Upon initial reading on script objects, I believe this is just what I am looking for.
Thanks again, Brad Bumgarner

: Is it possible to define a class? I wish to create a class with
: multiple property records. The only way I have been able to
: accomplish thus far is to define a record in my script(s). The
: applet that I am working on now will be using more than a
: dozen of these records types. It would be much simplier to
: define the “class” once and assign that class type
: to each of the variables using it.
I think you want to use script objects. For more detail, see the AS Language Guide (available from the left sidebar of the macscripter.net page, under ‘Resources’.)
–tet

: Is it possible to define a class? I wish to create a class with
: multiple property records.

OO programming in AS uses a slightly different approach to OOP in most other OO languages (e.g. C++). It’s actually more like a prototype-based language such as Self, imo, and once you get the hang of it it can be surprisingly powerful and flexible.

The basic building block you use is the script object. This is covered in chapter 9 of the AppleScript Language Guide. [If you don’t already have a copy of the ASLG, go to Apple’s site and get one.] This chapter also discusses constructor functions, inheritance and delegation, and other aspects of AS that can be used in OOP, though doesn’t really say much about how to use them for real tasks (ASLG is really just a guide to AS’s features, not a full teaching manual).

I’m not sure if there’s any other literature that discusses OOP in AS very much - I think there’s a bit of a shortage in that area. Mostly it’s a matter of reading general info on OO and then figuring for yourself how to apply it to the stuff described in ASLG…

Meantime, here’s a simple example of a constructor function that creates a ‘stack’ [1] object, along with some test code showing how to use it. This might help give some idea of how OO in AS works:

on newStack(maxSize) --constructor function
	script --the object
		--the object's properties (In this design you aren't meant to access these directly - use the provided 'methods' instead.)
		property _valList : {}
		property _maxSize : maxSize as integer

		--the object's 'methods' (These are the [handler] calls that let you interact with this object.)
		on addVal(theValue)
			if _valList's length is _maxSize then error "Stack overflow." number 310
			set _valList's beginning to theValue
		end addVal

		on removeVal()
			try
				set theValue to _valList's first item
			on error number -1728
				error "Stack underflow." number 311
			end try
			if _valList's length is 1 then
				set _valList to {}
			else
				set _valList to _valList's rest
			end if
			theValue
		end removeVal
	end script
end newStack

--TEST
set x to newStack(3) --create a new 'instance' of the stack object and store in variable x

tell x --use a tell block to direct messages to the object stored in variable x
	addVal(3) --send it a message to add a value to itself
	addVal("hello") --[ditto]
	removeVal() --send it a message to retrieve a value from itself
end tell
Result: "hello"

HTH

has

[1] A ‘stack’, for anyone that’s curious, is a simple data store where the first item added is the last item out. Commonly used in ‘real programming’ tasks; very useful.