Skip to content

Instantly share code, notes, and snippets.

@ELLIOTTCABLE
Created September 5, 2008 23:34
Show Gist options
  • Save ELLIOTTCABLE/9058 to your computer and use it in GitHub Desktop.
Save ELLIOTTCABLE/9058 to your computer and use it in GitHub Desktop.
URL protocol handling in RubyCocoa
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment