[MacRuby-devel] Mixing Objective-C and Ruby classes

Brian Chapados chapbr at gmail.com
Tue May 19 17:47:07 PDT 2009


I'm not sure how you're testing this, but beware of doing this from a
command line-only script or through macirb.  I suspect you need to be
in an Application context (or hook into the event loop through some
other means).  It works from a minimal hotcocoa app:
http://gist.github.com/114523

I also made some slight modifications to your Obj-C code to add a
delegate that gives you a callback hook that you can define in Ruby.
There is probably a better way to do this, but this was fast.
see: http://gist.github.com/114521

Note that I changed the keyboard shortcut to controlKey+optionKey, so
you'll need to press Control+Option+Space.


On Tue, May 19, 2009 at 4:33 PM, isaac kearse <isaackearse at gmail.com> wrote:
> Hey thanks Brian that worked!
> I tried compiling with the Carbon Framework but I didn't think to try it
> with both Carbon and Foundation.  I still got the warning "makes pointer
> from integer without a cast" but I'm just ignoring that :)
> So the bundle was created and I compiled it and required it my app.
> But when I actually call it with "Shortcut.new.addShortcut" (from the start
> method in my application.rb) I get a nasty error:
> isaac:~/src/tasks isaac$ macrake
> (in /Users/isaac/src/tasks)
> /Users/isaac/src/tasks/Tasks.app/Contents/Resources/lib/application.rb:12:
> [BUG] Segmentation fault
> MacRuby version 0.4 (ruby 1.9.1) [universal-darwin9.0, x86_64]
> -- stack frame ------------
> 0000 (0xc08000060): 00000004
> 0001 (0xc08000068): 00000000
> 0002 (0xc08000070): 00000004
> 0003 (0xc08000078): 00000000
> 0004 (0xc08000080): 00000004
> 0005 (0xc08000088): 00000000
> 0006 (0xc08000090): 00000004
> 0007 (0xc08000098): 80003ffe0
> 0008 (0xc080000a0): 00000004
> 0009 (0xc080000a8): 00000000
> 0010 (0xc080000b0): 00000004
> 0011 (0xc080000b8): 00000000
> 0012 (0xc080000c0): 00000004
> 0013 (0xc080000c8): 00000000
> 0014 (0xc080000d0): 8004f3380
> 0015 (0xc080000d8): 00000004
> 0016 (0xc080000e0): 00000000
> 0017 (0xc080000e8): 8004eb020
> 0018 (0xc080000f0): 00000004
> 0019 (0xc080000f8): 00000000 <- lfp <- dfp
> -- control frame ----------
> c:0008 p:---- s:0020 b:0020 l:000019 d:000019 CFUNC  :addShortcut
> c:0007 p:0019 s:0017 b:0017 l:000016 d:000016 METHOD
> /Users/isaac/src/tasks/Tasks.app/Contents/Resources/lib/application.rb:12
> c:0006 p:0103 s:0014 b:0014 l:000013 d:000013 TOP
>  /Users/isaac/src/tasks/Tasks.app/Contents/Resources/lib/application.rb:297
> c:0005 p:---- s:0012 b:0012 l:000011 d:000011 FINISH :set_encoding:
> c:0004 p:---- s:0010 b:0010 l:000009 d:000009 CFUNC  :load
> c:0003 p:0043 s:0006 b:0006 l:000005 d:000005 TOP
>  /Users/isaac/src/tasks/Tasks.app/Contents/Resources/rb_main.rb:2
> c:0002 p:---- s:0004 b:0004 l:000003 d:000003 FINISH :inherited:
> c:0001 p:0000 s:0002 b:0002 l:000001 d:000001 TOP
> ---------------------------
> DBG> :
> "/Users/isaac/src/tasks/Tasks.app/Contents/Resources/lib/application.rb:12:in
> `addShortcut'"
> DBG> :
> "/Users/isaac/src/tasks/Tasks.app/Contents/Resources/lib/application.rb:12:in
> `start'"
> DBG> :
> "/Users/isaac/src/tasks/Tasks.app/Contents/Resources/lib/application.rb:297:in
> `<top (required)>'"
> DBG> : "/Users/isaac/src/tasks/Tasks.app/Contents/Resources/rb_main.rb:2:in
> `load'"
> DBG> : "/Users/isaac/src/tasks/Tasks.app/Contents/Resources/rb_main.rb:2:in
> `<main>'"
> -- backtrace of native function call (Use addr2line) --
> 0x10010f2c1
> 0x100029334
> 0x100029418
> 0x1000c240d
> 0x7fff803763fa
> 0x0
> 0x7fff8108094d
> 0x105a31e8c
> 0x100121855
> 0x10010af7d
> 0x10010074f
> 0x1001056e2
> 0x10010596c
> 0x100030c96
> 0x100030d15
> 0x10010ada7
> 0x10010074f
> 0x1001056e2
> 0x10010596c
> 0x10002cd32
> 0x1000300f7
> 0x100118723
> 0x100000f84
> 0x100000f40
> -------------------------------------------------------
> Any ideas?
> Cheers,
> Isaac
> On Wed, May 20, 2009 at 10:59 AM, Brian Chapados <chapbr at gmail.com> wrote:
>>
>> Those symbols are defined in the Carbon framework. Try compiling with:
>>
>> cc shortcut.m -o shortcut.bundle -g -framework Foundation -framework
>> Carbon -dynamiclib -fobjc-gc -arch i386 -arch x86_64
>>
>> On Tue, May 19, 2009 at 1:32 PM, isaac kearse <isaackearse at gmail.com>
>> wrote:
>> > Hi Guys,
>> > I am writing a hotcocoa app, and I want to register a global keyboard
>> > shortcut so that I can perform an action in my app from any application.
>> > AFAIK you need to go down to Carbon to do this as documented here:
>> >
>> > http://cocoasamurai.blogspot.com/2009/03/global-keyboard-shortcuts-with-carbon.html
>> > This is the code I have so far: http://gist.github.com/114372
>> > When I try and package this as a bundle using the technique that Laurent
>> > demonstrated earlier in this thread I get these errors:
>> > isaac:~/src/tasks isaac$ gcc shortcut.m -o shortcut.bundle -g -framework
>> > Foundation -dynamiclib -fobjc-gc -arch i386 -arch x86_64
>> > shortcut.m: In function ‘-[Shortcut addShortcut]’:
>> > shortcut.m:25: warning: passing argument 1 of ‘InstallEventHandler’
>> > makes
>> > pointer from integer without a cast
>> > shortcut.m:28: warning: passing argument 4 of ‘RegisterEventHotKey’
>> > makes
>> > pointer from integer without a cast
>> > Undefined symbols:
>> >   "_GetApplicationEventTarget", referenced from:
>> >       -[Shortcut addShortcut] in cckmYEc0.o
>> >       -[Shortcut addShortcut] in cckmYEc0.o
>> >   "_RegisterEventHotKey", referenced from:
>> >       -[Shortcut addShortcut] in cckmYEc0.o
>> >   "_InstallEventHandler", referenced from:
>> >       -[Shortcut addShortcut] in cckmYEc0.o
>> > ld: symbol(s) not found
>> > collect2: ld returned 1 exit status
>> > Undefined symbols:
>> >   "_GetApplicationEventTarget", referenced from:
>> >       -[Shortcut addShortcut] in ccIWfnrB.o
>> >       -[Shortcut addShortcut] in ccIWfnrB.o
>> >   "_RegisterEventHotKey", referenced from:
>> >       -[Shortcut addShortcut] in ccIWfnrB.o
>> >   "_InstallEventHandler", referenced from:
>> >       -[Shortcut addShortcut] in ccIWfnrB.o
>> > ld: symbol(s) not found
>> > collect2: ld returned 1 exit status
>> > lipo: can't open input file:
>> > /var/folders/sm/smWEZrv7GueXZu2JpgAAuU+++TI/-Tmp-//ccX5Acy0.out (No such
>> > file or directory)
>> > I really have no idea what I'm doing here, so any ideas would be
>> > appreciated
>> > :)
>> > Cheers,
>> > Isaac
>> > On Mon, Apr 20, 2009 at 11:49 PM, victor jalencas
>> > <macruby-devel at principia.info> wrote:
>> >>
>> >> Many thanks John and Laurent.
>> >>
>> >>  After reading your messages I came back to XCode and discovered I had
>> >> made a typo in the ObjC part of the code (which, curiously, compiled)
>> >> and that's why my ruby code didn't find my selector.
>> >>
>> >>  As for calling ruby code from the obj-c side, I agree it's a bit
>> >> convoluted (not to mention deprecated). I hope there's a better way to
>> >> do that in store for upcoming versions. If I understood well your
>> >> example, you first created the Obj-C subclass and later, dynamically,
>> >> set its parent to be the ruby class. Anyway, seeing that the norm in
>> >> Cocoa is delegation rather than extension, and if all ruby classes are
>> >> readily available in Obj-C, I think I'll manage with just calling ruby
>> >> code instead of extending it. Apparently I lacked only calling the
>> >> sharedRuntime method, will need to experiment a bit more when I get
>> >> back to my Mac.
>> >>
>> >>
>> >>
>> >> cheers,
>> >> Victor
>> >>
>> >> --
>> >> --
>> >> Victor Jalencas <victor at carotena.net>
>> >> _______________________________________________
>> >> MacRuby-devel mailing list
>> >> MacRuby-devel at lists.macosforge.org
>> >> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>> >
>> >
>> > _______________________________________________
>> > MacRuby-devel mailing list
>> > MacRuby-devel at lists.macosforge.org
>> > http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>> >
>> >
>> _______________________________________________
>> MacRuby-devel mailing list
>> MacRuby-devel at lists.macosforge.org
>> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>
>
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>
>


More information about the MacRuby-devel mailing list