Created
February 12, 2011 11:38
-
-
Save quackingduck/823712 to your computer and use it in GitHub Desktop.
Based on the example here: http://ofps.oreilly.com/titles/9781449380373/ch01.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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