Any way to set icons to files?

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?

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 (check spelling too) inside the directory (once more an invisible file).

[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…

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.

On OS X, there is also another invisible file whose name holds MacOS or so.

BTW: you are right for the item icons… it probably is stored in the index file (name forgotten) where items are stored. When the Finder do 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…

Some big wig asked for changes… like so many things (some were not for the best).

Nota: I counted 21 items (files and folders) in the hard disk root folder.

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.

[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.

Sorry, I don’t quite understand this statement… What do you mean by “holding MacOS”?

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?

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.

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.

Possibly. What’s your point here?

Faster, probably. Accurate, not so sure…

Thanks.

NSWorkspace iconForFile: Will get the file icon.
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

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.

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.

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.

[quote=472041:@Sam Rowlands]NSWorkspace iconForFile: Will get the file icon.
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[/quote]
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)…

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!

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

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.

Thanks for sharing this information with us.

Let me know if calling setIcon also modifies this tag.

When you use setIcon method in NSWorkspaceMBS class, the file gets a resource fork with a icns resource.

GetFileFlagsMBS 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.FinderFlags or using MDItemMBS with kMDItemFSHasCustomIcon constant.

You can use ResourceForkMBS class to inspect (even in 64-bit with recent plugins!)

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