(This is sort of a followup of http://macscripter.net/viewtopic.php?id=41963.)
As you know, starting with Yosemite it is possible to use ASObjC in every script, not just in script libraries. So, I have tried to create a script bundle that displays a custom alert. This is a minimal example:
use framework "Foundation"
use framework "AppKit"
use scripting additions
property name : "Minimal UI"
property id : "net.macscripter.minimal-ui"
property version : "0.1"
set scriptBundle to my (NSBundle's bundleWithPath:(POSIX path of (path to me)))
set nib to my (NSNib's alloc()'s initWithNibNamed:"Test" bundle:scriptBundle)
set {didInstantiate, objects} to nib's instantiateNibWithOwner:me topLevelObjects:(reference)
set view to objects's objectAtIndex:0 -- Must be an NSView
set alert to my NSAlert's alloc()'s init()
alert's setMessageText:"Hello"
alert's setInformativeText:"I'd like to work fine"
alert's setAccessoryView:(my view)
alert's runModal()
alert's release()
Note that this is not a script library: this is meant to be run directly (as a script bundle or as an application). Of course, you need first to create a nib file containing an NSView (open Xcode, go to New > File. > View, and export as Test.nib) and put it in the Resources folder of the script or application bundle.
The fact is, this sometimes works, sometimes fails with an error like
And whether it succeeds or not, it is completely random (just try to run it a few times). Besides, when it is launched from osascript, and it happens to work, then after the dialog is dismissed osascript reports:
I know that, in Yosemite, there have been some bug fixes so that now an error occurs when attempting to save a script containing direct or indirect references to Obj-C objects (previously, you could get a segmentation fault on the next run of the script, because a property could have retained an invalid external reference). But in the code above I’m not changing any property and, in any case, even if I reset all the variables before quitting I keep getting that error -1763.
Am I doing something wrong that leads to undefined behavior (in the latest AppleScript release notes, there is an explicit warning about the fact that the developer is responsible for managing the threading of the script correctly, especially when presenting UI elements) or do you think that this exposes a bug?