How to get the icon of a file?

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
stam
Posts: 2996
Joined: Sun Jun 04, 2006 9:39 pm

How to get the icon of a file?

Post by stam » Mon Dec 27, 2021 3:42 pm

Hi all,

is there a way to to get the default file icon displayed in the Finder/Explorer for the file type?

An app i'm working on will allow the user to import files (to be stored as blobs in a db) but it would be really nice to display the default icon for the filetype for the platform... Grateful for any suggestions,

Stam

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: How to get the icon of a file?

Post by mwieder » Tue Dec 28, 2021 4:05 am

If you're looking for the system icons, try

Code: Select all

/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/
OTOH if you're looking for individual application icons you'll need to look in each application's bundle, i.e.:

Code: Select all

Applications/LiveCode.app/Contents/Resources/LiveCode.icns

stam
Posts: 2996
Joined: Sun Jun 04, 2006 9:39 pm

Re: How to get the icon of a file?

Post by stam » Tue Dec 28, 2021 5:10 pm

Thanks @mwieder - was really wondering if there is some library to do this.
I'm looking for file icons of various apps as shown in t he finder/desktop (ie the default powerpoint file icon, pdf file icon etc).

To put things in context this is to move one of my filemaker pro apps to LC; in the FMP app I've set up import of files and FMP shows the file icon in the container field, but not sure how to replicate this in LC, if that's even possible, and especially for cross-platform apps.

So i was wondering if someone else had already done this and whether maybe there was a library for this?

mbossiere
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 44
Joined: Wed Aug 01, 2007 2:15 am

Re: How to get the icon of a file?

Post by mbossiere » Thu Mar 14, 2024 6:13 am

It appears this thread ended here, back in 2021. But I too would like to know if there is an LC library to pull up the Finder icon of a given file (on MacOS or on any other desktop OS). Has this question been explore elsewhere that I am not aware of?
“You do what you do out of your own private passion for the thing itself”
A Dillard (source: https://educheer.com/essays/annie-dillard-s-handed-my-own-life/)

stam
Posts: 2996
Joined: Sun Jun 04, 2006 9:39 pm

Re: How to get the icon of a file?

Post by stam » Thu Mar 14, 2024 12:43 pm

Sadly I got no further - and the lack of responses belies the fact that this is not something anyone else could answer at the time.
In the end I created custom SVG icons for the commonest filetypes and a generic icon for 'unknown' filetypes.
Not great really, but did the job more or less.

I know Zax managed something like this, show in his app here: https://forums.livecode.com/viewtopic.php?f=143&t=38966
however if I understood correctly this is MacOS only. And you'll see I raised an issue about image quality as icons were 32 x 32 px (I guess ok for really small icons...)

If this fulfils your brief then DM him and see if he can help - but if like me you need cross-platform support it probably won't suffice.
I sadly have seen nothing equivalent for Windows, although it must be possible...

Stam

mbossiere
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 44
Joined: Wed Aug 01, 2007 2:15 am

Re: How to get the icon of a file?

Post by mbossiere » Sun Mar 17, 2024 7:46 pm

Thanks for the update Stam. I would assume the same, namely that it is not functionality that exists, but I will look into your link to what Zax managed to do. It might be ok for me as I'm building for MacOS only at the moment. Your custom icons might work for some better visuals in the meantime. Thanks, mbossiere
“You do what you do out of your own private passion for the thing itself”
A Dillard (source: https://educheer.com/essays/annie-dillard-s-handed-my-own-life/)

Zax
Posts: 519
Joined: Mon May 28, 2007 10:12 am
Contact:

Re: How to get the icon of a file?

Post by Zax » Thu Mar 21, 2024 4:07 pm

I wrote an little external for my app for MacOS only.
I'm a complete noob in objective-, foundation and this kind of stuff but I still managed, by copying and pasting bits of code found mainly in the work of Trevor DeVore.

I can of course share this code if anyone here is interested.

The main usages are:

Code: Select all

FileGetIcon(pFilePath, pIconSize [, pOptionalOutputFilePath]) // returns image data, or empty, or error string
	pIconSize: 32 || 64 || 128 || 512
	returns string beginning with "Error :" if an error occurs
		
	example #1: put FileGetIcon("/Applications/Mail.app", 64, "") into image "MyImg" // returns image data
	example #2:
		get FileGetIcon("/Applications", 128, "path/pOutputFileName.png") // create .png file : icon of folder "Applications"
		if word 1 of it is "Error" then answer error it with "Cancel"
It works for file, folder and applications.

stam
Posts: 2996
Joined: Sun Jun 04, 2006 9:39 pm

Re: How to get the icon of a file?

Post by stam » Thu Mar 21, 2024 5:38 pm

Thanks Zax... the question at hand is slightly different:It's about getting the icons for filetypes as designated by the current system.

I guess if dragging/dropping a file, this could be used? if it's only for apps then no so much...
In my case ideally would be MacOS+Win but that's a bigger ask...

Stam

Zax
Posts: 519
Joined: Mon May 28, 2007 10:12 am
Contact:

Re: How to get the icon of a file?

Post by Zax » Fri Mar 22, 2024 8:17 am

My external works for files, folders and applications.
When starting my project, I found another way to create .png icon file but it only works for appllication with bundle.

Code: Select all

   --------- tries to read the name of the default icon from the bundle's plist file
   put quote & pFileName & "/contents/info" & quote into theFile -- ".plist" extension on "info.plist" isn't needed.
   try
      put shell("defaults read " & theFile & " CFBundleIconFile") into iconName
      if (last char of iconName is cr) then delete last char of iconName
      if (debug) then echoMsg "iconName=" & iconName & "---"
      if (not iconName contains ".icns") then put ".icns" after iconName -- plist key sometimes don't includes extension
      put resPath & iconName into iconPathname
   catch errStr
      ...
   end try

Then you will have to convert icns to png using SIPS (Scriptable image processing system) on MacOS X
See https://www.simplehelp.net/2010/10/08/h ... s-in-os-x/

Concerning Windows, I really don't know how to proceed at this time but I would be very interested to make my external run on Windows!

Post Reply

Return to “Talking LiveCode”