Thank you in advance!
I found this very old SO post advising to use Accessibility API
https://stackoverflow.com/questions/8411587/detecting-mouse-over-dock-any-dock-icon-click
As a complement, how to locate your icon in the dock which show how to use accessibility API.
Or search for the UIElementInspector 44 sample code, as proposed here by Quinn:
https://forums.swift.org/t/swift-macos-listen-to-open-close-events-of-any-application/29021
Hope that may help.