Monday, November 23, 2020

#1 2020-08-01 09:45:50 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 449

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. smile

Applescript:

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

Last edited by Fredrik71 (2020-08-01 09:59:18 am)


The purpose to study someone else art is not to add, its to make less more.


Filed under: username, home directory

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)