Greetings,
I have a handler in my main script that I want to call as a method from a subclass script, but am having no luck.
I have stripped my Xcode project down to two script objects ("CtrlrAppDelegate" and "CtrlrSubclassAppDelegate"), a NIB with one button, and three resources—PNG images ("Red", "Green", and "Blue"), which look like colored LEDs. The project can be downloaded from:
Initially, the button image is "Blue". Upon clicking the button, the handler "buttonClicked_" is triggered, which does these two operations: 1. Call local handler "buttonSetImage_" to set the image to "Red".
2. Call the "changeButtonImage_" handler in the subclass script, which calls back to the main script handler "buttonSetImage_" to change the image to "Green".
Stripped as bare as I can get them, here is the code from the two scripts:
-- main script script CtrlrAppDelegate property parent : class "NSObject" property myButton : missing value -- wired up to button as IB outlet
on buttonClicked_(sender) -- wired up to button as IB action -- first, change button image by direct call buttonSetImage_("Red.png") -- second, change button image by indirect call from subclass script tell current application's CtrlrSubclassAppDelegate to changeButtonImage() end buttonClicked_
on buttonSetImage_(imageName) display dialog "Handler is executing" set theImage to current application's NSImage's imageNamed_(imageName) myButton's setImage_(theImage) end buttonSetImage_
on applicationWillFinishLaunching_(aNotification) end applicationWillFinishLaunching_
on applicationShouldTerminate_(sender) return current application's NSTerminateNow end applicationShouldTerminate_ end script
-- subclass script script CtrlrSubclassAppDelegate property parent : class "CtrlrAppDelegate"
on changeButtonImage() tell current application's CtrlrAppDelegate to buttonSetImage_("Green.png") end changeButtonImage_ end script
The same main-script handler ("buttonSetImage_") is called twice with a string parameter. The dialog box does pop up twice, indicating the handler is successfully called both times. When called from within the main script, it works, but when called from the subclass script, it fails to change the image.
This is the error message (minus the timestamp) reported by Xcode: Ctrlr[4168:303] *** +[CtrlrAppDelegate buttonSetImage:]: unrecognized function setImage_. (error -10000)
Is the failure because the subclass script is passing the parameter as (I'm guessing) a CFString? If so, how would I coerce it to an AS string? I tried "set imageName to imageName as string", but it still fails. And if that's not the issue, does anyone have an idea what is?
Many thanks, Stan C.
|