Home directory of current user, username.

Sometimes its not enough…

How many ways we need the path to the home directory of the current user or the username.

Enjoy. :slight_smile:

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

property currentUserName : short user name of (system info)
property currentUserFullname : long user name of (system info)
property currentUserHome : POSIX path of (home directory of (system info))

-- Initializes a file manager object.
set theFileManager to current application's NSFileManager's alloc()'s init()
set theUserDirectory to theFileManager's homeDirectoryForCurrentUser()
log "Instance Property: homeDirectoryForCurrentUser"
log {theUserDirectory as text, theUserDirectory's className() as text}

set theDirectoryAsPosixPath to POSIX path of my homeDirectory()
set theDirectoryAsAlias to my homeDirectory() as alias

-- Handler
on homeDirectory()
	return ((current application's NSFileManager's alloc()'s init())'s ¬
		homeDirectoryForCurrentUser()) as text
end homeDirectory

-- Function: NSHomeDirectory
set theHomeDirectory to current application's NSString's stringWithString:(current application's NSHomeDirectory())
log "Function: NSHomeDirectory"
log {theHomeDirectory as text, theHomeDirectory's className() as text}

(**
* This example in ASOC show that we need to include "FORWARD SLASH" if we add strings
* after the function. The function give us POSIX Path as defaults.
*)
set theHomeOfDesktop to (theHomeDirectory as text) & "/" & "Desktop"
log theHomeOfDesktop as text

set theUserName to current application's NSString's stringWithString:(current application's NSUserName())
log "Function: NSUserName"
log {theUserName as text, theUserName's className() as text}

set theFullUserName to current application's NSString's stringWithString:(current application's NSFullUserName())
log "Function: NSFullUserName"
log {theFullUserName as text, theFullUserName's className() as text}

set theUserName to current application's NSString's stringWithString:(theUserName as text)
set theHomeDirectoryForUser to theFileManager's homeDirectoryForUser:theUserName
log "Instance Method: homeDirectoryForUser:"
log {theHomeDirectoryForUser as text, theHomeDirectoryForUser's className() as text}

set theHomeDirectoryForUser to current application's NSHomeDirectoryForUser(theUserName)
log "Function: NSHomeDirectoryForUser"
log {theHomeDirectoryForUser as text, theHomeDirectoryForUser's className() as text}

set theTemporaryDirectory to theFileManager's temporaryDirectory()
log "Instance Property: temporaryDirectory"
log {theTemporaryDirectory as text, theTemporaryDirectory's className() as text}

-- Example from AS.
set theHomeAsAlias to path to home folder -- default alias
set theHomeAsPosix to POSIX path of (path to home folder as text)

-- #####
-- Example from shell
set theHomeFromShell to do shell script "echo $HOME"
log "BASH: $HOME"
log theHomeFromShell
set theUserName1Shell to do shell script "echo $USER"
log "BASH: $USER"
log theUserName1Shell
set theUserName2Shell to do shell script "echo | whoami"
log "BASH: whoami"
log theUserName2Shell
set theUserName3Shell to do shell script "echo | id -u -n"
log "id -u -n"
log theUserName3Shell
set theFullnameOfCurrentUserShell to do shell script "echo | id -F"
log "id -F"
log theFullnameOfCurrentUserShell
set theUserName4Shell to do shell script "echo | logname"
log "logname"
log theUserName4Shell
set theUserName5Shell to do shell script "echo | ps -o user= -p $$ | awk '{print $1}'"
log "ps -o user= -p $$ | awk '{print $1}'"
log theUserName5Shell
set theUserName6Shell to do shell script "echo | who | awk '{print $1}' | head -n1"
log "who | awk '{print $1}' | head -n1"
log theUserName6Shell
set theUserName7Shell to do shell script "echo | basename $(echo $HOME)"
log "basename $(echo $HOME)"
log theUserName7Shell
set theHomeFromShell2 to do shell script "cd $HOME; echo | pwd"
log "cd $HOME; pwd"
log theHomeFromShell2

-- Use property variable with system info.
set theCurrentUserNameAS to currentUserName -- ex. short user name of (system info)
log "short user name of (system info)"
log theCurrentUserNameAS
set theCurrentUserFullnameAS to currentUserFullname
log "long user name of (system info)"
log theCurrentUserFullnameAS
set theCurrentUserHomeAS to currentUserHome
log "home directory of (system info)"
log theCurrentUserHomeAS

I tested the suggested methods at speed. Perhaps users will find it interesting:

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

-- getting user name methods. Ordered from less effective to most effective
do shell script "echo | whoami" -- 0.024
do shell script "echo | id -F" -- 0.024
do shell script "echo | id -u -n" -- 0.023
do shell script "echo | ps -o user= -p $$ | awk '{print $1}'" -- 0.022
do shell script "echo | basename $(echo $HOME)" -- 0.021
do shell script "echo | who | awk '{print $1}' | head -n1" -- 0.021
do shell script "echo $USER" -- 0.011
short user name of (system info) -- 0.008
long user name of (system info) -- 0.008
(current application's NSUserName()) as text -- 0.003
(current application's NSFullUserName()) as text -- 0.003

-- getting user home folder methods. Ordered from less effective to most effective
do shell script "cd $HOME; echo | pwd" -- 0.015
do shell script "echo $HOME" -- 0.012
((current application's NSFileManager's alloc()'s init())'s homeDirectoryForUser:"123") as text -- 0.007, hfs path
((current application's NSFileManager's alloc()'s init())'s homeDirectoryForCurrentUser()) as text -- 0.007, hfs path
POSIX path of (path to home folder) -- 0.005
(current application's NSHomeDirectoryForUser("123")) as text -- 0.003
(current application's NSHomeDirectory()) as text -- 0.003