Skip to content

Instantly share code, notes, and snippets.

@quackingduck
Created February 12, 2011 11:38
Show Gist options
  • Save quackingduck/823712 to your computer and use it in GitHub Desktop.
Save quackingduck/823712 to your computer and use it in GitHub Desktop.
#!/usr/bin/env macruby
framework 'AppKit'
module AppDelegate
extend self
def applicationDidFinishLaunching(notification)
voice_type = "com.apple.speech.synthesis.voice.GoodNews"
@voice = NSSpeechSynthesizer.alloc.initWithVoice(voice_type)
end
def say_hello(sender)
@voice.startSpeakingString("Hello World!")
puts "Hello World!"
end
def windowWillClose(notification)
puts "Bye!"
exit
end
end
# Inits NSApp
NSApplication.sharedApplication
NSApp.delegate = AppDelegate
# Allows an app without an applicaiton bundle or info.plist to still be an app
NSApp.setActivationPolicy NSApplicationActivationPolicyRegular
NSWindow.alloc.initWithContentRect(
[200,300,300,100],
styleMask:NSTitledWindowMask|NSClosableWindowMask,
backing: NSBackingStoreBuffered,
defer: false
).tap do |w|
w.title = "MacRuby: The Definitive Guide"
w.delegate = AppDelegate
w.contentView.addSubview(
NSButton.alloc.initWithFrame([80, 10, 120, 80]).tap do |b|
b.bezelStyle = 4
b.title = 'Hello World!'
b.target = AppDelegate
b.action = 'say_hello:'
end
)
w.orderFrontRegardless
end
# Loads this app in front of the terminal (or whatever launched it)
NSApp.activateIgnoringOtherApps true
# Start the run loop
NSApp.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment