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

isaac kearse isaackearse at gmail.com
Wed May 20 13:14:40 PDT 2009


Brian you are a legend,That works perfectly, no errors or warnings.

I think I might have run into a similar issue when I tried to compile this
framework:
http://github.com/Caged/aixmlserialize/tree/master
(NSXMLDocument => NSDictionary conversion)
I ended up just extracting the one method that I was using and packaging it
into a bundle.

If it happens again I might have to switch to 32bit, or maybe its better to
stick with 64bit as 32bit-only APIs will probably be phased out eventually.

Thanks again for all your help,
Isaac


On Thu, May 21, 2009 at 7:23 AM, Brian Chapados <chapbr at gmail.com> wrote:

> I was able to reproduce your bug, and have a fix. This was somewhat
> obscure, but RTFM to the rescue...
>
> Note that GetApplicationEventTarget is not available on 64-bit systems
> (see Notes in the docs):
>
> http://developer.apple.com/documentation/Carbon/Reference/Carbon_Event_Manager_Ref/Reference/reference.html#//apple_ref/c/func/GetApplicationEventTarget
>
> Although you were not calling it directly, the macro
> InstallApplicationEventHandler() was calling it for you. I had
> compiled macruby with archs="i386" so it was working fine for me.
> When I did a default build it broke.
>
> In general, always double-check any Carbon stuff for
> 64bit-compatibility issues. I think Apple has said that Carbon is
> being phased out.  Maybe someone else knows more about that than I do.
>
> To make a long story short, just use GetEventMonitorTarget() to send
> your hotkey event to the event monitor.  That seems to be available on
> both platforms.
>
> I updated the gists with the new Obj-C code and hotcocoa file.
> Obj-C: http://gist.github.com/114521
> hotcocoa: http://gist.github.com/114523
>
> Brian
>
>
> On Wed, May 20, 2009 at 9:11 AM, Brian Chapados <chapbr at gmail.com> wrote:
> > I didn't use the hotcocoa cli interface to generate the file, so I
> > don't have a Rakefile or anything.  I just used a text editor and
> > followed an old example[1]. Try putting the ruby code into a file
> > called 'shortcutapp.rb' and type:
> >
> > [1]:
> http://andymatuschak.org/articles/2008/09/12/hotcocoa-is-pretty-damn-hot/
> >
> > macruby shortcutapp.rb
> >
> > If you want to package things up into an actual .app bundle, then you
> > would need a slightly different approach.   It shouldn't be too bad,
> > since hotcocoa helps you layout your code in a sensible way.  Make
> > sure the above works first, then go from there.
> >
> > On Wed, May 20, 2009 at 2:53 AM, isaac kearse <isaackearse at gmail.com>
> wrote:
> >> Hi Brian,
> >> I tried running your minimal app but it crashed with a similar error.
>  Here
> >> are all the gory details (including Problem Details and System Profile
> from
> >> the crash reporter):
> >> http://gist.github.com/114723
> >>
> >> I have also put the whole directory structure for the minimal hotcocoa
> app
> >> on github so we can be sure we are running exactly the same app:
> >> http://github.com/isaac/shortcut/tree/master
> >> I also got a few more warnings when compiling shortcut.m but I am
> assuming I
> >> can just ignore them? (I included them at the bottom of the gist also)
> >> Cheers,
> >> Isaac
> >> On Wed, May 20, 2009 at 1:10 PM, isaac kearse <isaackearse at gmail.com>
> wrote:
> >>>
> >>> Yeah I think you're right.  I was calling it inside a hotcocoa app but
> not
> >>> in the application context.  I'll try it out tonight and let you know
> how it
> >>> goes.
> >>> Thanks a lot for walking me through this.
> >>> On Wed, May 20, 2009 at 12:47 PM, Brian Chapados <chapbr at gmail.com>
> wrote:
> >>>>
> >>>> 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
> >>>> >
> >>>> >
> >>>> _______________________________________________
> >>>> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20090521/53d0d1b4/attachment-0001.html>


More information about the MacRuby-devel mailing list