As I asked earlier, I was trying to figure out who the current user is. Since the proposed solution doesn't work, I thought, okay, let's try sending notifications from a LaunchAgent! Only, right, notifications are per-process, so I tried Distributed Notifications... and that doesn't seem to work across users. (Now, since I log while also sending the distributed notifications, I can see that it is working the way I want. Except that willPowerOffNotification
doesn't actually seem to happen with a logout. Maybe that's because it's a CLI program? But the other notifications do work...)
Keeping track of users in macOS
There is an answer!
Apple tries to keep the foreground (GUI) activities protected from untoward background (agents, daemons) interest, absent explicit cooperation between foreground and background apps.
You've told us your proposed solution, but have provided little about the problem you are seeking to address with knowledge of the identity of the foreground (GUI) user.
Maybe the Apple Introduction to Multiple User Environments doc helps?
Requirement to have a single daemon that uses the current “active” user. Not my choice. 😊