How to change dir to $HOME?

Archives Forums/MacOS X Discussion/How to change dir to $HOME?

Phish(Posted 2004) [#1]
I would like to be able to put the preferences file for my game in ~/Library/Preferences.

Can anyone think of a good way to do this? Remember it has to work whether the app is stored, whether that is in /Applicaltions/ or ~/Desktop/ or something!

It would be nice if there was a command such as ChangeToHomeDir() or GetHomeDir()...

For now though, I would be happy with any solution, even if it meant interacting with C code (there isn't even any easy way to do it in C either is there AFAIK?)


Todd(Posted 2004) [#2]
You can use the "getenv_" function to get an environment variable, for example, the $HOME env variable:
Home$ = getenv_("HOME")



marksibly(Posted 2004) [#3]
Hi,

getenv_("HOME") will probably work from within the IDE but probably not from a standalone app.

Try this. It ain't pretty, but should work:

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

Print HomeDir()
ChangeDir HomeDir()


I agree that this should be 'builtin'.


Phish(Posted 2004) [#4]
Blimey. Don't have a clue how that code works.

But thank you! :-)


marksibly(Posted 2004) [#5]

Blimey. Don't have a clue how that code works.



Either do I really!

It was the result of an intense google session, and it's the only variation I found that worked on both 10.2 and 10.3