Accessing Properities Accross Script Objects

This is probably a really basic question, but for some reason I can’t find the answer to it. I’ve created a script object - thePreferences - which contains all the preferences for my application, and I’ve instantiated it via interface builder. Many of my other script objects have also been instantiated via interface builder, so I’ve connected these in interface builder so that all these objects have access to the preferences. So far so good.

The problem is that I’ve got another applescript with a parent class and a bunch of children, too many that I don’t want to instantiate them in interface builder, and I can’t figure out how to get them access to the properties in thePreferences. Currently I’m using load script to load the applescript and then copying the individual script objects to instantiate them. Though I think I’ve come a far way with this ASOC stuff, I think I’m missing something fundamental here.

Thanks in advance for any help.

If it were Objective-C you could rely on inheritance, but inheritance doesn’t work between two AS classes.

Probably the easiest solution is to have a property in your app delegate script that is connected to the preferences object. Then when your other scripts want to talk to the preferences object, they can do so by calling the app delegate like this:

tell current application’s NSApp’s delegate() to set thePrefs to thePreferences()

If you save them as classes, you can use alloc()'s init(). OTOH,I think load script gives you inheritance of properties. Not sure what the copying is about…

That works perfectly. Thanks.

Maybe I wasn’t doing things correctly in my applescript studio days. To be honest, I’m still trying to wrap my head around the difference between classes and instances. To help organize things I would define a number of scripts objects in various files. If I understand this stuff correctly, those are my classes. Then I would use something like:

		set myScriptLibrary to load script(myScriptsFilePath)
		copy aScriptObject of myScriptLibrary to myScriptObject
tell myScriptObject to runSomething()

The myScriptObject would then be my instance of the class defined in myScriptLibrary. Am I not thinking of this in the correct terms? Though I’m using interface builder to instantiate most of my classes, I will at times need multiple instances of the child classes I mentioned in my original post, and I was thinking that I would use this method to accomplish this.

Well if you’re talking about libraries of AS handlers, the difference is really only in how you address them. It starts to matter if they have properties – then you need instances.

If they’re normal scripts (no script object, no NSObject parent), you can use load script. But if you are making them as ASObjC classes, you can load them using alloc()'s init(). But it probably doesn’t matter all that much which way you do it.

I went with something like this:


tell current application's MyClass to set MyInstance to alloc()'s init()

Since MyClass is in a separate file, I thought that I would still have to use something like load script, but it works without it. I figured that current application can “see” all the classes in the framework, but does this mean that current application also automatically “sees” the classes in all the files within a project? It seems so.

Yep.

One more related question regarding init(). If I understand correctly how inheritance works, if I want to add a few things that are done when MyClass is instantiated, I could stick the following code in MyClass:


on init()
-- do my initialization stuff here
continue init() -- this runs all the stuff intit() normally does?!?
end init()

WIll that do what I’m thinking that it will do?

As I was thinking some more about init() I have a follow up to the above question. When I instantiate a class via interface builder, is init() run and so can I use the above to add what happens during that instantiation.

The “continue init()” statement should be the first line of the handler, and it should end with “return me”.

Thanks Shane! And I answered my own follow up question. It does appear to run on start up.

My apologies. I have yet another init() question. When I stick the handler in a parent class, it doesn’t appear to be called when a child is instantiated. Is there some trick I need for that?

You’re hitting one of the limitations of ASObjC: scripts classes don’t inherit from other script classes, only from Objective-C classes. I’m also not keen on implementing init in AS superclasses – as far as I can see, they can’t match the Objective-C method exactly.

Assuming you never use the superclass directly, you could make a handler in it, say setUp(), and then in your subclasses call it like this:

tell my parent to setUp()

That said, you might be able to use init in an AS subclass of another AS class by changing “continue init()” to “tell my parent to init()”.

Ok. Thanks! I’ll play around with that.