Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to read a data folder and configuration file in .DMG Mac OS file?

I am developing an app for Windows and Mac, and I need some help regarding how should I organize the file structure of my input data (e.g. configuration files), so that I can make it easy to use for Mac.

Here is how I set it up for Windows:

I have a very simple program that reads some data from the directory data. In Windows I created an example.exe and the data folder lives next to it. If the user needs to process more data (that receive in an email), she just needs to put it the file in the folder and double click on example.exe.

Here is what I have done for Mac Os:

I managed to create the DMG but I have no idea where to put the data folder. Hardcoding another folder (e.g. HOME or Desktop) works, but it just doesn't feel right. I thought of having a Button that opens a Dialog to specify the folder, but then I need to save the path for the next time the user opens the application. In order to save this path, I need a configuration file or workspace. So, again, I need a path where I can find the configuration file.

What would be the expected behaviour from a Mac usability perspective, and how I would go about it in terms of directory locations and configuration files?

like image 664
toto_tico Avatar asked Nov 08 '22 08:11

toto_tico


1 Answers

I would say there is nothing wrong with having the data folder live side by side with the executable python script in macOS.

I'm assuming this is a script only with no GUI. If there was a GUI, I say the user should be encouraged to copy the application into /Applications (or ~/Applications), then provide a way for the user to select a "data" folder using a dialog box.

I think the best solution, though, that will work for both Windows and macOS is to allow the data directory's path to be specified as a command-line argument. If none is specified, it can default to ./data

If you want to save the user-specified setting for the path to the data directory, a good, mac-like technique is to use the defaults system. You can easily read and write defaults in a script using the defaults read and defaults write commands. Defaults work using reverse domain name notation to help ensure that they are unique across all applications.

Lets say your organization is named ttwidgets, if you had a website named ttwidgets.com and this script is named pywidget, you would use the domain com.ttwidgets.pywidget.

The syntax to set the default is:

% defaults write com.ttwidgets.pywidget datapath /user/specified/path

This is then managed by macOS and stored persistently. You can read back the setting if it exists again later using:

% defaults read com.ttwidgets.pywidget datapath
/user/specified/path

if nothing is set (e.g., first use), you will get an error (and the command returns a non-zero return code:

% defaults read com.ttwidgets.pywidget an_unset_variable
2018-04-21 12:08:40.179 defaults[12409:1472790]
The domain/default pair of (com.ttwidgets.pywidget, an_unset_variable) does not exist
% echo $?
1

This should provide all the tools necessary to use macOS's built-in preferred method for persistently storing key/value settings for applications.

like image 69
drootang Avatar answered Nov 15 '22 06:11

drootang