Get path to the user's home folder?

Archives Forums/MacOS X Discussion/Get path to the user's home folder?

DrDeath(Posted 2009) [#1]
Is there a way to get the path to the user's home folder, i.e. /Users/yourusernamehere? I would require that for instance to store my program's preferences at the proper place (/Users/yourusernamehere/Library/Preferences).

I don't know whether this question has already been answered in the past, but I couldn't find an appropriate thread.

Thanks in advance!


Brucey(Posted 2009) [#2]
This module might be able to help you.
It has cross-platform support for many common folders.


xlsior(Posted 2009) [#3]
Another thought: /users/yourusernamehere = ~ on Linux, I assume it's the same on the Mac as well.

/users/yourusernamehere/Library/Preferences = ~/Library/Preferences

That said, Brucey's module is pretty nice and gives that kind of info on all three platforms.


RiK(Posted 2009) [#4]
Yup, it's ~/ on mac too.


DrDeath(Posted 2009) [#5]
I already tried "~/" (that was my first idea), but unfortunately that does not work. It seems that this shortcut is not available for path calls from within apps under MacOS X (it also does not work in AppleScript or in Objective-C).

But the volumes module does exactly what I need. Thanks a lot! :D


Brucey(Posted 2009) [#6]
It's always better to use platform APIs if you can.

To convert "~" to your home folder, you'll probably need to pass it to a shell, as I'm not sure you can use it in a system call. (of course, I'm happy to be proved wrong :-)


dawlane(Posted 2009) [#7]
This could be of some use
http://developer.apple.com/mac/library/qa/qa2007/qa1549.html


ImaginaryHuman(Posted 2009) [#8]
I've seem a fair number of games using the `shared` user folder to store game files, including Gray Alien Games actually, if that helps at all with permissions and stuff.


DrDeath(Posted 2009) [#9]
I've seem a fair number of games using the `shared` user folder to store game files, ...

...which is an absolutely unacceptable behaviour. A user's preferences belong in the user's home folder, and not somewhere else.


xlsior(Posted 2009) [#10]
...which is an absolutely unacceptable behaviour. A user's preferences belong in the user's home folder, and not somewhere else.


Except if you stick to the user folder, you'll never be able to do things like a shared highscore table, because none of the other users of the computer would be able to save their scores to the file.

Certain info pretty much needs to be shared...


_Skully(Posted 2009) [#11]
The shared folder is the game/applications storage area and the users folder is for the game-play/session data


ragtag(Posted 2009) [#12]
I was just going to ask the same, when I found this post. I was trying to compile an old game I made for Linux, and it had the following code in it for finding home folders on OSX. This code is really old, so may not work.

If os = "osx" Then
	' Code posted on forum by Mark Sibly. Finds the current home directory.
	Const kUserDomain=-32763
	Const kVolumeRootFolderType=Asc("r") Shl 24 | Asc("o") Shl 16 | Asc( "o") Shl 8 | Asc("t")
	
	Extern
		Function FSFindFolder( vRefNum,folderType,createFolder,foundRef:Byte Ptr )
		Function FSRefMakePath( ref:Byte Ptr,path:Byte Ptr,maxPathsize )
	End Extern
	
	Function HomeDir$()
	
		Local buf:Byte[1024],ref:Byte[80]
		
		If FSFindFolder( kUserDomain,kVolumeRootFolderType,False,ref ) Return
		If FSRefMakePath( ref,buf,1024 ) Return
		
		Return String.FromCString( buf )
	
	End Function
	
	SaveFile = HomeDir() +"/Library/Preferences/BlackjackPrefs.txt"
End If


FSFindFolder and FSRefMakePath stop this from compiling at all on linux, though it might still work on OS X. But must be simpler way. :)

What's the right place to store save games and the like? Something like this?
Linux:
~/.yourgame/savegame
OSX:
~/Library/Application Support/yourgame/savegame
WindowsXP (vista/7 ??):
C:\Documents and Settings\username\Application Data\yourgame\savegame

Btw...how do you find the home folder on Windows and Linux too?


Winni(Posted 2009) [#13]
Vista and Windows 7:
"%systemdrive%\Users\%username%\Saved Games"


TrionWork(Posted 2009) [#14]
getenv_("HOME")
return the user's home directory


xlsior(Posted 2009) [#15]
%systemdrive%\Users\%username%\Saved Games


Um... No.
On most English systems -- yes, but not on many of the foreign language systems - the folder may or may not be called 'users' there.

Better way of accessing that same path:

check the %userprofile% environment variable instead.
"%userprofile%\Saved Games"

Anyway, Brucey has a Volumes module that is multi-plaform, and returns the imprtant folders on Windows, Linux and Mac -- I recommend taking a look at it.


Brucey(Posted 2009) [#16]
Best to use system APIs if you can help it, for these things. (that's what they are there for!)


Winni(Posted 2009) [#17]
On most English systems -- yes, but not on many of the foreign language systems - the folder may or may not be called 'users' there.


Nope, Vista and Windows 7 -always- call the users folder "Users", as they always call the Program Files folders "Program Files" and "Program Files (x86)". It's just that foreign language systems use a trick to display the folder names in the respective language - they use a hidden file that contains the translation.

Besides - I've tried the above on a German 64-Bit version of Vista Ultimate. ;-)