NSWorkspace的“frontmostApplication”在初始使用后不会更改 [英] NSWorkspace's 'frontmostApplication' doesn't change after initial use
问题描述
我尝试获取当前活动(前台)应用程序的更新。甚至跨计算机屏幕。我使用这个代码尝试做:
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)
每3秒在命令行应用程序中循环,并打印到控制台。
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.
但是,它不会从第一个应用程序后改变。
1.为什么要这样做?
2.什么是正确的代码,使其工作?
3.每次最前面的应用程序更改时,是否有一种方法来简单地获取应用程序名称? (最好在swift或obj-c中)
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)
推荐答案
名称每次最前沿的应用程序更改?
Is there a way to simply get the app name every time the forefront application changes?
使用 NSWorkspace 通知 NSWorkspaceDidActivateApplicationNotification
。
这篇关于NSWorkspace的“frontmostApplication”在初始使用后不会更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!