1

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)

Jono
  • 3,393
  • 6
  • 33
  • 48
  • What is `computer`? — Also, what do you mean "loops in a command line application"? Show what you're actually doing. Don't talk about it - show it. – matt Sep 12 '15 at 15:47
  • Sorry that was stupid of me, edited. – Jono Sep 12 '15 at 16:45

1 Answers1

2

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

Use the NSWorkspace notification NSWorkspaceDidActivateApplicationNotification.

matt
  • 515,959
  • 87
  • 875
  • 1,141