# Any way to set icons to files?

**URL:** <https://forum.xojo.com/t/any-way-to-set-icons-to-files/52057>\
**Category:** General\
**Created:** [January 20, 2020, 10:16pm UTC](https://forum.xojo.com/t/any-way-to-set-icons-to-files/52057 "2020-01-20T22:16:53Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arnaud\_N](https://forum.xojo.com/user_avatar/forum.xojo.com/arnaud_n/32/21824_2.png) [@Arnaud\_N](https://forum.xojo.com/u/Arnaud_N)\
**Post date:** [January 20, 2020, 10:16pm UTC](https://forum.xojo.com/t/any-way-to-set-icons-to-files/52057/1 "2020-01-20T22:16:53Z")

</div>

Hello,

I’m trying to retrieve files/folders icons that the user may have changed (not interested in the default icon for the file’s type, but only if the user has altered it, like pasting on Mac), save them and later restore them. For Mac, Windows and Linux (otherwise, it’s no fun trying hard things)

To gather the icon, I’m using various functions in the MBS plugin (I have yet to test my method). How would I go about restoring (setting) them later?

---

<div class="post-metadata">

**Author:** ![Arnaud\_N](https://forum.xojo.com/user_avatar/forum.xojo.com/arnaud_n/32/21824_2.png) [@Arnaud\_N](https://forum.xojo.com/u/Arnaud_N)\
**Post date:** [January 20, 2020, 10:36pm UTC](https://forum.xojo.com/t/any-way-to-set-icons-to-files/52057/3 "2020-01-20T22:36:18Z")

</div>

[quote=472023:@Emile Schwarz]The icons are stored in .DS\_Store (check spelling), an invisible file for items.

For folders, the icon is stored in an Icon + Return file inside the directory (once more an invisible file).[/quote]  
Thanks for your reply.  
OK, for folders, I already knew the file. That’s not a problem, as I can store the icon file as well. (BTW: wondering what happens if the user edits that icon file and, for example, put Hello world instead; I’ll try)

For files, .DS\_Store isn’t the right answer. That one stores Finder’s views of the containing folder (e.g. if you choose to sort by name or show extra information). You can hardly store the icon of all files in a given folder inside a single file.  
It used to be stored in the resources fork. Now, I guess there’s another location to get/set it, I just don’t know which.

Still looking for ways on Linux and Windows

---

<div class="post-metadata">

**Author:** ![Arnaud\_N](https://forum.xojo.com/user_avatar/forum.xojo.com/arnaud_n/32/21824_2.png) [@Arnaud\_N](https://forum.xojo.com/u/Arnaud_N)\
**Post date:** [January 20, 2020, 11:44pm UTC](https://forum.xojo.com/t/any-way-to-set-icons-to-files/52057/5 "2020-01-20T23:44:02Z")

</div>

[quote=472027:@Emile Schwarz]Thumbs.db holds the images icons preview (as jpg) for images, I do not know for folders(also invisible file).

That file is not mandatory, it is created when the folder is viewed as Icons AND some images exists in that folder.[/quote]  
That one still doesn’t suit my needs, sorry. If I create a picture file, I know Windows would create the thumbnail automatically; If I create another kind of file, the Thumbs.db file isn’t to be modified. In both cases, dealing with this file is probably not a good thing.

> [@472027:@Emile Schwarz](#):
>
> On OS X, there is also another invisible file whose name holds MacOS or so.

Sorry, I don’t quite understand this statement What do you mean by holding MacOS?

> [@472027:@Emile Schwarz](#):
>
> BTW: you are right for the item icons it probably is stored in the index file (name forgotten) where items are stored.

You’re talking about the catalog of the file system? That’s possible, as I guess a custom icon isn’t stored in the file itself (otherwise, I couldn’t have a 0 byte file with a custom icon; and I actually can). Still, how to set it?

> [@472027:@Emile Schwarz](#):
>
> When the Finder does not display correctly that file, it is because the file is corrupted. In the old times, it was possible to ask the Finder to rebuild this file, but I never knew how to do that in macOS X

Right. In Mac OS 9 (and earlier), there were the Desktop DB and Desktop DF files; those could be rebuilt (using option+command keys at the end of the start up process, IIRC). Unless I’m mistaken, they didn’t contain file icons, but your choices to open files with other applications and other desktop services information. I don’t think they contained file icons, otherwise those files would have been way bigger (and searching inside a single file for a particular file’s icon would have been a pain for the OS, anyway).

Mac OS X doesn’t use those files anymore. .DS\_store files provide a subset of these ones, I believe.

> [@472027:@Emile Schwarz](#):
>
> Some big wig asked for changes like so many things (some were not for the best).

I couldn’t agree more. If changes could be made only because technology progresses (and not because an OS maker just decides to remove a feature because he doesn’t like that feature himself), we would not have to learn a ton of new things for the sake of change.

> [@472027:@Emile Schwarz](#):
>
> Nota: I counted 21 items (files and folders) in the hard disk root folder.

Possibly. What’s your point here?

> [@472027:@Emile Schwarz](#):
>
> I think it will be faster to search the informaton in what was was called Inside Mac: Files (20 years ago or so); actual name unknow.

Faster, probably. Accurate, not so sure

Thanks.

---

<div class="post-metadata">

**Author:** ![Sam\_Rowlands](https://forum.xojo.com/user_avatar/forum.xojo.com/sam_rowlands/32/265_2.png) [@Sam\_Rowlands](https://forum.xojo.com/u/Sam_Rowlands)\
**Post date:** [January 21, 2020, 1:17am UTC](https://forum.xojo.com/t/any-way-to-set-icons-to-files/52057/6 "2020-01-21T01:17:54Z")

</div>

NSWorkspace iconForFile: Will get the file icon.  
[https://developer.apple.com/documentation/appkit/nsworkspace/1528158-iconforfile?language=objc](https://developer.apple.com/documentation/appkit/nsworkspace/1528158-iconforfile?language=objc)

NSWorkspace setIcon:forFile:options will set the icon.  
[https://developer.apple.com/documentation/appkit/nsworkspace/1529882-seticon?language=objc](https://developer.apple.com/documentation/appkit/nsworkspace/1529882-seticon?language=objc)

However, I don’t know of a way to determine if that icon is the file type icon or a custom icon. Hopefully someone else can fill that in for you.

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [January 21, 2020, 1:20am UTC](https://forum.xojo.com/t/any-way-to-set-icons-to-files/52057/7 "2020-01-21T01:20:51Z")

</div>

> [@472034:@Arnaud Nicolet](#):
>
> Windows would create the thumbnail automatically

Only when the holding folder is opened. If you create a folder on Windows (and do not open it), store some images there, archive it in a zip file, there will never be a Thumbs.db file.

> [@472034:@Arnaud Nicolet](#):
>
> Sorry, I don’t quite understand this statement What do you mean by holding MacOS?

A folder holding files that is compacted (zip for example) and opened on Windows 10 will have a special file whose name contains MacOS (something like that) in it.

I am sorry to not be more precise / up to date.

---

<div class="post-metadata">

**Author:** ![Arnaud\_N](https://forum.xojo.com/user_avatar/forum.xojo.com/arnaud_n/32/21824_2.png) [@Arnaud\_N](https://forum.xojo.com/u/Arnaud_N)\
**Post date:** [January 21, 2020, 1:43pm UTC](https://forum.xojo.com/t/any-way-to-set-icons-to-files/52057/8 "2020-01-21T13:43:10Z")

</div>

[quote=472041:@Sam Rowlands]NSWorkspace iconForFile: Will get the file icon.

> **[iconForFile: | Apple Developer Documentation](https://developer.apple.com/documentation/appkit/nsworkspace/1528158-iconforfile?language=objc)**
>
> Returns an image containing the icon for the specified file.

NSWorkspace setIcon:forFile:options will set the icon.

> **[setIcon(\_:forFile:options:) | Apple Developer Documentation](https://developer.apple.com/documentation/appkit/nsworkspace/1529882-seticon?language=objc%5B%2Fquote%5D)**
>
> Sets the icon for the file or directory at the specified path.

Great! Actually, I’m using NSWorkspace to get the icon since I moved to Mojave (around 7 months ago), so I knew for the get part. I didn’t ever noticed there was a set icon alongside Granted, it’s even in the MBS plugin (which is the way I get the icon)

> [@472041:@Sam Rowlands](#):
>
> However, I don’t know of a way to determine if that icon is the file type icon or a custom icon. Hopefully someone else can fill that in for you.

In the file/folder flags (e.g. with GetFileFlagsMBS), one flag is HasCustomIcon. This flag already existed in MacOS 9 (recall using ResEdit to see them) but is still used now (at least, it was when I last checked, some months ago).  
This flag, once you paste an icon in the Finder’s information dialog, is set to true (to false when you delete the custom icon). If the file has a custom icon but you remove the flag, it shows the default icon (I experimented this years ago, I guess it’s still true). If you set the flag while the file has never had a pasted, custom icon, the file stays with the default icon (under some circumstances, I’ve seen the icon showing another one, like a stationary icon).

I’m not sure, since I know the NSWorkspace set method only from now, whether I have to also set the HasCustomIcon flag after calling the NSWorkspace method or if it does it automatically. I’ll see, obviously.

Thank you!

---

<div class="post-metadata">

**Author:** ![Arnaud\_N](https://forum.xojo.com/user_avatar/forum.xojo.com/arnaud_n/32/21824_2.png) [@Arnaud\_N](https://forum.xojo.com/u/Arnaud_N)\
**Post date:** [January 21, 2020, 1:53pm UTC](https://forum.xojo.com/t/any-way-to-set-icons-to-files/52057/9 "2020-01-21T13:53:54Z")

</div>

> [@472043:@Emile Schwarz](#):
>
> Only when the holding folder is opened. If you create a folder on Windows (and do not open it), store some images there, archive it in a zip file, there will never be a Thumbs.db file.

Good point.  
Anyway, I’m not dealing with previews of picture files in my app, but good to know.

> [@472043:@Emile Schwarz](#):
>
> A folder holding files that is compacted (zip for example) and opened on Windows 10 will have a special file whose name contains MacOS (something like that) in it.

Ah, you’re referring to the resource and metadata files (for a test.jpg file having resources or otherwise unknown properties to other operating systems, you get a .\_test.jpg file along the original file (actually, Mac OS would strongly hide those files, even if you bring them back from a volume whose the format doesn’t support resources)).  
Those files usually don’t contain a preview of the original file, but, rather, the resource fork (or other alternate forks, as you may have 50 forks for a given file if you want) and things like the Finder’s label value or concepts foreign to other systems. Granted, one of those alternate forks may contain a preview, but it’s far from a rule.

---

<div class="post-metadata">

**Author:** ![Sam\_Rowlands](https://forum.xojo.com/user_avatar/forum.xojo.com/sam_rowlands/32/265_2.png) [@Sam\_Rowlands](https://forum.xojo.com/u/Sam_Rowlands)\
**Post date:** [January 23, 2020, 1:34am UTC](https://forum.xojo.com/t/any-way-to-set-icons-to-files/52057/10 "2020-01-23T01:34:17Z")

</div>

> [@472089:@Arnaud Nicolet](#):
>
> In the file/folder flags (e.g. with GetFileFlagsMBS), one flag is HasCustomIcon. This flag already existed in MacOS 9 (recall using ResEdit to see them) but is still used now (at least, it was when I last checked, some months ago).

Thanks for sharing this information with us.

Let me know if calling setIcon also modifies this tag.

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [January 23, 2020, 12:52pm UTC](https://forum.xojo.com/t/any-way-to-set-icons-to-files/52057/11 "2020-01-23T12:52:41Z")

</div>

When you use setIcon method in [NSWorkspaceMBS](http://www.monkeybreadsoftware.net/class-nsworkspacembs.shtml) class, the file gets a resource fork with a icns resource.

[GetFileFlagsMBS](http://www.monkeybreadsoftware.net/files-folderitem-method1.shtml) function then reports 1024 for the file with custom icon and 0 for a file without custom icon. Please use Bitwise.BitAnd to check for the bit as others may be set.

Same information can also be seen via [FileListMBS](http://www.monkeybreadsoftware.net/class-filelistmbs.shtml).FinderFlags or using [MDItemMBS](http://www.monkeybreadsoftware.net/class-mditemmbs.shtml) with kMDItemFSHasCustomIcon constant.

You can use [ResourceForkMBS](http://www.monkeybreadsoftware.net/class-resourceforkmbs.shtml) class to inspect (even in 64-bit with recent plugins!)

---

<div class="post-metadata">

**Author:** ![Arnaud\_N](https://forum.xojo.com/user_avatar/forum.xojo.com/arnaud_n/32/21824_2.png) [@Arnaud\_N](https://forum.xojo.com/u/Arnaud_N)\
**Post date:** [January 24, 2020, 2:07pm UTC](https://forum.xojo.com/t/any-way-to-set-icons-to-files/52057/12 "2020-01-24T14:07:51Z")

</div>

> [@472375:@Sam Rowlands](#):
>
> Let me know if calling setIcon also modifies this tag.

So I confirm: HasCustomIcon is set to true with calling SetIcon.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [April 29, 2021, 4:15pm UTC](https://forum.xojo.com/t/any-way-to-set-icons-to-files/52057/13 "2021-04-29T16:15:31Z")

</div>


