Mac Tutorial
1. Hello World
Lets build an OS X application called 'Hello World'.
Open your terminal and go to a place where you would like this first project to be created, then type the following command.
$ motion create --template=osx Hello
Note
|
Please note that we are passing the --template=osx option to the motion command here. By default, motion create will create iOS projects. Since we want to create an OS X project, we need to select the OS X project template.
|
This command will create a RubyMotion OS X project in a new directory named Hello. If this directory already exists or cannot be created, the command will fail.
Let’s have a look inside.
$ cd Hello
$ ls
Rakefile app resources spec
A RubyMotion project is Rakefile
-based. rake
is the de-facto Ruby build program. It is similar to make
and it ships with Mac OS X by default.
The app directory contains the application code. The resources directory will eventually contain the resource files of your project, such as icon, image or sound files. The spec directory contains specification/test files.
Let’s run the default task.
$ rake
This should build our project then start the app, and you should see… an empty window! It’s actually normal, we haven’t written any code yet!
If you look inside the app directory you will see an app_delegate.rb file, which is created by default. This file implements the AppDelegate
class, which is responsible for controlling your application.
class AppDelegate
def applicationDidFinishLaunching(notification)
buildMenu
buildWindow
end
end
Open the app/app_delegate.rb file with your favorite editor. We will change the code to do something more interesting, such as triggering an alert.
class AppDelegate
def applicationDidFinishLaunching(notification)
buildMenu
buildWindow
alert = NSAlert.new
alert.messageText = "Hello World!"
alert.runModal
end
end
If you run the rake
command again from the terminal you should be able to see the alert in the simulator.
2. And Now?
Congratulations, you successfully created your first RubyMotion OS X application. That wasn’t too hard, was it?
To continue, we recommend that you check the Samples page as well as the Sample Code Repository on GitHub. Each of the sub-folders contains a RubyMotion project as introduced above. You can type rake in each directory to build and run them and check their source code by reading the files in the 'app' directory.