diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d76dbaf..16960aef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 4.2.5 + +##### Desktop (macOS) +Fixes the issue of picking filenames that contain a comma followed by a space ([#890](https://github.com/miguelpruivo/flutter_file_picker/issues/890)). + ## 4.2.4 ##### iOS diff --git a/lib/src/file_picker_macos.dart b/lib/src/file_picker_macos.dart index af5bb05b..93983930 100644 --- a/lib/src/file_picker_macos.dart +++ b/lib/src/file_picker_macos.dart @@ -158,21 +158,32 @@ class FilePickerMacOS extends FilePicker { .replaceAll('\n', '\\\n'); /// Transforms the result string (stdout) of `osascript` into a [List] of - /// file paths. + /// POSIX file paths. List resultStringToFilePaths(String fileSelectionResult) { if (fileSelectionResult.trim().isEmpty) { return []; } - return fileSelectionResult + + final paths = fileSelectionResult .trim() - .split(', ') + .split(', alias ') .map((String path) => path.trim()) .where((String path) => path.isNotEmpty) - .map((String path) { - final pathElements = path.split(':').where((e) => e.isNotEmpty).toList(); + .toList(); + + if (paths.length == 1 && paths.first.startsWith('file ')) { + // The first token of the first path is "file" in case of the save file + // dialog + paths[0] = paths[0].substring(5); + } else if (paths.isNotEmpty && paths.first.startsWith('alias ')) { + // The first token of the first path is "alias" in case of the + // file/directory picker dialog + paths[0] = paths[0].substring(6); + } - // First token is either "alias" or "file" (in case of saveFile dialog) - final volumeName = pathElements[0].split(' ').sublist(1).join(' '); + return paths.map((String path) { + final pathElements = path.split(':').where((e) => e.isNotEmpty).toList(); + final volumeName = pathElements[0]; return ['/Volumes', volumeName, ...pathElements.sublist(1)].join('/'); }).toList(); } diff --git a/pubspec.yaml b/pubspec.yaml index 380e1a4e..8886a15f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -3,7 +3,7 @@ description: A package that allows you to use a native file explorer to pick sin homepage: https://github.com/miguelpruivo/plugins_flutter_file_picker repository: https://github.com/miguelpruivo/flutter_file_picker issue_tracker: https://github.com/miguelpruivo/flutter_file_picker/issues -version: 4.2.4 +version: 4.2.5 dependencies: flutter: diff --git a/test/file_picker_macos_test.dart b/test/file_picker_macos_test.dart index 2b28f12f..2ecdf877 100644 --- a/test/file_picker_macos_test.dart +++ b/test/file_picker_macos_test.dart @@ -189,6 +189,40 @@ void main() { equals('/Volumes/TAILS 4.20 - 202/EFI/debian/grub/x86_64-efi'), ); }); + + test( + 'should interpret the result of picking filenames that contain blanks and commas', + () { + final picker = FilePickerMacOS(); + + final filePaths = picker.resultStringToFilePaths( + 'alias Macintosh:Users:JohnDoe:test, test.csv, alias macOS Base System:bin:unicorn , generator.sh', + ); + + expect(filePaths.length, equals(2)); + expect( + filePaths[0], + equals('/Volumes/Macintosh/Users/JohnDoe/test, test.csv'), + ); + expect( + filePaths[1], + equals('/Volumes/macOS Base System/bin/unicorn , generator.sh'), + ); + }); + + test('should interpret the result of the save file dialog', () { + final picker = FilePickerMacOS(); + + final filePaths = picker.resultStringToFilePaths( + 'file macOS:Users:JohnDoe:Desktop:bill.pdf', + ); + + expect(filePaths.length, equals(1)); + expect( + filePaths[0], + equals('/Volumes/macOS/Users/JohnDoe/Desktop/bill.pdf'), + ); + }); }); group('generateCommandLineArguments()', () {