You can use NSWorkspace
to get the focused application and Timer
to schedule the printing of the app's name every 20 seconds. Here's an example implementation:
main.swift689 chars25 lines
In this implementation, we create a timer that fires every 20 seconds using scheduledTimer(withTimeInterval:repeats:block:)
. The block prints the name of the frontmost application by accessing NSWorkspace.shared.frontmostApplication
. The timer is retained by the view controller and invalidated when the view controller is deallocated, to prevent any retain cycles.
gistlibby LogSnag