openFileBrowser function

void openFileBrowser(
  1. FileSystemEntity location, {
  2. Platform platform = const LocalPlatform(),
  3. ProcessManager processManager = const LocalProcessManager(),
})

Implementation

void openFileBrowser(FileSystemEntity location,
    {Platform platform = const LocalPlatform(),
    ProcessManager processManager = const LocalProcessManager()}) {
  switch (platform.operatingSystem) {
    case 'linux':
      // Tries to open the system file manager using DBus and select the file.
      // Some file managers don't support selecting the file, but it will at
      // least open the directory that the file exists in.
      final ProcessRunner runner =
          ProcessRunner(processManager: processManager);
      runner.runProcess(<String>[
        'dbus-send',
        '--session',
        '--print-reply',
        '--dest=org.freedesktop.FileManager1',
        '/org/freedesktop/FileManager1',
        'org.freedesktop.FileManager1.ShowItems',
        'array:string:${location.absolute.path}',
        'string:""',
      ]).then((ProcessRunnerResult result) {
        if (result.exitCode != 0) {
          print(
              'Failed to open file ${location.absolute.path}: ${result.output}');
        }
      });
      break;
    case 'macos':
      processManager.run(<String>['open', '-R', location.absolute.path]);
      break;
    case 'windows':
      processManager.run(<String>[
        'start',
        if (location is Directory)
          location.absolute.path
        else
          location.parent.absolute.path
      ], runInShell: true);
      break;
    default:
      throw Exception(
          'Opening files on platform ${platform.operatingSystem} is not supported.');
  }
}