Object Oriented Programming with Applescript

Hi all again,

Over the past couple of weeks I’ve been attempting to delve into programming and applescript in particular for the first time in ten years. I’m attempting to create a script which will make it a hell of a lot easier for Scrum Masters to report to management in future regardless of the tools they use.

Part of this has led me from messy code to OOP. Well, I’m now confused. I just don’t know the detail of instantiating a sub-class from a parent. Do I do it from a method inside the parent? Do I treat them separately and create ID’s in both classes? I’ve no idea.

The information I’m attempting to manage all relate to a Project then a Sprint then a Feature. How can I relate them in such a way that I can say “get description of feature x of sprint y of project z” or equivalent? Or, even “get all features in sprint y”?

Code below…

set project to CM_Project()
set sprint to CM_Sprint()
set feature to CM_Feature()

project's set_project_name("The Brilliant Project")
project's set_project_start_date("15/09/2012")
project's set_project_end_date("15/09/2013")
project's set_project_sprint_length(3)

log project's get_project_name()
log project's get_project_calendar_days()


on CM_Project()
	script CM_Project
		property projectName : missing value
		property startDate : missing value
		property endDate : missing value
		property calendarDays : missing value
		property workingDays : missing value
		property sprintLength : missing value
		property sprintTotal : missing value
		property sprintList : missing value
		property sprintCount : missing value
		on init()
			set projectName to ""
			set startDate to ""
			set endDate to ""
			set calendarDays to ""
			set workingDays to 0
			set sprintLength to 0
			set sprintTotal to 0
			set sprintList to {}
			set sprintCount to 0
			return me
		end init
		on set_project_name(PNValue)
			set projectName to PNValue
		end set_project_name
		on set_project_start_date(SDValue)
			set startDate to SDValue
		end set_project_start_date
		on set_project_end_date(EDValue)
			set endDate to EDValue
		end set_project_end_date
		on set_project_sprint_length(SLValue)
			set sprintLength to SLValue
		end set_project_sprint_length
		on get_project_name()
			return projectName
		end get_project_name
		on get_project_start_date()
			return startDate
		end get_project_start_date
		on get_project_end_date()
			return endDate
		end get_project_end_date
		on get_project_calendar_days()
			set calendarDays to ((date (get_project_start_date() as string)) - (date (get_project_end_date() as string))) div days
			return calendarDays
		end get_project_calendar_days
		on get_project_working_days()
			return workingDays
		end get_project_working_days
		on get_project_sprint_length()
			return sprintLength
		end get_project_sprint_length
		on get_project_sprint_total()
			set sprintTotal to (get_project_calendar_days / 7) / get_project_sprint_length
			return sprintTotal
		end get_project_sprint_total
		on create_new_sprint()
			set sprintCount to sprintCount + 1
			set end of sprintList to sprintCount
		end create_new_sprint
	end script
	return CM_Project's init()
end CM_Project

on CM_Sprint()
	script CM_Sprint
		property parentProject : missing value
		property sprintNumber : missing value
		property startDate : missing value
		property endDate : missing value
		property NoOfTotalFeatures : missing value
		property NoOfTotalPoints : missing value
		property NoOfCompletedPoints : missing value
		property NoOfInProgressPoints : missing value
		property NoOfInBacklogPoints : missing value
		property NoOfCompletedFeatures : missing value
		property NoOfInProgressFeatures : missing value
		property NoOfInBacklogFeatures : missing value
		on init()
			set parentProject to ""
			set sprintNumber to 0
			set starDate to ""
			set endDate to ""
			set NoOfTotalFeatures to 0
			set NoOfTotalPoints to 0
			set NoOfCompletedPoints to 0
			set NoOfInProgressPoints to 0
			set NoOfInBacklogPoints to 0
			set NoOfInProgressFeatures to 0
			set NoOfInBacklogFeatures to 0
			return me
		end init
	end script
	return CM_Sprint's init()
end CM_Sprint

on CM_Feature()
	script CM_Feature
		property parentSprint : missing value
		on init()
			return me
		end init
	end script
	return CM_Feature's init()
end CM_Feature

You can’t create custom classes in AppleScript. Classes and Script Objects are two different things (I know this led into discussions in the past) but the Script Object is as close as you can get to a full class. An class is a blue print of how an object should look like when it’s alive. In AS an Script Object is already an object at run time and is not an class.

Because Script Object are not classes they aren’t types either so you can’t say “script object index of …” like you can with text class: “text 1 of list”. To make this work similar you have to create an array of and get them by index.

When talking about properties you can also see a difference between AppleScript classes and Script Objects because you can’t code like:

set properties of scriptObject to  {firstname:"john", lastname:"doe"}

Sorry to disappoint you and I don’t consider AppleScript as a truly Object Oriented language. Like most people say about AppleScript: “It’s a scripting language with some object oriented features!”

EDIT: The solution to your question “get description of feature x of sprint y of project z” you should use

get description of item x of featureList of item y of sprintList of item z of projectList 

Hi DJ. I am learning OOP in Python now, great explanation.

Great! I really love Python, but never got the change to really feel comfortable in the language. I’ve had a Car-PC once with ISO-Linux and PyCar installed. To add some features (and remove features for USA only) I had to change the code who was written in Python. Unfortunately it’s the only project I worked on that was written in Python, but I loved the code.


Have a look in this thread about object oriented programming with Apple Script.

I agree that python can look beautiful, at least if you compare with perl, but perl on the other hand can look down right sexy, but oh so not understandable after a couple of months away. Python on the other hand is readable.

I want to learn go. :slight_smile: