RubyCocoa and Mac System Icons

Hi There,

I am trying to create an NSImage which contains the system kComputer
icon. Unfortunately RubyCocoa can’t resolve kComputer or
use :kComputer or it’s header value ‘root’… I have tried the
following:

  1. NSWorkspace.sharedWorkspace.iconForFileType(NSFileTypeForHFSTypeCode
    (:root))

  2. NSWorkspace.sharedWorkspace.iconForFileType(NSFileTypeForHFSTypeCode
    (:kComputer))

  3. NSWorkspace.sharedWorkspace.iconForFileType(NSFileTypeForHFSTypeCode
    (kComputer))

  4. NSWorkspace.sharedWorkspace.iconForFileType(NSFileTypeForHFSTypeCode
    (NSNumber.numberWithUnsignedLong(‘root’)))

  5. NSWorkspace.sharedWorkspace.iconForFileType(:root)

  6. NSWorkspace.sharedWorkspace.iconForFileType(:kComputer)

  7. NSWorkspace.sharedWorkspace.iconForFileType(kComputer)
    etc…

Has anybody got this to work in RubyCocoa?
Many Thanks
Tom Medhurst

Tom Medhurst wrote:

Hi There,

I am trying to create an NSImage which contains the system kComputer
icon. Unfortunately RubyCocoa can’t resolve kComputer or
use :kComputer or it’s header value ‘root’… I have tried the
following:

  1. NSWorkspace.sharedWorkspace.iconForFileType(NSFileTypeForHFSTypeCode
    (:root))

  2. NSWorkspace.sharedWorkspace.iconForFileType(NSFileTypeForHFSTypeCode
    (:kComputer))

  3. NSWorkspace.sharedWorkspace.iconForFileType(NSFileTypeForHFSTypeCode
    (kComputer))

  4. NSWorkspace.sharedWorkspace.iconForFileType(NSFileTypeForHFSTypeCode
    (NSNumber.numberWithUnsignedLong(‘root’)))

  5. NSWorkspace.sharedWorkspace.iconForFileType(:root)

  6. NSWorkspace.sharedWorkspace.iconForFileType(:kComputer)

  7. NSWorkspace.sharedWorkspace.iconForFileType(kComputer)
    etc…

Has anybody got this to work in RubyCocoa?
Many Thanks
Tom Medhurst

First off, since in Ruby all constants begin with a capital letter, all
the constants like kSuchAndSuch morph into OSX::KSuchAndSuch.

Second of all, where on earth is kComputer defined? I can’t find it
documented anywhere; in particular, it’s not in this list:
http://developer.apple.com/documentation/Carbon/Reference/IconServices/Reference/reference.html#//apple_ref/doc/uid/TP30000239-CH4g-TPXREF130

Third, it seems to me that the iconForFileType method of NSWorkspace
takes a string representing either a filename extension or a HFS file
type. So if anything of these is going to work, it’d be:

OSX::NSWorkspace.sharedWorkspace.iconForFileType(‘root’)

which does, in fact, return an NSImage instance for me. Of course, I get
an NSImage with every string I’ve tried, including the empty string - I
haven’t tried drawing it on screen to see if it’s the one you want,
since I don’t have anywhere to draw it to, as it were. But it should be
a good place for you to start.

Hi Adam,
Thanks for looking into this for me!
kComputer is defined in the IconsCore.h header file in
CoreServices.framework/LaunchServices.framework. (tried referencing it
as
KComputer, but that didn’t work)

I tried your example and it just returned the generic “unknown
file-type”
icon. :frowning:

I am trying to load the system icon for the computer… it’s really easy
in
Ubuntu to load stock (system) icons, so I am really frustrated when I
can’t
seem to figure this out on my lovely mac :’(

Any ideas…?
Thanks
Tom

Tom Medhurst wrote:

Hi Adam,
Thanks for looking into this for me!
kComputer is defined in the IconsCore.h header file in
CoreServices.framework/LaunchServices.framework. (tried referencing it
as
KComputer, but that didn’t work)

I tried your example and it just returned the generic “unknown
file-type”
icon. :frowning:

I am trying to load the system icon for the computer… it’s really easy
in
Ubuntu to load stock (system) icons, so I am really frustrated when I
can’t
seem to figure this out on my lovely mac :cry:

Any ideas…?
Thanks
Tom

On my machine, the constant is kComputerIcon, not kComputer, and yes, it
evaluates to Root. RubyCocoa isn’t going to recognize the Ruby constant
KComptuerIcon or anything like it, though, because at least on my
machine all that stuff is pure C stuff (not Objective-C) which isn’t
accounted for yet by custom bridge-support files. You can try fooling
around with bridge-support, but I’d just skip kComputerIcon entirely and
use what it evaluates to, the string ‘root’.

That out of the way, according this source: [1], the correct line in
Objective-C would be
SImage* macIcon = [[NSWorkspace sharedWorkspace] iconForFileType:
NSFileTypeForHFSTypeCode(kComputerIcon)];

To me, that translates as:
OSX::NSWorkspace.sharedWorkspace.iconForFileType(OSX::NSFileTypeForHFSTypeCode(‘root’))

so, try that out.

[1] Getting standard system icons in Cocoa

Hi Adam,Sorry it has taken me a while to reply; I have been away from my
precious Mac for a couple of weeks (it’s been tough! :))

When I ran the ruby code you suggested I got:

ServerListViewBrowserItem#imageRepresentation: ArgumentError: invalid
value
for Integer: “root”

/Users/tom/dev/proj/bonjour-graphviz/gui-tools/mac/ZeroConfNetworkBrowser/build/Debug/ZeroConfNetworkBrowser.app/Contents/Resources/serverlistviewbrowseritem.rb:42:in
`NSFileTypeForHFSTypeCode’*

/Users/tom/dev/proj/bonjour-graphviz/gui-tools/mac/ZeroConfNetworkBrowser/build/Debug/ZeroConfNetworkBrowser.app/Contents/Resources/serverlistviewbrowseritem.rb:42:in
`imageRepresentation’*

/Users/tom/dev/proj/bonjour-graphviz/gui-tools/mac/ZeroConfNetworkBrowser/build/Debug/ZeroConfNetworkBrowser.app/Contents/Resources/rb_main.rb:26:in
`NSApplicationMain’*

It would appear that NSFileTypeForHFPTypeCode is expecting an integer
value
instead of ‘root’.

Any ideas?

Many Thanks,

Tom

Tom Medhurst wrote:

Hi Adam,Sorry it has taken me a while to reply; I have been away from my
precious Mac for a couple of weeks (it’s been tough! :))

When I ran the ruby code you suggested I got:

ServerListViewBrowserItem#imageRepresentation: ArgumentError: invalid
value
for Integer: “root”

/Users/tom/dev/proj/bonjour-graphviz/gui-tools/mac/ZeroConfNetworkBrowser/build/Debug/ZeroConfNetworkBrowser.app/Contents/Resources/serverlistviewbrowseritem.rb:42:in
`NSFileTypeForHFSTypeCode’*

/Users/tom/dev/proj/bonjour-graphviz/gui-tools/mac/ZeroConfNetworkBrowser/build/Debug/ZeroConfNetworkBrowser.app/Contents/Resources/serverlistviewbrowseritem.rb:42:in
`imageRepresentation’*

/Users/tom/dev/proj/bonjour-graphviz/gui-tools/mac/ZeroConfNetworkBrowser/build/Debug/ZeroConfNetworkBrowser.app/Contents/Resources/rb_main.rb:26:in
`NSApplicationMain’*

It would appear that NSFileTypeForHFPTypeCode is expecting an integer
value
instead of ‘root’.

Any ideas?

Many Thanks,

Tom
Ok, my final suggestion then is to try using 1919905652 instead of
‘root’. On my system (PowerPC 32-bit), that number is the integer
interpretation of those four chars. I’m not really sure if that how this
works… I don’t really know C all that well. Also, if you are on an
intel machine, you may want to figure out what kComputerIcon evaluates
to yourself.