Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Clear Mac OS X's icon cache.
sudo find /private/var/folders/ -name com.apple.dock.iconcache -exec rm {} \;
@jasonm23
Copy link

jasonm23 commented Aug 27, 2020

Ok so for me the cp / touch method fails only happens on specific app bundles. (1 of 3 so far tested...)

However for that one failing bundle, xattr -wx method also fails, but I think that's mainly the exotic filename that Macos adds on if we really drag it there.

For example

ls /Applications/Foo.app/Icon\r
ls: /Applications/Foo.app/Iconr: No such file or directory

Also I think this may be relevant: (post drag n drop method)

ls -l /Applications/Foo.app
total 1240
drwxr-xr-x  9 jason  admin  288 Aug 25 09:57 Contents
-rw-r--r--@ 1 jason  admin    0 Aug 27 09:38 Icon?

Filesize is zero on Icon?


So if I tab complete the filename in zsh I get:

/Applications/Foo.app/Icon$'\r'

Although also trying:

cp AppIcon.icns /Applications/Foo.app/Icon$'\r'
xattr -wx com.apple.FinderInfo "0000000000000000040000000000000000000000000000000000000000000000" /Applications/Foo.app

Also fails...

@jasonm23
Copy link

jasonm23 commented Aug 27, 2020

A straight copy of an .icns to Icon\r ends up with this BTW.

ls -l /Applications/Foo.app
total 680
drwxr-xr-x  9 jason  admin     288 Aug 25 09:57 Contents
-rw-r--r--  1 jason  admin  344212 Aug 27 09:52 Icon?

Edit: to note the filesize

@andreiborisov
Copy link

andreiborisov commented Aug 27, 2020

However for that one failing bundle, xattr -wx method also fails, but I think that's mainly the exotic filename that Macos adds on if we really drag it there.

Yeah, it seems to have such a weird name to avoid potential name conflicts with user files.

I can confirm it doesn't work in Zsh and Bash (I’m using fish so I didn’t catch it). I’m not Zsh expert, but there definitely should be a way to enter \r literally in a command-line.

UPDATED

This is works:

icon_name=$(echo -ne "Icon\r")
cp ~/Documents/MyAwesomeIcons/AnotherIconForTheApp "/Applications/App.app/$icon_name"

I've updated the original post to reflect this.

@andreiborisov
Copy link

andreiborisov commented Aug 27, 2020

A straight copy of an .icns to Icon\r ends up with this BTW.

It should have non-zero file size, however, it appears to be converted in some unknown binary format by Finder, since the file size is different from dragged .icns file.

Filesize is zero on Icon?

Possible conversion fail resulted in zero file size Icon\r? Does the icon change for the app after the dragging that produces zero file size Icon\r?

@jasonm23
Copy link

jasonm23 commented Aug 27, 2020

At zero bytes it's like some weird flag.

I mean if you are getting this to work, and repeatably, good for you.

TBH though it doesn't look like a good option. I am surprised that you are having issues with the touch method. Are you on Catalina btw?

@andreiborisov
Copy link

andreiborisov commented Aug 27, 2020

Are you on Catalina btw?

Yep.

In my opinion it’s much better solution, because:

  • it works both for apps and folders
  • it doesn’t require to change anything in the app bundle which can have unintended consequences with Gatekeeper and other integrity checks
  • you can easily revert changes even with Finder GUI

@jasonm23
Copy link

jasonm23 commented Aug 27, 2020

The part about "it works on my machine" was missing.

@andreiborisov
Copy link

andreiborisov commented Aug 27, 2020

The part about "it works on my machine" was missing.

😂 I guess, but there is nothing special about my machine. Have you tried my updated snippets for Zsh?

@jasonm23
Copy link

jasonm23 commented Aug 27, 2020

Seems like the zero bytes Icon? is more than just filename change for the .icns file though.

To me that's a thing we should know if we're going to say this is a working / repeatable solution.

If we got that, I'd say it's at least an ok solution.

BTW wrt folders, is that what happens there too? An Icon? file is placed inside the folder and the xattrs are patched in the same way?

@jasonm23
Copy link

jasonm23 commented Aug 27, 2020

Oh I didn't see your naming update.

So yeah, naming is one part but clearly Macos is not copying the icns to the Icon? file.

@andreiborisov
Copy link

andreiborisov commented Aug 27, 2020

So yeah, naming is one part but clearly Macos is not copying the icns to the Icon? file.

No, the resulted file is actually bigger in size compared to .icns, so it’s definitely in a different format. As far as I know those Icon\r files are date back to the oldest macOS releases, so surely someone figured out how they encoded by now.

For our purposes, however, it's not strictly necessary to know what format it is. I’ve just pre-converted icons that I need by dragging them and copy already encoded versions. Would be nice to be able to skip this step though, yeah.

BTW wrt folders, is that what happens there too? An Icon? file is placed inside the folder and the xattrs are patched in the same way?

I’m not familiar with wrt folders. But it’s the same with ordinary folders, makes no difference whether it’s an app bundle or a folder for macOS whatsoever.

@jasonm23
Copy link

jasonm23 commented Aug 27, 2020

Ah sorry.

WRT = with regard to

@jasonm23
Copy link

jasonm23 commented Aug 27, 2020

it's not strictly necessary to know what format it is. I’ve just pre-converted icons that I need by dragging them and copy already encoded versions. Would be nice to be able to skip this step though, yeah.

Ok so I missed the part where you can't use a icns file. TBH that pretty much defeats the purpose.

However If you can automate away that step maybe you have a workable solution.

@andreiborisov
Copy link

andreiborisov commented Aug 27, 2020

Ok so I missed the part where you can't use a icns file. TBH that pretty much defeats the purpose.

Yeah, I guess I should've mentioned that I'm doing this to switch between dark/light variants of the app icon.

@jasonm23
Copy link

jasonm23 commented Aug 27, 2020

I better test to see if the touch method is still working in Catalina! but I hope you figure out why it's broken for you. Please do test more app bundles (if you haven't) because there do seem to be some challenging ones.

@jasonm23
Copy link

jasonm23 commented Aug 27, 2020

FYI tested touch method in Catalina. (only one bundle tested, but working ok)

@jasonm23
Copy link

jasonm23 commented Aug 27, 2020

(The app bundle that failed on Mojave for me, still fails on Catalina. No big shock there.)

@andreiborisov
Copy link

andreiborisov commented Aug 27, 2020

(The app bundle that failed on Mojave for me, still fails on Catalina. No big shock there.)

Interesting...

However If you can automate away that step maybe you have a workable solution.

I'll try to investigate how Icon\r encoded, but knowing macOS it's probably some arbitrary binary nonsense😁

@jasonm23
Copy link

jasonm23 commented Aug 27, 2020

So are you also seeing these Icon\r files as zero bytes after dragging? Because that's not really signalling encoding is it?

It signals some undocumented black magic voodoo. That's why I'm basically 100% out on this method.

But good luck!

@andreiborisov
Copy link

andreiborisov commented Aug 27, 2020

So are you also seeing these Icon\r files as zero bytes after dragging?

No, they have non-zero size for me. Does an icon for the app changes with the dragging method for you when the size of Icon\r is zero?

@andreiborisov
Copy link

andreiborisov commented Aug 27, 2020

It signals some undocumented black magic voodoo. That's why I'm basically 100% out on this method.

I mean it is all undocumented black magic, unfortunately. Why touching an app bundle refreshes cache for example? Why it works for one app, but not for the other? More importantly, how can we sure it won’t break in the next macOS release? At least with Icon\r method there is some level of confidence it won’t change the behavior between macOS versions simply because it’s part of the filesystem and it’s required to be backwards compatible.

But I see your point of course.

@jasonm23
Copy link

jasonm23 commented Aug 27, 2020

So are you also seeing these Icon\r files as zero bytes after dragging?

No, they have non-zero size for me. Does an icon for the app changes with the dragging method for you when the size of Icon\r is zero?

That's right, but I'm several hours away from the last live test. It's something I can check again in the morning.

@andreiborisov
Copy link

andreiborisov commented Aug 27, 2020

Ok, that's even more interesting😂

CleanShot 2020-08-27 at 19 08 35@2x

yet

CleanShot 2020-08-27 at 19 09 00@2x

Sorry, I didn't try to check the size from shell before, I have no idea why it's reported as 0 there, but in reality, it has size as you can see...

@jasonm23
Copy link

jasonm23 commented Aug 27, 2020

"in reality..." Uhmmm.

Tbh in reality all we know is that Finder's internal logic has embedded the icon somewhere. The trouble is, where. We know it's not really in that file Icon\r because the actual file system doesn't register the file size.

The worst problem here is that neither representation is "in reality". They're both views through different abstraction layers, but it's safe to say only that those icon data bytes went "somewhere". Unfortunately that doesn't give us a thing to stand on.

All completely undocumented. All completely unreliable.

If you can script it somehow and make it reliable, applause for you.

@frakman1
Copy link

frakman1 commented Dec 1, 2020

On High Sierra, deleting com.apple.dock.plist worked for me. However, a couple of native apps stubbornly refused to work. The Notes and Screen Sharing apps still have the default app icon.

Update:
I fixed those apps by removing them from the dock and then re-adding them. It worked after that.

@beginnerJq
Copy link

beginnerJq commented Apr 2, 2021

@jasonm23 thanks

@jkrovitz
Copy link

jkrovitz commented Nov 7, 2021

sudo rm -rfv /Library/Caches/com.apple.iconservices.store; sudo find /private/var/folders/ \( -name com.apple.dock.iconcache -or -name com.apple.iconservices \) -exec rm -rfv {} \; ; sleep 3;sudo touch /Applications/* ; killall Dock; killall Finder

after that restart your mac.

wahlaaah.. all restore and back to normal

This worked really well! Thank you @zigang93.

@tobiashochguertel
Copy link

tobiashochguertel commented May 6, 2022

sudo rm -rfv /Library/Caches/com.apple.iconservices.store; sudo find /private/var/folders/ \( -name com.apple.dock.iconcache -or -name com.apple.iconservices \) -exec rm -rfv {} \; ; sleep 3;sudo touch /Applications/* ; killall Dock; killall Finder
after that restart your mac.
wahlaaah.. all restore and back to normal

This worked really well! Thank you @zigang93.

Works!

Here for better readable and discovering the shell-snippet again:

sudo rm -rfv /Library/Caches/com.apple.iconservices.store; sudo find /private/var/folders/ \( -name com.apple.dock.iconcache -or -name com.apple.iconservices \) -exec rm -rfv {} \; ; sleep 3;sudo touch /Applications/* ; killall Dock; killall Finder

@jbrodriguez
Copy link

jbrodriguez commented May 7, 2022

@jasonm23 @andreiborisov i'm interested in changing icons via cli, doing some more debugging, found these articles:

The first one clarifies the 0 bytes size of Icon\r: the file is actually stored in the resource fork

@jbrodriguez
Copy link

jbrodriguez commented May 7, 2022

and finally this tool looks promising (although i haven't tested it myself) https://github.com/mklement0/fileicon

EDIT: i tried the tool and it works for me on Monterrey, although i still need to rebuild the cache and restart dock:

rm /var/folders/*/*/*/com.apple.dock.iconcache
killall Dock

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment