UPDATE: I’ve created a Python implementation of this algorithm. It’s available here.

I was curious how docks seem able to determine the correct icon (ie find the correct .desktop file) for applications even when their .desktop files lack the StartupWMClass key. So I had a look at the source code for ‘Docky’. After a little figuring out of the, almost entirely uncommented :(, code, I determined that the algorithm goes like this, stopping on the first successful step:

  • Check if there is a .desktop file with the appropriate StartupWMClass.
  • See if there is a .desktop file whose name is the same as that of the WM_CLASS of the window.
  • Then check if the window belongs to ‘OpenOffice’, ‘LibreOffice’ or ‘Wine’.
  • Look for a .desktop file whose name is the same as that of the WM_CLASS of the window with all . characters removed.
  • Check if the process name matches the Exec field of any .desktop file.
  • Finally, check if the window command matches the Exec field of any .desktop file.

Here is the relevant method starting a line 165 of Docky.Services/Docky.Services/WindowMatcherService.cs in Docky-2.2.0:

See the full code here.