9

In a cross-platform application, I am working with a configuration file that allows the user to override various defaults if he wishes to.

The problem I have is... where to place / look for this configuration file, especially with regards to MacOS X (which I never used and have no access to)? I know that MacOS X is based on Unix, but I also know that many things are done very much differently there...

My current choices:

Unix:

~/.config/<appname>/<appname>.cfg

Windows (shomewhat unsure about this one too, things seem to be all over the place here - %LOCALAPPDATA% or %USERPROFILE% seem to be valid choices too):

%APPDATA%/<appname>/<appname>.cfg

MacOS:

...?

Any comments, on MacOS or my other choices?

DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • 1
    It should be noted however that for Unix the preferred path is actually `${XDG_CONFIG_DIR:-${HOME}/.config}//.cfg`. The `~/.config` is just the default value of `$XDG_CONFIG_HOME`. – ntninja Apr 20 '18 at 13:30
  • @alexander255: Wow, very nice finding, thank you! For anyone as confused at first glance as me, XDG_CONFIG_DIR refers to a specification by [freedesktop.org](https://en.wikipedia.org/wiki/Freedesktop.org), formerly "X Desktop Group" (hence "XDG"). – DevSolar Apr 20 '18 at 13:46

2 Answers2

10

I think it must be ~/Library/Application Support/<appname>/<appname>.cfg
See the table in Mac App Programming Guide : "The Mac Application Environment" -> "Low-Level Details of the Runtime Environment" -> "The File System".

cody
  • 3,233
  • 1
  • 22
  • 25
  • It doesn't have to be done that way, especially if it's just a simple command line app that is portable. – Randy Howard Mar 28 '13 at 08:55
  • 2
    @RandyHoward: It's not exactly a "simple command line app". That's why I was wondering where a Mac user would *expect* the config file to be, instead of just storing it somewhere and telling the user to cope. ;-) – DevSolar Mar 28 '13 at 08:58
  • What I mean is, it's not some big OS X Gui cocoa app, that expects things to be done in a platform-specific way. If you app can run it on OS X, UNIX and Windows platforms, it is obviously not a "Mac Application". I stand by my answer, simplicity basically. – Randy Howard Mar 28 '13 at 09:00
  • 1
    @RandyHoward you can store files anywhere under the user's home directory. The question is where user will expect they will be. – cody Mar 28 '13 at 09:06
  • Many programs (especially of the gnu variety), or of unix/linux heritage in general drop config files in the home directory. If your program is a portable GUI app (you don't say in your question what is is), say something in QT, then the target user for that might not be comfortable with the command line, or digging around for config files in their home directory at all. It all depends upon your target user. Which you did not define. – Randy Howard Mar 28 '13 at 09:08
  • @RandyHoward: ...which is difficult to define, because I'm actually looking at a *framework* in my case (which might be refined to a simple command line tool *or* a Qt application). Anyways, both answers (and this discussion) have been helpful. Checkmark goes to cody for the more specific information. – DevSolar Mar 28 '13 at 09:10
  • 1
    Not worried about the checkmark. My point was more with expected behavior. As a "user" of an app, I expect its configuration method to be consistent with its application type. If it's an OS X GUI app, I expect a preferences pane, accessible from -, for example. If it's a command line tool, pretty much regardless of complexity, I expect to configure it through a startup file buried under my home directory, or may be /etc/something. It's about matching the product to user expectations. Anyway, glad you felt like you got help here. – Randy Howard Mar 28 '13 at 09:40
3

You can store it in the same manner you show for Unix/Linux above.

~ works the same way for home directories on OS X as UNIX platforms. Same goes for . in names for hidden directories.

Given the cross-platform needs you describe, I would do it exactly the same way on OS X as in your UNIX example.

Randy Howard
  • 2,165
  • 16
  • 26