I am planning to develop a cross-platform script. On Linux and other operating systems, it will store configuration in XDG_CONFIG_HOME
and data files (specifically, downloaded plugins) in XDG_DATA_HOME
. On Windows, it will use APPDATA
for both (unless someone has a better idea). However, what would be the proper thing to do on Mac OS X?
On my first glance through a handy Macbook's ~/Library
directory, I saw Preferences
and Application Support
folders. I was originally planning to use those, but Preferences
seems to just contain plists with reverse domain names like com.apple.foo.bar.baz.plist
, and every folder in Application Support
corresponds to a bundle in /Applications
, so I'm not sure how well the system would react to files that don't match its standards. Storing them directly in ~/Library
might be an option, but I don't want to pollute it with a stray myscript.conf
file if there's a better place for it.
Where should I store these files? (And please don't say just ~/.myscript
. I know it's the Unix tradition, but it annoys me to see random dotfiles in the home directory.)
$XDG_CONFIG_HOME defines the base directory relative to which user specific configuration files should be stored.
The XDG Base Directory Specification (XDGBDS) defines four categories of so called DotFiles and the corresponding directories in a users home directory that should be used for those. The categories are cache, configuration, data and runtime files.
Comparing Apple's documentation for the various paths to the XDG Base Directory specifications approximates to the following locations:
XDG_CONFIG_HOME
▶︎ ~/Library/Preferences/
XDG_DATA_HOME
▶︎ ~/Library/
XDG_CACHE_HOME
▶︎ ~/Library/Caches/
Mapping XDG Base Directory Specification locations for "My App" on Mac OS X could look like this:
XDG_CONFIG_HOME
▶︎ ~/Library/Preferences/name.often.with.domain.myapp.plist
XDG_DATA_HOME
▶︎ ~/Library/My App/
XDG_CACHE_HOME
▶︎ ~/Library/Caches/My App/
These mappings seem pretty reasonable but they aren't exact. Some kinds of cache or data may be appropriate for ~/Library/Application Support/My App
, and other may be best in the temp locations or the App bundle. All of it is by convention and the same reasons for using the best XDG_
locations apply to using the best locations on the Mac OS X system.
Your annoyance at ~/.myscript
is in line with Apple's guidelines: "Don't pollute user space".
References:
I agree with OJFord's comment: if you are writing a script (I think this applies to all CLI-only application), simply follow the XDG Base Directory would be better.
Rationales:
XDG_CONFIG_HOME
are typically small-case bare-names like git
; the ones in ~/Library/Preferences/
are typically files named with reverse domain name notation like com.apple.AppStore.plist
, or directories named in title case like Blackmagic Design
./Applications
. You don't place your script in /Applications
; you place them in UNIX-specific directorys. Better to be consistent.~/Library/Preferences/
is full of .plist
s. No cross-platform script that I know is using property lists.For example, Git places their config file in XDG_CONFIG_HOME/git/config
, not in ~/Library/Preferences/Git/config
. Makes sense to me.
I made the point only for configuration files ($XDG_CONFIG_HOME
) and data files ($XDG_DATA_HOME
); for cache files it gets subtle. According to How-To Geek, ~/Library/Caches
directory is excluded from Time Machine by default. I don’t care about cache folder since I back up neither directories anyway; but for ones who care, I recommend them to link ~/.cache
to somewhere in the default cache folder, such as:
cd ~; mv .cache ~/Library/Caches/XDG-cache; ln -s ~/Library/Caches/XDG-cache .cache
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With