Last active
May 12, 2025 23:05
-
-
Save macshome/11bf0e5021fcd2b25a9e749823386763 to your computer and use it in GitHub Desktop.
A playground to understand how to find the current username on macOS.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Some Swift code for different ways to find the current username on macOS. | |
// | |
// You can run this in a Playground, or compile it with `swiftc`. Compiling | |
// it will let you experiment with different ways of running the command. | |
print("********** Foundation Usernames **********") | |
// If you are only importing Foundation you have a few options to get the current user. | |
// Note that if run from the CLI they will report the CLI user running them. | |
import Foundation | |
// NSUserName() is the simplest by far. | |
let nsUsername = NSUserName() | |
print("Current user name: \(nsUsername)") | |
// ProcessInfo() is a close second. | |
let piUser = ProcessInfo.processInfo.userName | |
print("Current user name: \(piUser)") | |
// If you really want to you can also read it from the environment variables of the current process. | |
if let foundationUser = ProcessInfo().environment["USER"] { | |
print("Current user name: \(foundationUser)") | |
} | |
print("\n********** Cocoa Usernames **********") | |
// If you have Cocoa, or CoreGraphics, imported you can be pretty precise by finding who the user | |
// of the current CoreGraphics session is. | |
// | |
// Since Cocoa is all about the GUI session, this will report the name of the logged in user. | |
import Cocoa | |
if let cgSession = CGSessionCopyCurrentDictionary() as? [String: Any], | |
let cgUsername = cgSession["kCGSSessionUserNameKey"] { | |
print("Current user name: \(cgUsername)") | |
} else { | |
print("Could not get current CGSession.") | |
} | |
print("\n********** SystemConfiguration Usernames **********") | |
// If you want to replicate the approach that many scripters take with `scutil` you can import | |
// SystemConfiguration and then get the current console user from that. | |
// | |
// Like Cocoa, this will report the user who has a logged in GUI session. | |
import SystemConfiguration | |
if let csUsername = SCDynamicStoreCopyConsoleUser(nil, nil, nil) as String? { | |
print("Current user user: \(csUsername)") | |
} else { | |
print("Could not get current console user.") | |
} | |
print("\n********** POSIX Usernames **********") | |
// If you really wanted to, you can always import Darwin and use the POSIX tools. | |
// | |
// Like the foundation methods this will report the owner of process. | |
import Darwin | |
if let pw = getpwuid(getuid()), let name = pw.pointee.pw_name { | |
let pwUsername = String(cString: name) | |
print("Current user name: \(pwUsername)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output from Playground or Terminal as current user:
Example output when running as a different user with
sudo
: