Application username ?

BlitzMax Forums/BlitzMax Beginners Area/Application username ?

tesuji(Posted 2005) [#1]
This may only be relevant on OS X, but is there an easier way to get at the username of the person running the application ?

I want to persist a user preference file in /Users/*username*/Library/Application Support/*appname*/ but would need to extract the current username if I'm to do this and support multiple users.

Here's a workaround I've done but it's not ideal <grin>


Local appUser:String = getOSXUser()
Print appUser
End

' ------------------------------------------------
' Get OS X application user (in a roundabout way)
' ------------------------------------------------
Function getOSXUser:String()

    Local username:String = Null

    Local usersDir:String[] = LoadDir("/Users")
    ' iterate through each user dir
    For Local dirName:String = EachIn usersDir
        If Left(dirName,1) <> "." And dirName <> "Shared"
            ' *should* be a valid user dir
            ' try & change dir to a restricted dir
            dirIsReadable = ChangeDir("/Users/"+dirName+"/Library")
            If dirIsReadable
                username = dirName
                Exit
            End If
        End If
    Next

    Return username

End Function



Any ideas ?


Perturbatio(Posted 2005) [#2]
try:
Print getenv_("USER")


under windows it's
Print getenv_("USERNAME")



tesuji(Posted 2005) [#3]
Thanks Perturbatio

getenv_("USER") works great. Was searching all over for something like that but couldn't find it in the docs or the wiki.


Perturbatio(Posted 2005) [#4]
yeah, getenv_ is part of the pub.stdc module I believe. but not documented.