Lots of help from: http://codeintensity.blogspot.com/2008/01/rubycocoa-rocks.html
This is what I learned from attempting to create a RubyCocoa, Core Data, non-document-based application in Xcode that could handle a 'URL scheme' as defined by OS X.
Lots of help from: http://codeintensity.blogspot.com/2008/01/rubycocoa-rocks.html
This is what I learned from attempting to create a RubyCocoa, Core Data, non-document-based application in Xcode that could handle a 'URL scheme' as defined by OS X.
# This file should be created by default - no need to | |
# modify it if so (this is the default content). | |
require 'osx/cocoa' | |
def rb_main_init | |
path = OSX::NSBundle.mainBundle.resourcePath.fileSystemRepresentation | |
rbfiles = Dir.entries(path).select {|x| /\.rb\z/ =~ x} | |
rbfiles -= [ File.basename(__FILE__) ] | |
rbfiles.each do |path| | |
require( File.basename(path) ) | |
end | |
end | |
if $0 == __FILE__ then | |
rb_main_init | |
OSX.NSApplicationMain(0, nil) | |
end | |
<key>CFBundleURLTypes</key> | |
<array> | |
<dict> | |
<key>CFBundleURLName</key> | |
<string>A Something URL that points to a Something</string> | |
<key>CFBundleURLSchemes</key> | |
<array> | |
<string>smth</string> | |
</array> | |
</dict> | |
</array> |
class String | |
# Translate a four-character code to some numeric value | |
# apparently used internally by OS X. | |
def to_fcc | |
raise unless self.length == 4 | |
self.unpack('N').first | |
end | |
end |
# This will replace NSArrayController as the class of your | |
# Core Data object's controller. In my case, my primary | |
# model is that for a 'Something'. In the Interface Builder, | |
# after creating an NSArrayController and attaching it to | |
# the relevant elements of your interface, make sure it | |
# uses SomethingsController instead of the default | |
# NSArrayController as the 'class'. | |
class SomethingsController < OSX::NSArrayController | |
def awakeFromNib | |
registerMyApp | |
# Whatever else... | |
end | |
def registerMyApp | |
OSX::NSAppleEventManager.sharedAppleEventManager. | |
setEventHandler_andSelector_forEventClass_andEventID_( | |
self, :getUrl_withReplyEvent, 'GURL'.to_fcc, 'GURL'.to_fcc) | |
end | |
def getUrl_withReplyEvent(event, reply) | |
url = event.paramDescriptorForKeyword('----'.to_fcc).stringValue | |
# Utilize the URL in whatever way you desire. | |
end | |
end | |