From: Frederick Cheung Date: 2008-08-01T03:23:55+09:00 Subject: Re: How to get special directories? On 31 Jul 2008, at 18:29, Niklas Baumstark wrote: > Gordon Thiesfeld wrote: >> On Thu, Jul 31, 2008 at 12:13 PM, Niklas Baumstark >> wrote: >>> Eric Hodel wrote: >>>> On Jul 31, 2008, at 07:57 AM, Niklas Baumstark wrote: >>>>> how can i get ruby to get me some special directories, such as the >>>>> home-folder on linux or "documents and settings" on windows? is >>>>> there >>>>> already some functionality available to do this or will i have >>>>> to use >>>>> platform-native functions (for example COM on windows)? >>>> >>>> To get folders like 'documents and settings' on windows you need to >>>> use win32api. Depending on language, these may have different >>>> names. >> >> You can also use the win32-dir gem >> >> C:\>gem in win32-dir >> Successfully installed win32-dir-0.3.2 >> 1 gem installed >> C:\>irb >>>> require 'win32/dir' >> => true >>>> Dir::PROFILE >> => "C:\\Documents and Settings\\gthiesfeld" > > Thanks that'll help alot! Is something similiar available on linux as > well? > Dunno about linux, but here's something that will work on mac os x. The way mac os x tracks things like disk is a combination of a domain and a folder type. Domains are thinks like user domain, local domain etc..., folder types are things like 'applications folder', 'library folder' and so on. So (in C) the user's library folder (/Users/fred/Library in my case) is identified by kUserDomain, kDomainLibraryFolderType and the top level one (/Library) is identified by kLocalDomain, kDomainLibraryFolderType The following code uses rubyinline to call some C code. This doesn't seem to work if you paste it into irb, but as a standalone ruby file runs fine require 'rubygems' gem 'RubyInline' require 'inline' class Folder module Domains USER = -32763 SYSTEM_DISK = -32768 SYSTEM = -32766 LOCAL = -32765 end module Types def self.int_for_code code code.unpack('N').first end TOP = int_for_code 'dtop' #more of these constants in Folders.h DOCUMENTS = int_for_code 'docs' end inline do |builder| builder.include "" builder.add_compile_flags "-framework CoreServices" builder.c_singleton " static char *find(int domain, unsigned long folder_type){ char c_path[PATH_MAX]; FSRef folder; if(noErr == FSFindFolder(domain, folder_type, true, &folder)){ if(noErr ==FSRefMakePath(&folder, (UInt8*)c_path, PATH_MAX)) return c_path; } /* decide what you want to do in case of failure */ return \"err\"; } " end end puts Folder::find(Folder::Domains::USER, Folder::Types::DOCUMENTS) #should output the user's documents folder puts Folder::find(Folder::Domains::USER, Folder::Types::TOP) #should output the top level of the user domain, ie the home folder