How to create individual file icons.
How to create individual file icons.
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
Re: How to create individual file icons.
You can set a custom icon for a file using a method from NSWorkspace.
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.
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
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)
Raspberry Pi OS (Arm64)
Re: How to create individual file icons.
Thanks for the hint, wilbert!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.
- 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.
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!
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
Re: How to create individual file icons.
I'm not sure what you exactly have in mind.WilliamL wrote:Wow, that was easy! Once I got the path correct (and had a testfile.txt) it worked perfectly!
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)
Raspberry Pi OS (Arm64)
Re: How to create individual file icons.
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]
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
Re: How to create individual file icons.
The code looks fine to me. You could try to display the FileImage image to see if it is correctly created.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).
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)
Re: How to create individual file icons.
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.
oh, the file picture appears to have transferred to another computer along with the file ok.
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
Re: How to create individual file icons.
I didn't expect that but that is great!WilliamL wrote:oh, the file picture appears to have transferred to another computer along with the file ok.
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)