问题
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