Creating a new class in inside script

I’m writing a script to put some text on an image. The text could be viewed as a number of fields placed different places within the image, having different properties like, font, size etc.

Currently I’m doing it with a lot of variable like this:


			set titleFontSize to 60
			set subTitleFontSize to 50
			set detailsFontSize to 42
			
			-- Layout
			set titleXInsert to 250
			set titleYInsert to 250
			set subTitleXInsert to titleXInsert + 50
			set subTitleYInsert to titleYInsert + 30 + subTitleFontSize
			set confXInsert to titleXInsert
			set confYInsert to subTitleYInsert + detailsFontSize + 200
			set locXInsert to 1200
			set locYInsert to detailsFontSize + 40
			--( . . . ) 

While this will work, it feels terribly inefficient and difficult to manage.
So from what little I’ve heard about actual object oriented programming, I should be creating a Class that would work something like this:

class textField
property font: fontName
property size: fontSize
property placement: insertionPoint

and then create a textField object for each of the fields I want.

Is it even possible to specify a new class in an AppleScript? If so, how does that work (I haven’t really found anything on googling “simple class creation AppleScript”).
Is there a better way of going about this?

Thanks a lot in advance.
/adam

They are named Script Objects. When you google fo rit you will see what they are

a simple script object could look like this

on newTextField()
	script textField
		property __width : missing value
		property __height : missing value
		property xPos : missing value
		property yPos : missing value
		property content : missing value
		
		on init()
			set __width to 120
			set __height to 20
			set xPos to 0
			set yPos to 20
			set content to ""
			return me
		end init
		
		on getHeight()
			return __height
		end getHeight
		
		on setHeight(__length)
			set __height to __length
			my updateView()
		end setHeight
		
		on getWidth()
			return __width
		end getWidth
		
		on setWidth(__length)
			set __width to __length
			my updateView()
		end setWidth
		
		on getPosition()
			return {xPos, yPos}
		end getPosition
		
		on setPosition(__coordinates)
			set {xPos, yPos} to coordinates
			my updateView()
		end setPosition
		
		on getBounds()
			return {xPos, yPos, xPos + __width, yPos + __height}
		end getBounds
		
		on setBounds(__bounds)
			set {xPos, yPos, a, b} to __bounds
			set __width to a - xPos
			set __height to b - yPos
			my updateView()
		end setBounds
		
		on getStringValue()
			return content
		end getStringValue
		
		on setStringValue(__string)
			set content to __string
			my updateView()
		end setStringValue
		
		on updateView()
			--apply the changes the user has made to the properties
		end updateView
	end script
end newTextField

To use it

set myTextField to newTextField()'s init()

now MyTextField is an initialized object. To use the handlers (methods)

myTextField's getWidth()
myTextField's setWidth(300)
myTextField's getBounds()

Thank you for that, DJ Bazzie Wazzie

On experimenting with it, I found that I didn’t need to specify all the set/get commands. I’m thinking this is due to inheritance from the parent script class.

However I also found that

set textField to newTextField() of me
get properties of textField

didn’t produce what I expected.
Instead of a record of the current values of the properties I specified (e.g __width etc.), I get:

{selection:insertion point after character 1351 of text of document "test.scpt", frontmost:true, class:application, name:"AppleScript Editor", version:"2.4.1"}

What am I doing wrong?

Also, is it possible to set several properties of a script object at the same time? With something like:

 set properties of textField to {__width:20, content: "some text"}

thanks
/adam

When you want to create a class like a normal class and not like a script object you should overwrite all commands and properties that should behave other than default. for example this is an example if you want to use properties

on newObject()
	script prototype
		property properties : missing value
		
		on init()
			set properties to {x:0, y:0}
			return me
		end init
		
		on setX(x)
			set properties to {x:x} & properties
			return
		end setX
		
		on setY(y)
			set properties to {y:y} & properties
			return
		end setY
	end script
end newObject

set z to newObject()'s init()
z's properties --result:{x:0, y:0}
z's setX(5)
z's properties --result:{x:5, y:0}

You can of course combine this example structure with the example code of my previous code but seems a bit of unnecessary code.