Script libraries with GUIs: is it possible?

Well I just thought it might come in handy when working with nibs, eg sub classing or delegates etc. But not necessary of course.

I don’t think you can actually write the library in Xcode since Xcode scripts are class-based and as Shane said, this doesn’t work in libraries. All you’re doing here is creating a dummy project solely for the sake of making connections in the nib, then you can copy the nib into your library.
Eg, your App Delegate might look like this:

script AppDelegate
	property myOutlet : missing value
	on myAction:sender
	end myAction
end script

Then in the nib you set the class of File’s Owner to “AppDelegate” so you can connect your outlets and actions to it.

I’ve been playing around a bit with this nib loading stuff and have a problem where if I show the window using makeKeyAndOrderFront: (in a stay-open app) the window is completely unresponsive, even for mousing over the close widgets. I can make it work using modal instead but was hoping someone might have an idea of why this problem happens.

How are you loading the nib? Might help if you show some code.

Using loadNibNamed with connections set in the nib. I’ve tried a few different ways though, such as init/instantiate and using an object enumerator instead of connections, all do the same thing.

property testWindow : missing value

on showWindow()
	set myBundle to my (NSBundle's bundleWithPath:(POSIX path of (path to me)))
	myBundle's loadNibNamed:"MainMenu" owner:me topLevelObjects:(missing value)
	testWindow's makeKeyAndOrderFront:(null)
end showWindow

I made a nib called Test.nib. It contains a window, and the window has a button with an action of doIt: targeted at First Responder. I added it to the Resources folder of a lib containing the following:

use scripting additions
use framework "Foundation"
use framework "AppKit"

on showWindow()
	set myBundle to my (NSBundle's bundleWithPath:(POSIX path of (path to me)))
	set {theResult, theArray} to myBundle's loadNibNamed:"Test" owner:me topLevelObjects:(reference)
	set theWindow to theArray's objectAtIndex:0
	theWindow's makeKeyAndOrderFront:(null)
end showWindow

on doIt:sender
	display dialog "Here"
end doIt:

I then called the script using:

use theLib : script "With nib"
theLib's showWindow()

The dialog appears, and when I hit the button, the “Here” dialog appears.

How are you running the script?
I just tried exactly what you’ve done there and found the window was responsive if I just clicked “run” in Script Editor, but when I save as a “stay-open” applet and launch the applet it again wouldn’t respond.

P.S. How did you connect a custom action to First Responder?

You’re right – I was running from ASE. Odd…

Just connect it to a method of the same name in the .xib’s owner script.

I found the problem is avoided if I do a “display alert” before showing the window. Any ideas why, or if there might be a less-intrusive solution?

Not really. Display alert probably does some regular tickling of the run loop somehow.

For reference, I encountered a similar problem when checking out the sample script library from your website which created a window with a progress bar. Worked fine within script editor, but when saved as an applet it gave me this error when it tried to order in the window:
Error (1000) creating CGSWindow on line 263
Again, doing a “display alert” beforehand avoided the problem.

Remove the tell application “SystemUIServer” – it should work OK then.

Well yes, that allows the “choose file” to avoid the problem, which is fine, but I’m wondering why the problem happens and if it can be avoided without showing some sort of UI from standard additions beforehand. Not a big deal, I’m fine with using “display alert” to avoid it for now, I’m just curious :slight_smile:

It looks like an applet can’t make a connection to the window server by itself. Looks like a bug to me.

Gannet,

You might want to download the WebKit Utilities example from here:

macosxautomation.com/mavericks/libraries/examples.html

I’m snowed under at the moment, but it seems work OK…

Update: OK, so that’s saved as a Cocoa-AppleScript app, which is different. I fear we’re out of luck.

Thanks Shane.
I hope they fix it sometime but in the meantime I guess I’ll just have to live with it.