# RubyCocoa and Mac System Icons

**URL:** <https://www.ruby-forum.com/t/rubycocoa-and-mac-system-icons/159423>\
**Category:** Ruby\
**Created:** [February 4, 2009, 1:07pm UTC](https://www.ruby-forum.com/t/rubycocoa-and-mac-system-icons/159423 "2009-02-04T13:07:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom\_Medhurst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/958977/32.png) [@Tom\_Medhurst](https://www.ruby-forum.com/u/Tom_Medhurst)\
**Post date:** [February 4, 2009, 1:07pm UTC](https://www.ruby-forum.com/t/rubycocoa-and-mac-system-icons/159423/1 "2009-02-04T13:07:18Z")

</div>

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_

---

<div class="post-metadata">

**Author:** ![Tom\_Medhurst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/958977/32.png) [@Tom\_Medhurst](https://www.ruby-forum.com/u/Tom_Medhurst)\
**Post date:** [February 21, 2009, 12:06am UTC](https://www.ruby-forum.com/t/rubycocoa-and-mac-system-icons/159423/2 "2009-02-21T00:06:29Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![Tom\_Medhurst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/958977/32.png) [@Tom\_Medhurst](https://www.ruby-forum.com/u/Tom_Medhurst)\
**Post date:** [February 21, 2009, 11:55pm UTC](https://www.ruby-forum.com/t/rubycocoa-and-mac-system-icons/159423/3 "2009-02-21T23:55:41Z")

</div>

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. ☹

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

---

<div class="post-metadata">

**Author:** ![Tom\_Medhurst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/958977/32.png) [@Tom\_Medhurst](https://www.ruby-forum.com/u/Tom_Medhurst)\
**Post date:** [February 24, 2009, 2:00am UTC](https://www.ruby-forum.com/t/rubycocoa-and-mac-system-icons/159423/4 "2009-02-24T02:00:58Z")

</div>

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. ☹
> 
> 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

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](http://zathras.de/angelweb/blog-get-standard-system-icons.htm)

---

<div class="post-metadata">

**Author:** ![Tom\_Medhurst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/958977/32.png) [@Tom\_Medhurst](https://www.ruby-forum.com/u/Tom_Medhurst)\
**Post date:** [March 13, 2009, 6:14pm UTC](https://www.ruby-forum.com/t/rubycocoa-and-mac-system-icons/159423/5 "2009-03-13T18:14:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tom\_Medhurst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/958977/32.png) [@Tom\_Medhurst](https://www.ruby-forum.com/u/Tom_Medhurst)\
**Post date:** [March 14, 2009, 1:43am UTC](https://www.ruby-forum.com/t/rubycocoa-and-mac-system-icons/159423/6 "2009-03-14T01:43:02Z")

</div>

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.
