Hi,
as you all know, with the introduction of the use keyword in AppleScript 2.3 (in OS X Mavericks), AppleScript allows you to run scripts from a Script Libraries folder as follows:
use MyLib : script "aLib"
MyLib's someHandler()
I have tried to make the external library the parent of my script and, to my surprise, it works:
This method has the advantage that no qualification (MyLib’s…) of the external handlers is needed. I have experimented with this approach a bit without issues. But is this way of loading a library safe and portable? I haven’t found it documented anywhere.
No I dont beleive this has been documented, and I also dont see any performance or error inducing problems with this technique.
But one possable issue is that any one script can only have one property parent entry, so if you where using any more than one script library, you would still have to use the “MyLib’s someHandler()” syntax on these other libraries anyway.
So for me I would sooner write it as in your first example, so as to make it clearer and more readable where the method is being called from.
I haven’t tried it yet, but I see great possibilites. I can either import a base-library directly into my context/namespace, like druido pointed out.
But say I have a base library that I use from two other totally different libraries, then I can have that base library as parent in the libraries I set as my parent, or use. Either way, I have a kind of object composition through subclassing, with the subclass being totally unrelated. Provided it will work this way of course.
I’ve found a curious behaviour. Create a script bundle (.scptd), add a Script Libraries folder in its Resources folder, and put a script called myLib.scpt in that Script Libraries folder, with the following content:
on foo()
return 42
end
Then, in the main script of the bundle, loading myLib as follows works fine:
use myLib : script "myLib"
myLib's foo() -- 42
but this fails with a syntax error (“Can’t get script “myLib””):
This seems to be the safest way to set the parent property to an external script. I’d like to know, though, why the second example doesn’t compile (if you put the script in ~/Library/Script Libraries instead of inside the bundle, then it works).