- Computers & Software»
- Computer Science & Programming
How-to Develop applications with MacRuby on XCode 4 | App for Mac
MacRuby, unlike its predecessor RubyCocoa, is intimately tried to the Objective-C runtime and tightly coupled with the Cocoa Framework. A great way to learn MacRuby is to learn Ruby and to learn the Cocoa Framework. Both of these are beyond the scope of this article.
The first article in this series, “Ruby & Gems; How To Package and Deploy on a Apple Macbook” shows how to setup the foundation of a MacRuby app in XCode 4. This second installment will demonstrate the proverbial “Hello world” application.
I will pick up where I left off with the Rubies application first created in the first installment and add a button and label to display Hello and to also say “Hello” using the Audio libraries in Cocoa.
Start by locating and opening the AppDelegate.rb file and add the following code to the applicationDidFinishLaunching method:
speak = "com.apple.speech.synthesis.voice.GoodNews" @voice = NSSpeechSynthesizer.alloc.initWithVoice(speak)
You will also need to add the AppKit framework
Here is what the code in the file looks like:
# # AppDelegate.rb # Rubies # # Created by Kevin Languedoc on 7/31/11. # Copyright 2011 kCodebook. All rights reserved. # framework 'AppKit' class AppDelegate attr_accessor :window def applicationDidFinishLaunching(a_notification) # Add code to initialize the Hello speak command speak = "com.apple.speech.synthesis.voice.GoodNews" @voice = NSSpeechSynthesizer.alloc.initWithVoice(speak) end
Next we will add a method to handle the closing of the window and another method to make your app say Hello World when the button is clicked.
To handle the closing of the window, add the following code right after the applicationDidFinishLaunching method.
def windowWillClose(a_notification) puts "GoodBye!" exit end
The following method interacts with the button and voice synthesizer:
def say_helloWorld(sender) @voice.startSpeakingString("Hello World!") puts "Hello World!" end
Now that the AppDelegate class is setup, I am going to add some code to setup the window and button.
At the end (outside) of the AppDelegate class, add the following code:
#initialize app object app = NSApplication.sharedApplication app.delegate = AppDelegate.new #setup window window = NSWindow.alloc.initWithContentRect([200, 300, 300, 500], # define the style styleMask:NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask, backing:NSBackingStoreBuffered, defer:false) # add some configuration like the window title and the type of window to create. Also # you will need to add the window to the app window.title = 'Hello World' window.level = NSModalPanelWindowLevel window.delegate = app.delegate # Now add a Button object and set its location (x, y) and size (width, height) button = NSButton.alloc.initWithFrame([80, 10, 120, 80]) # Define the label on the button, add the button to the app and specify which method to call. button.bezelStyle = 4 button.title = 'Say Hello World!' button.target = app.delegate button.action = 'say_helloWorld:' # Also add the button to the window window.contentView.addSubview(button) # Finally load the window and run the app window.display window.orderFrontRegardless app.run
At this point to can either compile the app into an app archive for sharing or run it the XCode IDE. Turn up your speakers before clicking on the button.
To learn how to get started with MacRuby in Xcode 4, read the first article:
How To Build GUI in MacRuby and Xcode