How do I load an AppleScript (class) into another AppleScript (class)?

Can anyone point me in the right direction on how to do this?

You can use load script as always, otherwise you can make a new class (File _> New File) and either call its handlers as class methods, or instantiate it in IB or using alloc()'s init().

I’m having trouble figuring out exactly how to accomplish this. I have a second class script named “test.applescript” in the project. It contains this code:

script testAppDelegate
	property parent : class "NSObject"

	on hello_()
		display dialog "Hello"
	end hello_
end script

My other script has this when a button is clicked:

tell class "test" of current application
end tell

But that doesn’t work… I must be calling it wrong or missing something…

You have to follow Objective-C naming conventions of one underscore for each parameter, or none for your case. You also have to coerce any results and arguments.

Well, that still doesn’t work. Now, my second script has this:

on hello()
	log "HELLO"
	display dialog "hello"
	return "hello"
end hello

and my first script has this:

tell class "test" of current application
end tell

There are no errors, but nothing is logged, no dialog appears and the only thing that returns from the call is this:

<NSAppleEventDescriptor: 'obj '{ 'form':'name', 'want':'pcls', 'seld':'utxt'("test"), 'from':'null'() }>

It works fine here. Is the class name a match for case? (Cocoa classes should begin with a cap, although it won’t stop this from working.) Did you add the second class to the project using File → New File?

The case does match. I just changed them both to title case and it still didn’t work. Yes, I added the new AS class with file - new. Do I need to declare the new class in my original script file somehow?

Nope – it should Just Work.

I just added another new class and tried it again and it worked. I must have done something wrong last time to create the new file… Thanks.

This brings up a related question… Can the first script access properties of the second script? Trying this doesn’t work:

tell class "Custom" of current application
	set x to textProperty
end tell

With textProperty being a property in the class.

Yes. But it does it the Cocoa way, which is indirectly via synthesized property accessor methods, so to access someProperty you’d use:

set x to current application's TestClass's someProperty()
current application's TestClass's setSomeProperty_(x)

You also have to allow for the fact that results and arguments will be converted to Cocoa classes, so if someProperty above is a string, x in the first line will be an NSString and will need to be coerced to text to be used as AS text.

Actually, getting the value of a property is a bit more complicated than I said – you can only get the property of an instance of a class, not of a class itself. Synthesized property accessors are instance methods, not class methods.

So you need to either make an instance of your class in IB and connect it to a property, then use that property as a reference, or initialize an instance in code.

To do the former, make a property in your main script something like:

property testRef: missing value

Then in IB, add an NSObject (blue cube) to the .xib window, change its class to your new class (in the Identity panel), and connect it to the new property in your main script. Then you can use:

 testRef's someProperty()

To do the latter, you use something like:

set myTestInstance to current application's Testing's alloc()'s init()
myTestInstance's someProperty()

I’m also curious about how I would load a class into a variable rather than calling it’s subroutines as we discussed previously in this old post. Any suggestions or examples would be appreciated.

Never mind… figured it out.