Externalise functions into a library
Hi,
I've been working on a new Applescript which is intended as a customizable template. When it's done, I will be publishing it on GitHub. But right now, I'm still a long way from it being done. In order to allow updates of the framework provided with this template separately from updates in the custom code bits, I've decided I want to split as much as possible off into a library file. There are a whole number of questions I have about this, as it's the first time I'm trying to do this.
Please keep in mind this is just a fun thing on the side. I'm not a developer, never have been.
I'll reference the following files below ...
- library.scpt is the script with the main, common functions that don't change from one to the next use of this framework
- template.scpt is the template document which provides the minimal structure for using this framework
- main.scpt is an actual script that I am writing, it started out as a copy of template.scpt and then I started changing it
Loading an external library
There are comments all over the web about how to get one applescript to load another, but for some reason none of it seems to work. Although it seems broken again as I write this, I did at one point get the following line to work
set _library to load script alias ((path to desktop as text) & "library.scpt")
Q1: Where do I find a comprehensive reference and step-by-step guide to making the loading of a library script work?
Configuration settings and variables
The idea is that template.scpt contains a number of properties that library.scpt will need to determine behavior. The configuration settings of the framework, if you will. So I make a copy of template.scpt to main.scpt and I start editing these variables. In addition to this, there are certain variables which will be used by functions in library.scpt and in template/main.
Q2: How do I set a property or variable in main.scpt, which can then be used by functions in library.scpt? And how do I set a property or variable inside a specific function in library.scpt, that a function in main.scpt can read and alter if needed?
Structure
There is effectively one function in template/main that does the bulk of the work. This function gets called by the framework, which lives in library.scpt. Let's call that function getLiveData(). My intention is to structure this framework as follows.
library.scpt contains
- a function init() that does the prep-work like validation of files existing, configuration settings existing, setting starting values of variables, etc.
- a function main() that is the main routine that runs the script
- - and a bunch of other functions that are called by init, main or getLiveData.
template/main will look like this
- property setting A-Z that are essentially configuration settings
- function getLiveData() : does the getting of live data, which is what the script is centered around
- init() : invoke the init function
- main() : invoke the main function
Q3: Does that seem like a sensible setup for a split of common and custom stuff?
Validation
One of the things I'm struggling with is how do I validate settings having been made and functions existing in main.scpt. This is effectively what the init() function is for, but I have no idea how I test for a property existing or not.
Q4: In main.scpt I say 'property aSetting : TRUE', how can library.scpt then check if this property exists or not?
Q5: In main.scpt I call init() after the getLiveData() has been declared, but how do I check from init() in library.scpt that a function getLiveData() exists in main.scpt?
Your help and comments will be much appreciated.
And as soon as the script is ready, I'll share it here.
thanks,
Rob
MacBook Air, OS X Mountain Lion (10.8.3), 2GHz i7, 8GB RAM, 256GB SSD