icon from extension

Hello,
is it possible to get the icon associated to a doc from the doc’s extension?
At present I create a dummy file in the temporary folder and using macoxLib I get its icon.
For instance, to get the icon of a .zip file I do:
dim f as FolderItem = SpecialFolder.temporary.child(“Test.zip”)
dim b as BinaryStream = BinaryStream.Create(f)
b.Write “a”
b = nil
dim p as Picture = f.Icon
f.Delete

But I think this approach is pretty expensive, especially if I want to get several specimens of icons.
Hence my question whether it be possible to get icons from extensions without having at hand a folderitem.
I’d avoid using plugins.
Thank you.

Hi Carlo,

Yes, it’s very easy to get the icon of any file or extension. You’ll need check NSWorkspace class. Out of my head it’s like:

 Const CocoaLib As String = "Cocoa"
  declare function sharedWorkspace lib CocoaLib selector "sharedWorkspace" (class_id as Ptr) as Ptr
  Declare Function NSClassFromString Lib CocoaLib (aClassName as CFStringRef) As Ptr
  declare function iconForFileType lib CocoaLib selector "iconForFileType:" (obj_id as Ptr, extension as CFStringRef) as Ptr

dim icon as ptr // NSImage
 icon =  iconForFileType( sharedWorkspace( NSClassFromString( "NSWorkspace" )), ".doc" )
if icon<>nil then
-> convert NSImage to Xojo Picture
end if

Have you already worked with NSImage and Xojo pictures ?

the direct answer is no, since technically the icon belongs to the “file” and not to “extension”

.APP and .EXE files for example have a “default” icon (which varies between OS), but when you create a compile app with XOJO (as an example) you can assign whatever ICON you want…

So to say all files that end in “.EXE” have the same icon is incorrect, since it is possible that 1000 different .EXE files will have 1000 different icons.

Even using the method you stipulated above will give you only the “default” icon for a file

[quote=265514:@Dave S]the direct answer is no, since technically the icon belongs to the “file” and not to “extension”

.APP and .EXE files for example have a “default” icon (which varies between OS), but when you create a compile app with XOJO (as an example) you can assign whatever ICON you want…

So to say all files that end in “.EXE” have the same icon is incorrect, since it is possible that 1000 different .EXE files will have 1000 different icons.

Even using the method you stipulated above will give you only the “default” icon for a file[/quote]

There are two ways to get the icon.
a.) from file
b.) from extension

As far as I understood, the question was only to get the icon from the extensions. If not then only change to:

declare function iconForFile lib CocoaLib selector "iconForFile:" (obj_id as Ptr, fullPath as CFStringRef) as Ptr

my statements still stand

@Rob Way b) from extension.

@Dave I don’t expect the same icon; as you say, it is possible to have 1000 different icons. The hope was/is to get a declare or shell or whatever that retrieves the correct icon associated to a given extension.

Thank you both, Dave and Rob.

@Rob [quote]Have you already worked with NSImage and Xojo pictures ?[/quote]
Not yet. Let us see. Meanwhile I’ll give a try to your declares.
Thanks.

@Rob If you can spare some more time, help to get a Xojo picture from an NSimage will be most appreciated.
Thanks.

The Retina Kit has a function for this, iconForFileType will return a retina image from a UTI type. http://www.ohanaware.com/retinakit/

And it’s still true for binarys.

But he hasked: [quote=265508:@Carlo Rubini]is it possible to get the icon associated to a doc from the doc’s extension?[/quote]

So the approach of Rob and Sam is still a valid one :slight_smile:

Yes that is what was asked… but what is not being considered, is the extenstion will return the DEFAULT icon for the extenstion, NOT the icon that may have been assigned.

there are only a few dozen (if that many) default icons supported by each OS…

@Sam Rowlands Thank you for the reminder, but I’m not yet ready to make the jump…

I have a function to get the actual icon from a file “iconForFile”, but in the OPs case he hasn’t created the file yet.

No worries, it’s there when you’re ready.

Just for the records: Eli’s answer put me on the right track; so with macoslib installed, the following snippet does what I was looking for, without having to create a folderitem:

Canvas11.Backdrop = NSWorkspace.FileTypeIcon(“zip”)//doc etc.

Thanks again to those who contributed.

Which is weird.
I see no contribution from Eli in this thread. :wink:

if you use MBS Plugin, you also find in it in NSWorkSpaceMBS.
Or use Folderitem.IconMBS function.

The OP explicitly said no plugins in the initial posts some ppl still drop their ads for their own plugins to make sales pitch…

Half of the people here use my plugins and they would be disappointed if plugin alternative was not mentioned.

@Paul Sondervan

[quote]@Carlo Rubini Eli’s answer put me on the right track.
I see no contribution from Eli in this thread. ;)[/quote]

Sorry, Rob. I actually intended to write: “Rob’s answer put me on the track.”
Paul, thank you for the alert.

Indeed, but let’s not forget that this thread will be found with search later by people who may not have such prejudice, who will be glad to hear about a professional solution. Plugins are convenient and reliable, an advantage appreciated by many developers, me for one. Moreover, Christian’s plugins are now 64 bit compatible, which is more than what MacOSLib can do today.