如何检测MACOS上启动应用程序何时?
我在用户的MacOS计算机上有一个背景过程。它的工作是在计算机上启动任何应用程序时都检测到。当前,我正在检测nsnotificationcenter
's nsworkspacedidlaunchapplicationnotification
事件。这非常适合检测何时刚启动应用程序(即该应用程序当时还没有实例)。
但是,在MacOS上,如果我们单击左上角的红十字按钮,它通常会关闭应用程序窗口,并且应用程序继续在后台运行。在码头上可见的应用图标也可以很明显,其下方的点指示器也可以看到。如果我在码头上单击应用程序图标,然后启动它,则不会触发nsworkspacedIdlaunchApplicationNotification
事件。
为了跟踪此类事件,我尝试使用nsworkspacedIdactivativateApplicationNotification
事件。使用此事件,我能够检测所有应用程序启动方案。问题在于,每当应用程序焦点(例如使用命令+选项卡切换窗口),单击其Dock图标,在两个应用程序之间进行更改时,此事件就会触发,…
是否可以过滤这些触发器或确定哪个操作导致了哪种操作。扳机?还是我可以听的其他一些事件/方法给出了所需的过滤触发器?我只想检测创建应用程序新窗口的方案。
I have a background process running on the user’s macOS machine. Its job is to detect whenever any app is launched on the machine. Currently, I am detecting the NSNotificationCenter
’s NSWorkspaceDidLaunchApplicationNotification
event. This works perfectly for detecting when an app is freshly launched (i.e. the app had no instance already running at that time).
But, on macOS, if we click the red cross button at the top-left corner, it generally closes the app window and the app continues to run in the background. This is also evident by the app icon visible on the dock with the dot indicator below it. If I click the app icon on the dock and then launch it, the NSWorkspaceDidLaunchApplicationNotification
event won’t be triggered.
To track such events, I tried using the NSWorkspaceDidActivateApplicationNotification
event. Using this event, I was able to detect all the app launch scenarios. The problem is that this event gets triggered whenever the app comes into focus such as switching windows using command+tab, clicking on its dock icon, changing between two apps, …
Is there a way to filter out these triggers or identify which action led to the trigger? Or is there some other event/ method I can listen to which gives the required filtered triggers? I only want to detect scenarios where a new window of the app is created.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如评论中提到的那样,您似乎想要的是两件不同的事情,应分别处理以达到。
启动新过程时检测应用程序启动。如果不是足够的(通常适用于视觉用户应用程序),则可以使用NSWorkspacedIdlaunchApplicationNotification,如果不是nswarchapplication,则可以使用kqueue,甚至不是端点,甚至端点框架框架来统治它们。
>
在已经启动的应用程序中跟踪窗口。从视觉上讲,如果在App Dock图标下的白点,该应用仍将启动。
此任务有一个可访问性框架,您可以跟踪创建窗口的事件,窗口破坏,从目标过程ID,可见性状态以及等。
自发布以来,这有点放弃,没有更新,但是在大多数情况下对您有用。
What you seems to want is two different things, as was mentioned in comments, which should be processed separately to be reached.
To detect app launch, when the new process is started. You could use the NSWorkspaceDidLaunchApplicationNotification if it is enough (usually for visual user apps), or kqueue if it is not, or even EndpointSecurity framework to rule them all.
To track the window(s) in the already launched app. Visually, if the white dot under app dock icon is there, the app is still launched.
There is an Accessibility framework for this task, you could track the event of window creation, window destruction, get count of windows from target process id, visibility state and etc.
It is a bit abandoned and has no updates since maybe release, but it will work for you in most cases.