diff --git a/modules/juce_core/native/juce_posix_SharedCode.h b/modules/juce_core/native/juce_posix_SharedCode.h index 80c2faf301..efa32bf0fc 100644 --- a/modules/juce_core/native/juce_posix_SharedCode.h +++ b/modules/juce_core/native/juce_posix_SharedCode.h @@ -1000,7 +1000,8 @@ public: Array argv; for (int i = 0; i < arguments.size(); ++i) - argv.add (arguments[i].toUTF8().getAddress()); + if (arguments[i].isNotEmpty()) + argv.add (arguments[i].toUTF8().getAddress()); argv.add (nullptr); @@ -1069,12 +1070,15 @@ bool ChildProcess::start (const String& command) { StringArray tokens; tokens.addTokens (command, true); - tokens.removeEmptyStrings (true); + return start (tokens); +} - if (tokens.size() == 0) +bool ChildProcess::start (const StringArray& args) +{ + if (args.size() == 0) return false; - activeProcess = new ActiveProcess (tokens); + activeProcess = new ActiveProcess (args); if (activeProcess->childPID == 0) activeProcess = nullptr; diff --git a/modules/juce_core/native/juce_win32_Threads.cpp b/modules/juce_core/native/juce_win32_Threads.cpp index 7129092185..24e405ccfb 100644 --- a/modules/juce_core/native/juce_win32_Threads.cpp +++ b/modules/juce_core/native/juce_win32_Threads.cpp @@ -563,6 +563,11 @@ bool ChildProcess::start (const String& command) return activeProcess != nullptr; } +bool ChildProcess::start (const StringArray& args) +{ + return start (args.joinIntoString (" ")); +} + bool ChildProcess::isRunning() const { return activeProcess != nullptr && activeProcess->isRunning(); diff --git a/modules/juce_core/threads/juce_ChildProcess.h b/modules/juce_core/threads/juce_ChildProcess.h index 454a6c6cb5..2ac5b77ec9 100644 --- a/modules/juce_core/threads/juce_ChildProcess.h +++ b/modules/juce_core/threads/juce_ChildProcess.h @@ -57,6 +57,15 @@ public: */ bool start (const String& command); + /** Attempts to launch a child process command. + + The first argument should be the name of the executable file, followed by any other + arguments that are needed. + If the process has already been launched, this will launch it again. If a problem + occurs, the method will return false. + */ + bool start (const StringArray& arguments); + /** Returns true if the child process is alive. */ bool isRunning() const; diff --git a/modules/juce_gui_basics/native/juce_linux_FileChooser.cpp b/modules/juce_gui_basics/native/juce_linux_FileChooser.cpp index 39da8630b2..4721749b07 100644 --- a/modules/juce_gui_basics/native/juce_linux_FileChooser.cpp +++ b/modules/juce_gui_basics/native/juce_linux_FileChooser.cpp @@ -45,18 +45,37 @@ void FileChooser::showPlatformDialog (Array& results, FilePreviewComponent* previewComponent) { const String separator (":"); - String command ("zenity --file-selection"); - if (title.isNotEmpty()) command << " --title=\"" << title << "\""; - if (file != File::nonexistent) command << " --filename=\"" << file.getFullPathName () << "\""; - if (isDirectory) command << " --directory"; - if (isSave) command << " --save"; - if (selectMultipleFiles) command << " --multiple --separator=" << separator; + StringArray args; + args.add ("zenity"); + args.add ("--file-selection"); - command << " 2>&1"; + if (title.isNotEmpty()) args.add ("--title=" + title); + if (isDirectory) args.add ("--directory"); + if (isSave) args.add ("--save"); + + if (selectMultipleFiles) + { + args.add ("--multiple"); + args.add ("--separator=" + separator); + } + + const File previousWorkingDirectory (File::getCurrentWorkingDirectory()); + + if (file.isDirectory()) + { + file.setAsCurrentWorkingDirectory(); + } + else if (file.exists()) + { + file.getParentDirectory().setAsCurrentWorkingDirectory(); + args.add ("--filename=" + file.getFileName()); + } + + args.add ("2>&1"); ChildProcess child; - if (child.start (command)) + if (child.start (args)) { const String result (child.readAllProcessOutput().trim()); @@ -75,4 +94,6 @@ void FileChooser::showPlatformDialog (Array& results, child.waitForProcessToFinish (60 * 1000); } + + previousWorkingDirectory.setAsCurrentWorkingDirectory(); }