macosluahammerspoon

In Hammerspoon how do I get the currently focused application name on startup?


I can get the application name after the user switches focus with the following code:

function applicationWatcher(appName, eventType, appObject)
  if (eventType == hs.application.watcher.activated) then
    print(appName)
  end
end

local appWatcher = hs.application.watcher.new(applicationWatcher)

But I'd like this appName right after hammerspoon initializes.


Solution

  • See the hs.application.frontmostApplication() function: http://www.hammerspoon.org/docs/hs.application.html#frontmostApplication :)