https://kotlinlang.org logo
#compose-desktop
Title
# compose-desktop
r

Ralf Stephan

09/26/2023, 11:26 AM
Building an app on Linux, using Conveyor for Win/Mac binaries. Problem is, I can write my settings file in the current (installation) directory on Linux, but I'm told on Mac there is a permission error ("read-only"). This seems simple but I don't have a Mac, nor an idea how this is usually handled. Any hints?
a

Alexander Maryanovsky

09/26/2023, 1:50 PM
Settings shouldn’t typically be written to the installation directory. On any platform.
r

Ralf Stephan

09/26/2023, 1:52 PM
What about
System.getProperty("user.dir")
?
a

Alexander Maryanovsky

09/26/2023, 2:04 PM
I use this:
Copy code
/**
 * The directory where the app stores its user files.
 */
val USER_FILES_DIR = run {
    val userHome = System.getProperty("user.home")
    val appName = "MyApp"
    val appNameLowercase = appName.lowercase(Locale.ROOT)
    File(
        userHome,
        when (hostOs){
            OS.MacOS -> "Library/Application Support/$appName"
            OS.Windows ->
                if (File(userHome, "AppData").exists())
                    "AppData\\$appName\\$appName"  // Windows 7 and later
                else
                    "Application Data\\Local Settings\\$appName\\$appName"  // Windows XP
            OS.Linux -> ".local/share/$appNameLowercase"
            else -> ".$appNameLowercase"
        }
    )
}
r

Ralf Stephan

09/26/2023, 2:22 PM
Thanks!
m

Michael Paus

09/26/2023, 2:42 PM
Every platform has different storage locations for user data. Why not use a library like this to get the right ones? https://github.com/dirs-dev/directories-jvm
r

Ralf Stephan

09/26/2023, 3:38 PM
Very good!
3 Views