NSWorkspace's 'frontmostApplication' doesn't change after initial use

拜拜、爱过 提交于 2019-12-25 06:33:06

问题


I'm trying to get an update of the current active (foreground) application. Even across computer screens. I'm using this code to try to do it:

print(NSWorkspace.sharedWorkspace().frontmostApplication?.localizedName)

This loops in a command line application every 3 seconds, and prints to console. It works with any application open as active when it first starts up.

However, it does not change from the first app afterwards. 1. Why is it doing this? 2. What is the proper code to make it work? 3. Is there a way to simply get the app name every time the forefront application changes? (preferably in swift or obj-c)


回答1:


Is there a way to simply get the app name every time the forefront application changes?

Use the NSWorkspace notification NSWorkspaceDidActivateApplicationNotification.



来源:https://stackoverflow.com/questions/32533525/nsworkspaces-frontmostapplication-doesnt-change-after-initial-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!