MacOS – Set a custom dock icon for an application instance via Terminal

dockiconmacosterminal

I'm frequently opening multiple instances of applications, especially Emacs, since I may be working on different projects on the same time, and I find this the best way to manage this (yes, I know there are lots of project management extensions for Emacs, I don't need to be taught in Emacs).

I'm doing this with open -n -a Emacs.app which opens a new separate Emacs instance. But it would be really nice to set a custom dock icon for the opened instances, so I can easily differentiate them when doing Cmd+Tab.

Do you know any hack or way to set a custom Application (Dock) icon for a instance? Even if it is a rather complex shell command (I could then create a shellscript for this).

What I really want to achieve is to create a shellscript for this so I can call opennew Emacs.app x and then it would fork the Emacs icon and add a badge with an x in it.

Then I can call opennew Emacs.app s for "school related", opennew Emacs.app p for "project related" etc.

Best Answer

I don't have a complete solution for you, but from looking through the man pages, I think I kinda see how one might be crafted.

  • open(1) provides the --args option for passing arguments on to the invoked application.

  • emacs(1) will read values from an .Xresources file. One of the values it reads is emacs.iconName.

  • X allows you to select Xresources for a specific app by setting $XENVIRONMENT to contain the path of a file containing Xresource values. Also, X programs are supposed to support the -name and -title options for setting (I think) the name of an Xresource file to be loaded and/or the title of the application instance.

So I haven't tested this, but you might be able to do something like:

open -n -a Emacs.app --args "-name <resource-file-path> -title school"

Then, in the resource file, a line that says

emacs.iconName:<path-to-school-icon-file>

In another resource file, you'd have iconName set to a work icon, etc.

If you try this, I'd be interested in knowing how it works out.