Page 1 of 1

How to create individual file icons.

Posted: Sat Jan 23, 2016 2:14 am
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. :)

Re: How to create individual file icons.

Posted: Sat Jan 23, 2016 7:42 am
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.

Re: How to create individual file icons.

Posted: Sat Jan 23, 2016 9:15 am
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

Re: How to create individual file icons.

Posted: Sat Jan 23, 2016 6:27 pm
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!

Re: How to create individual file icons.

Posted: Sat Jan 23, 2016 6:50 pm
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.

Re: How to create individual file icons.

Posted: Sat Jan 23, 2016 6:59 pm
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]

Re: How to create individual file icons.

Posted: Sat Jan 23, 2016 7:13 pm
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.

Re: How to create individual file icons.

Posted: Sat Jan 23, 2016 7:46 pm
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)

Re: How to create individual file icons.

Posted: Tue Jan 26, 2016 5:53 pm
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! :)