How to create individual file icons.

Mac OSX specific forum
WilliamL
Addict
Addict
Posts: 1215
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

How to create individual file icons.

Post by WilliamL »

When my app creates a file using CreateFile(#File, Filename$) the icon of the file is determined by my Info.plist but I want to have the file icon to look like the picture I am creating with my app. Does anybody know how to do that.. if it's not too hard. :)
MacBook Pro-M1 (2021), Sonoma 14.3.1 (CLT 15.3), PB 6.10b7 M1
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: How to create individual file icons.

Post by wilbert »

You can set a custom icon for a file using a method from NSWorkspace.

Code: Select all

FilePath.s = "testfile.txt"

MyImage = CreateImage(#PB_Any, 512, 512, 32, #PB_Image_Transparent)
StartVectorDrawing(ImageVectorOutput(MyImage))
MovePathCursor(256, 256)
AddPathCircle(256, 256, 240, 0, 235, #PB_Path_Connected)
ClosePath()
VectorSourceColor(RGBA(255, 0, 0, 255))
StrokePath(10)
StopVectorDrawing()

Workspace = CocoaMessage(0, 0, "NSWorkspace sharedWorkspace")

If CocoaMessage(0, Workspace, "setIcon:", ImageID(MyImage), "forFile:$", @FilePath, "options:", #Null)
  Debug "custom icon successfully set"
Else
  Debug "error setting custom icon"
EndIf

; If CocoaMessage(0, Workspace, "setIcon:", #nil, "forFile:$", @FilePath, "options:", #Null)
;   Debug "custom icon successfully removed"
; Else
;   Debug "error removing custom icon"
; EndIf
If you have a custom file type which needs to be supported, you need a QuickLook Plugin to render the thumbnails. That way you can support all the files your application creates at once.
QuickLook plugins have a .qlgenerator extension and can be placed in an application bundle (in MyApp.app/Contents/Library/QuickLook/) or in one of the standard locations ~/Library/QuickLook or /Library/QuickLook.
Unfortunately I have no experience with creating such a plugin so I don't know if this would be possible with PureBasic.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: How to create individual file icons.

Post by Danilo »

wilbert wrote:If you have a custom file type which needs to be supported, you need a QuickLook Plugin to render the thumbnails. That way you can support all the files your application creates at once.
QuickLook plugins have a .qlgenerator extension and can be placed in an application bundle (in MyApp.app/Contents/Library/QuickLook/) or in one of the standard locations ~/Library/QuickLook or /Library/QuickLook.
Unfortunately I have no experience with creating such a plugin so I don't know if this would be possible with PureBasic.
Thanks for the hint, wilbert!

- Introduction to Quick Look Programming Guide
- QuickLook Plugins List - A directory of Quick Look Plugins for Apple's OS X
- Quick Look plugins for developers
- Quick Look Plugins
WilliamL
Addict
Addict
Posts: 1215
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: How to create individual file icons.

Post by WilliamL »

Wow, that was easy! Once I got the path correct (and had a testfile.txt) it worked perfectly! :)

Now I will integrate it into my program. This will be a great improvement.

Thanks wilbert!
MacBook Pro-M1 (2021), Sonoma 14.3.1 (CLT 15.3), PB 6.10b7 M1
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: How to create individual file icons.

Post by wilbert »

WilliamL wrote:Wow, that was easy! Once I got the path correct (and had a testfile.txt) it worked perfectly! :)
I'm not sure what you exactly have in mind.
I think that these custom icons are stored locally and won't be copied if the file is transferred to another computer so I don't know if that will work for you.
But yes, it's easy. :)

The links Danilo posted are great if you need to support dynamically generated icons.
Windows (x64)
Raspberry Pi OS (Arm64)
WilliamL
Addict
Addict
Posts: 1215
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: How to create individual file icons.

Post by WilliamL »

If the icons don't transfer it could be a problem. I'll have to try it and see what happens.

Right now I've got this but I haven't figured out why the picture doesn't show even though the icon is changed (it's blank). I'm trying to force PB to automatically reduce the size to 512x512 when it draws to the new image.

[see below]
Last edited by WilliamL on Sun Jan 24, 2016 6:47 pm, edited 1 time in total.
MacBook Pro-M1 (2021), Sonoma 14.3.1 (CLT 15.3), PB 6.10b7 M1
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: How to create individual file icons.

Post by wilbert »

WilliamL wrote:Right now I've got this but I haven't figured out why the picture doesn't show even though the icon is changed (it's blank).
The code looks fine to me. You could try to display the FileImage image to see if it is correctly created.
Windows (x64)
Raspberry Pi OS (Arm64)
WilliamL
Addict
Addict
Posts: 1215
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: How to create individual file icons.

Post by WilliamL »

ok, this works! I took out ', 32, #PB_Image_Transparent' from 'FileImage = CreateImage(#PB_Any, 512, 512, 32, #PB_Image_Transparent)' and it works. Not sure why but I can use it now and worry about that later. :wink:

oh, the file picture appears to have transferred to another computer along with the file ok.

Code: Select all

            ;create custom icon
            FileImage = CreateImage(#PB_Any, 512, 512)
            If StartDrawing(ImageOutput(fileimage)) And fileimage
                Box(0,0,300,300,0) ; some visual
                DrawImage(ImageID(#imagechart),0,0,512,512)
                StopDrawing()
            Else
                stop("Can't draw")
            EndIf
            Workspace = CocoaMessage(0, 0, "NSWorkspace sharedWorkspace")
            If CocoaMessage(0, Workspace, "setIcon:", ImageID(fileImage), "forFile:$", @filename$, "options:", #Null)
              Debug "custom icon successfully set"
            Else
              Debug "error setting custom icon"
            EndIf
            FreeImage(fileimage)
MacBook Pro-M1 (2021), Sonoma 14.3.1 (CLT 15.3), PB 6.10b7 M1
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: How to create individual file icons.

Post by wilbert »

WilliamL wrote:oh, the file picture appears to have transferred to another computer along with the file ok.
I didn't expect that but that is great! :)
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply