[macruby-changes] [2937] MacRubyWebsite/trunk

source_changes at macosforge.org source_changes at macosforge.org
Sun Nov 1 20:07:00 PST 2009


Revision: 2937
          http://trac.macosforge.org/projects/ruby/changeset/2937
Author:   mattaimonetti at gmail.com
Date:     2009-11-01 20:06:58 -0800 (Sun, 01 Nov 2009)
Log Message:
-----------
updated the talks & events sections + added 3 recipes

Modified Paths:
--------------
    MacRubyWebsite/trunk/lib/events.rb
    MacRubyWebsite/trunk/lib/talks.rb

Added Paths:
-----------
    MacRubyWebsite/trunk/content/recipes/browse-for-folder-or-file-dialog.txt
    MacRubyWebsite/trunk/content/recipes/embed-a-custom-font.txt
    MacRubyWebsite/trunk/content/recipes/how-to-play-an-audio-file.txt

Added: MacRubyWebsite/trunk/content/recipes/browse-for-folder-or-file-dialog.txt
===================================================================
--- MacRubyWebsite/trunk/content/recipes/browse-for-folder-or-file-dialog.txt	                        (rev 0)
+++ MacRubyWebsite/trunk/content/recipes/browse-for-folder-or-file-dialog.txt	2009-11-02 04:06:58 UTC (rev 2937)
@@ -0,0 +1,70 @@
+--- 
+title:      Browse for folder or file dialog
+created_at: 2009-10-25 18:51:36.516659 -07:00
+recipe:     true
+author:     Matt Aimonetti
+filter:
+  - erb
+  - textile
+--- 
+h1(title). <%= h(@page.title) %>
+
+<div class="author">
+  By <%= member_name(@page.author) %>
+</div>
+
+<div class='recipe'>
+
+  This is yet another pretty simple tip. 
+  Use case: let say you want your applications users to choose one or multiple files or folder on their file system. A good example would be that you want the user to choose a file to process or a folder where to save some data.
+
+  <p>
+    <br/>
+  <img class="aligncenter" title="MacRuby, file browser dialog" src="http://img.skitch.com/20091025-nc89xd2ywqutqqddnwm2met3x4.jpg" alt="" width="389" height="43" />
+  </p>
+  
+  In the example above, I added a browse button and a text field.
+
+  I would like my users to click on the browse button, locate a folder and display it in the text field.
+
+  In your MacRuby controller, use a simple action method as well as an accessor to the text field:
+
+<% coderay :lang => 'ruby' do -%>
+attr_accessor :destination_path
+
+def browse(sender)
+end
+<% end %>
+
+  Now, in Interface builder bind the destination_path outlet to the text field you want to use to display the path and bind the button to the browse action.
+
+  Let's go back to our action method and let's create a dialog panel, set some options and handle the user selection:
+
+<% coderay :lang => 'ruby' do -%>
+def browse(sender)
+  # Create the File Open Dialog class.
+  dialog = NSOpenPanel.openPanel
+  # Disable the selection of files in the dialog.
+  dialog.canChooseFiles = false
+  # Enable the selection of directories in the dialog.
+  dialog.canChooseDirectories = true
+  # Disable the selection of multiple items in the dialog.
+  dialog.allowsMultipleSelection = false
+
+  # Display the dialog and process the selected folder
+  if dialog.runModalForDirectory(nil, file:nil) == NSOKButton
+  # if we had a allowed for the selection of multiple items
+  # we would have want to loop through the selection
+    destination_path.stringValue = dialog.filenames.first
+  end
+end
+<% end %>
+
+  That's it, your user can now browse for a folder and the selection will be displayed in the text field. Look at the <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSOpenPanel_Class/Reference/Reference.html" target="_blank">NSOpenPanel documentation</a> for more details on the Cocoa API.
+  
+</div>
+
+<span>
+  "Original post":http://merbist.com/2009/10/25/macruby-browse-for-folder-or-file-dialog/
+</span>
+

Added: MacRubyWebsite/trunk/content/recipes/embed-a-custom-font.txt
===================================================================
--- MacRubyWebsite/trunk/content/recipes/embed-a-custom-font.txt	                        (rev 0)
+++ MacRubyWebsite/trunk/content/recipes/embed-a-custom-font.txt	2009-11-02 04:06:58 UTC (rev 2937)
@@ -0,0 +1,41 @@
+--- 
+title:      Embed a custom font
+created_at: 2009-10-06 18:51:36.516659 -07:00
+recipe:     true
+author:     Matt Aimonetti
+filter:
+  - erb
+  - textile
+--- 
+h1(title). <%= h(@page.title) %>
+
+<div class="author">
+  By <%= member_name(@page.author) %>
+</div>
+
+<div class='recipe'>
+
+Let say you want to release your MacRuby app and use a custom embedded font?
+You probably don't want to force your users to install the font.
+Well, don't worry, just put the font file in your resources folder and use the following code:
+
+<% coderay :lang => 'ruby' do -%>
+font_location = NSBundle.mainBundle.pathForResource('MyCustomFont', ofType: 'ttf')
+font_url = NSURL.fileURLWithPath(font_location)
+# in MacRuby, always make sure that cocoa constants start by an uppercase
+CTFontManagerRegisterFontsForURL(font_url, KCTFontManagerScopeProcess, nil)
+<% end %>
+
+That’s it, now your custom font is available and you can change your textfield instance’s font like that:
+
+<% coderay :lang => 'ruby' do -%>
+text_field.font = NSFont.fontWithName('MyCustomFont', size:24)
+<% end %>
+
+The only tricky things here were to know the Cocoa API call and to know that even if the Cocoa API references the constant to use as kCTFontManagerScopeProcess, because in Ruby, constants start by an uppercase, you need to convert it to: KCTFontManagerScopeProcess.
+
+</div>
+
+<span>
+  "Original post":http://merbist.com/2009/10/06/macruby-tips-embed-a-custom-font/
+</span>
\ No newline at end of file

Added: MacRubyWebsite/trunk/content/recipes/how-to-play-an-audio-file.txt
===================================================================
--- MacRubyWebsite/trunk/content/recipes/how-to-play-an-audio-file.txt	                        (rev 0)
+++ MacRubyWebsite/trunk/content/recipes/how-to-play-an-audio-file.txt	2009-11-02 04:06:58 UTC (rev 2937)
@@ -0,0 +1,110 @@
+--- 
+title:      How to play an Audio file
+created_at: 2009-10-07 18:51:36.516659 -07:00
+recipe:     true
+author:     Matt Aimonetti
+filter:
+  - erb
+  - textile
+--- 
+h1(title). <%= h(@page.title) %>
+
+<div class="author">
+  By <%= member_name(@page.author) %>
+</div>
+
+<div class='recipe'>
+
+Let's say you would like to play an audio file in your MacRuby app/script, how would you do?
+It's actually pretty simple, you just need to use NSSound. If we wanted to use a system sound we could do:
+
+<% coderay :lang => 'ruby' do -%>
+NSSound.soundNamed('Basso').play
+<% end %>
+
+But let's look at a more advanced example with some Cocoa patterns. We will loop through all the audio files in a folder and we will play them one after the other.
+
+<% coderay :lang => 'ruby' do -%>
+#!/usr/bin/env macruby
+framework 'Cocoa'
+# Cocoa documentation reference:
+# http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSSound_Class/Reference/Reference.html
+
+def play_sound
+  if @sounds.empty?
+    NSApplication.sharedApplication.terminate(nil)
+  else
+    sound_file = @sounds.shift
+    s = NSSound.alloc.initWithContentsOfFile(sound_file, byReference: false)
+    puts "previewing #{sound_file}"
+    s.delegate = self
+    s.play
+  end
+end
+
+# This is a delegate method called by the sound object
+def sound(sound, didFinishPlaying: state)
+  play_sound if state
+end
+
+# Delegate method called when the app finished loading
+def applicationDidFinishLaunching(notification)
+  @sounds = Dir.glob("/System/Library/Sounds/*.aiff")
+  play_sound
+end
+
+# We are delegating the application to self so the script will know when
+# it finished loading
+NSApplication.sharedApplication.delegate = self
+NSApplication.sharedApplication.run
+<% end %>
+
+On my machine, I get the following output:
+
+<% coderay :lang => 'shell' do -%>
+$ macruby macrubysound.rb
+previewing /System/Library/Sounds/Basso.aiff
+previewing /System/Library/Sounds/Blow.aiff
+previewing /System/Library/Sounds/Bottle.aiff
+previewing /System/Library/Sounds/Frog.aiff
+previewing /System/Library/Sounds/Funk.aiff
+previewing /System/Library/Sounds/Glass.aiff
+previewing /System/Library/Sounds/Hero.aiff
+previewing /System/Library/Sounds/Morse.aiff
+previewing /System/Library/Sounds/Ping.aiff
+previewing /System/Library/Sounds/Pop.aiff
+previewing /System/Library/Sounds/Purr.aiff
+previewing /System/Library/Sounds/Sosumi.aiff
+previewing /System/Library/Sounds/Submarine.aiff
+previewing /System/Library/Sounds/Tink.aiff
+<% end %>
+
+Cocoa delegation might seem a bit strange at first when you come from Ruby. In Ruby we rarely do any type of async delegation so let's quickly look at what's going on in this script.
+
+The first thing we do is loading the cocoa framework which makes total sense, then we define a bunch of methods and finally we get to:
+
+<% coderay :lang => 'ruby' do -%>
+NSApplication.sharedApplication.delegate = self
+NSApplication.sharedApplication.run
+<% end %>
+
+We are setting the run loop to delegate to set, which means that when an event is triggered, the delegation method will be called on self (our script).
+Once that done, we are starting out run loop.
+
+Once the application is loaded the applicationDidFinishLaunching delegate is being triggered (line 24). At this point, we are looking for all the sound files in the sound system folder and storing them in an instance variable (line 25). Finally, we are calling the play_sound method (line 26).
+
+The play_sound method checks that we have some audio files left to play (line 7), otherwise it quits the app. (line 8 ) If we still have some files in the queue, we get the first one (line 10) and use it to create an instance of NSSound (line 11).
+
+Before playing the NSSound instance, we are setting its delegate to our script (self) (line 13). If you read <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSSound_Class/Reference/Reference.html" target="_blank">NSSound Cocoa documentation</a> for #play, you will notice that #play "initiates playback asynchronously and returns control to your application. Therefore, your application can continue doing work while the audio is playing." In our case, we don't want to do that, otherwise all the sounds will play at the same time.
+
+The documentation also mentions a delegation you can use to avoid that. This is exactly what we did when we implemented:  def sound(sound, didFinishPlaying: state) (line 19)
+
+Rubyists might be surprised by the method signature. MacRuby extended Ruby to support Objective-C selectors.
+
+When the sound is playing, this delegate gets called, we check on the state of the sound, if it finished playing then we call play_sound again.
+
+That's it!  It's a very elegant implementation giving us the benefits of both Ruby and Cocoa.
+
+<span>
+  "Original post":http://merbist.com/2009/10/06/macruby-tips-how-to-play-an-audio-file/
+</span>
\ No newline at end of file

Modified: MacRubyWebsite/trunk/lib/events.rb
===================================================================
--- MacRubyWebsite/trunk/lib/events.rb	2009-11-02 02:44:13 UTC (rev 2936)
+++ MacRubyWebsite/trunk/lib/events.rb	2009-11-02 04:06:58 UTC (rev 2937)
@@ -4,9 +4,6 @@
   
   def macruby_events
     [
-      Event.new("2-3 Oct 2009", "OSDC.fr", "http://act.osdc.fr/osdc2009fr/", "Paris, France", "Olivier Gutknecht presents MacRuby"),
-      Event.new("13-14 Oct 2009", "Rails Summit Latin America", "http://www.railssummit.com.br/", "São Paulo, Brazil", "Rich Kilmer presents MacRuby"),
-      Event.new("30-31 Oct 2009", "RubyEnRails", "http://2009.rubyenrails.nl", "Amsterdam, Netherlands", "Eloy Duran Presents MacRuby & HotCocoa"),
       Event.new("19-21 Nov 2009", "RubyConf", "http://rubyconf.org/", "San Francisco, CA", "Laurent Sansonetti presents MacRuby"),
       Event.new("19-21 Nov 2009", "RubyConf", "http://rubyconf.org/", "San Francisco, CA", "Matt Aimonetti talks about writing 2D video games with MacRuby")
     ]

Modified: MacRubyWebsite/trunk/lib/talks.rb
===================================================================
--- MacRubyWebsite/trunk/lib/talks.rb	2009-11-02 02:44:13 UTC (rev 2936)
+++ MacRubyWebsite/trunk/lib/talks.rb	2009-11-02 04:06:58 UTC (rev 2937)
@@ -4,6 +4,9 @@
   
   def macruby_talks
     [
+      Talk.new("2009-10-30", "RubyEnRails", "MacRuby & RubyCocoa (slides only)",          "http://tr.im/DCW0", "Eloy Duran", nil),
+      Talk.new("2009-10-14", "RailsSummit", "MacRuby", "http://agaelebe.blip.tv/file/2730084/", "Rich Kilmer", nil),
+      Talk.new("2009-10-03", "OSDC.fr", "Intro to MacRuby (slides only)", "http://www.slideshare.net/olg/macruby-an-introduction", "Olivier Gutknecht", nil),
       Talk.new("2009-05-27", "RailsWayCon", "MacRuby & HotCocoa (slides only)",           "http://www.slideshare.net/freaklikeme/macruby-hotcocoa", "Thilo Utke", nil),
       Talk.new("2009-04-22", "GoGaruCo",  "MacRuby and HotCocoa (slides only)",           "http://www.slideshare.net/mattetti/macruby-hotcocoa-presentation-by-rich-kilmer", "Rich Kilmer", nil),
       Talk.new("2009-01-13", "SDRuby",    "MacRuby: What's the big deal?",                "http://sdruby.com/podcast/58", "Matt Aimonetti", "http://www.slideshare.net/mattetti/macruby-when-objectivec-and-ruby-meet"),
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20091101/5b3fc161/attachment-0001.html>


More information about the macruby-changes mailing list