JetBrains / compose-multiplatform

Compose Multiplatform, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.
https://jetbrains.com/lp/compose-multiplatform
Apache License 2.0
15.78k stars 1.14k forks source link

How to get current working dir on MacOs in compose desktop? #1960

Open thelou1s opened 2 years ago

thelou1s commented 2 years ago

I wanna get current working dir, in order to call some commands. I copied code from jetpack compose desktop lib's demo, but got wrong dir("/"). Could any one tell me what's the problem? thank you.

(jetpack compose desktop lib: https://github.com/JetBrains/compose-jb/blob/master/tutorials/Native_distributions_and_local_execution/README.md)

                        val userDirStr = File(System.getProperty("user.dir"))
                        if (userDirStr != null) {
                            println("Row, onGloballyPositioned, userDirStr = " + userDirStr)
                            outputText = "directory = " + userDirStr
                        }
AlexeyTsvetkov commented 2 years ago

@thelou1s The code seems to be correct (you can also simply use File("").absoluteFile since JVM resolves relative paths relative to a working directory). However, I'm not sure, what do you expect a working directory of a graphical application to be? A working directory "is typically the directory in which the Java virtual machine was invoked." When launched from a console, it will be a normal terminal emulator's working directory. However, usually graphical applications are launched via some other application (e.g. Finder on macOS, explorer on Windows). In that case, a working directory may be a working directory of a "parent" application (but generally it seems to be platform specific). Could you provide more details about your use case?

thelou1s commented 2 years ago

I build a new Compose Crossplatform project, and copy old code into it. Then "File(System.getProperty("compose.application.resources.dir"))" just got the right path.

https://stackoverflow.com/questions/71316296/how-to-get-current-working-dir-on-macos-in-compose-desktop

okushnikov commented 1 month ago

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.