Skip to content

Commit ed54cbe

Browse files
committed
fix #890: change separator for splitting the path
Change the pattern from ", " to ", alias " when splitting the output of osascript. Although this is not a perfect solution, it allows to pick filenames that contain a comma followed by a blank (", ").
1 parent 13bc9b4 commit ed54cbe

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

lib/src/file_picker_macos.dart

+18-7
Original file line numberDiff line numberDiff line change
@@ -158,21 +158,32 @@ class FilePickerMacOS extends FilePicker {
158158
.replaceAll('\n', '\\\n');
159159

160160
/// Transforms the result string (stdout) of `osascript` into a [List] of
161-
/// file paths.
161+
/// POSIX file paths.
162162
List<String> resultStringToFilePaths(String fileSelectionResult) {
163163
if (fileSelectionResult.trim().isEmpty) {
164164
return [];
165165
}
166-
return fileSelectionResult
166+
167+
final paths = fileSelectionResult
167168
.trim()
168-
.split(', ')
169+
.split(', alias ')
169170
.map((String path) => path.trim())
170171
.where((String path) => path.isNotEmpty)
171-
.map((String path) {
172-
final pathElements = path.split(':').where((e) => e.isNotEmpty).toList();
172+
.toList();
173+
174+
if (paths.length == 1 && paths.first.startsWith('file ')) {
175+
// The first token of the first path is "file" in case of the save file
176+
// dialog
177+
paths[0] = paths[0].substring(5);
178+
} else if (paths.isNotEmpty && paths.first.startsWith('alias ')) {
179+
// The first token of the first path is "alias" in case of the
180+
// file/directory picker dialog
181+
paths[0] = paths[0].substring(6);
182+
}
173183

174-
// First token is either "alias" or "file" (in case of saveFile dialog)
175-
final volumeName = pathElements[0].split(' ').sublist(1).join(' ');
184+
return paths.map((String path) {
185+
final pathElements = path.split(':').where((e) => e.isNotEmpty).toList();
186+
final volumeName = pathElements[0];
176187
return ['/Volumes', volumeName, ...pathElements.sublist(1)].join('/');
177188
}).toList();
178189
}

test/file_picker_macos_test.dart

+33
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,39 @@ void main() {
189189
equals('/Volumes/TAILS 4.20 - 202/EFI/debian/grub/x86_64-efi'),
190190
);
191191
});
192+
193+
test('should interpret the result of picking filenames that contain blanks and commas',
194+
() {
195+
final picker = FilePickerMacOS();
196+
197+
final filePaths = picker.resultStringToFilePaths(
198+
'alias Macintosh:Users:JohnDoe:test, test.csv, alias macOS Base System:bin:unicorn , generator.sh',
199+
);
200+
201+
expect(filePaths.length, equals(2));
202+
expect(
203+
filePaths[0],
204+
equals('/Volumes/Macintosh/Users/JohnDoe/test, test.csv'),
205+
);
206+
expect(
207+
filePaths[1],
208+
equals('/Volumes/macOS Base System/bin/unicorn , generator.sh'),
209+
);
210+
});
211+
212+
test('should interpret the result of the save file dialog', () {
213+
final picker = FilePickerMacOS();
214+
215+
final filePaths = picker.resultStringToFilePaths(
216+
'file macOS:Users:JohnDoe:Desktop:bill.pdf',
217+
);
218+
219+
expect(filePaths.length, equals(1));
220+
expect(
221+
filePaths[0],
222+
equals('/Volumes/macOS/Users/JohnDoe/Desktop/bill.pdf'),
223+
);
224+
});
192225
});
193226

194227
group('generateCommandLineArguments()', () {

0 commit comments

Comments
 (0)