Script run as root needs to get current user

I’ve got a script I have to run as root that needs to get the name of the current user. The two methods I know of

set FOO to do shell script "whoami"


set FOO to system attribute "USER"

will both return “root” when I run the script rather than the current user. Can anyone help?

Can’t test at the moment, what does this return though?

tell application "System Events" to set FOO to name of current user


If you run the script as root, isn’t then the root the current user?

Well, the script may be run as root in the background from my ARD server machine while a local user logged in as themself may be sitting in front of the machine. I want the script to get their username even though it’s running as root.

This code will return the correct username if an actual person logged in, but will return root if a script running as root was used to type in the name and password and then hit return on the login screen.