Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 958c92c

Browse files
committed
implement new param initialDirectory for macOS
1 parent c28a683 commit 958c92c

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

lib/src/file_picker_macos.dart

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class FilePickerMacOS extends FilePicker {
55
@override
66
Future<FilePickerResult?> pickFiles({
77
String? dialogTitle,
8+
String? initialDirectory,
89
FileType type = FileType.any,
910
List<String>? allowedExtensions,
1011
Function(FilePickerStatus)? onFileLoading,
@@ -22,6 +23,7 @@ class FilePickerMacOS extends FilePicker {
2223
final List<String> arguments = generateCommandLineArguments(
2324
escapeDialogTitle(dialogTitle ?? defaultDialogTitle),
2425
fileFilter: fileFilter,
26+
initialDirectory: initialDirectory ?? '',
2527
multipleFiles: allowMultiple,
2628
pickDirectory: false,
2729
);
@@ -50,10 +52,12 @@ class FilePickerMacOS extends FilePicker {
5052
Future<String?> getDirectoryPath({
5153
String? dialogTitle,
5254
bool lockParentWindow = false,
55+
String? initialDirectory,
5356
}) async {
5457
final String executable = await isExecutableOnPath('osascript');
5558
final List<String> arguments = generateCommandLineArguments(
5659
escapeDialogTitle(dialogTitle ?? defaultDialogTitle),
60+
initialDirectory: initialDirectory ?? '',
5761
pickDirectory: true,
5862
);
5963

@@ -72,6 +76,7 @@ class FilePickerMacOS extends FilePicker {
7276
Future<String?> saveFile({
7377
String? dialogTitle,
7478
String? fileName,
79+
String? initialDirectory,
7580
FileType type = FileType.any,
7681
List<String>? allowedExtensions,
7782
bool lockParentWindow = false,
@@ -85,6 +90,7 @@ class FilePickerMacOS extends FilePicker {
8590
escapeDialogTitle(dialogTitle ?? defaultDialogTitle),
8691
fileFilter: fileFilter,
8792
fileName: fileName ?? '',
93+
initialDirectory: initialDirectory ?? '',
8894
saveFile: true,
8995
);
9096

@@ -122,6 +128,7 @@ class FilePickerMacOS extends FilePicker {
122128
String dialogTitle, {
123129
String fileFilter = '',
124130
String fileName = '',
131+
String initialDirectory = '',
125132
bool multipleFiles = false,
126133
bool pickDirectory = false,
127134
bool saveFile = false,
@@ -141,14 +148,20 @@ class FilePickerMacOS extends FilePicker {
141148
argument += 'default name "$fileName" ';
142149
}
143150
} else {
144-
argument += 'of type {$fileFilter} ';
151+
if (fileFilter.isNotEmpty) {
152+
argument += 'of type {$fileFilter} ';
153+
}
145154

146155
if (multipleFiles) {
147156
argument += 'with multiple selections allowed ';
148157
}
149158
}
150159
}
151160

161+
if (initialDirectory.isNotEmpty) {
162+
argument += 'default location "$initialDirectory" ';
163+
}
164+
152165
argument += 'with prompt "$dialogTitle"';
153166
arguments.add(argument);
154167

test/file_picker_macos_test.dart

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ void main() {
243243

244244
expect(
245245
cliArguments.join(' '),
246-
equals('-e choose file of type {} with prompt "Select a file:"'),
246+
equals('-e choose file with prompt "Select a file:"'),
247247
);
248248
});
249249

@@ -277,7 +277,7 @@ void main() {
277277
expect(
278278
cliArguments.join(' '),
279279
equals(
280-
'-e choose file of type {} with multiple selections allowed with prompt "Select files:"',
280+
'-e choose file with multiple selections allowed with prompt "Select files:"',
281281
),
282282
);
283283
});
@@ -334,5 +334,51 @@ void main() {
334334
equals('-e choose folder with prompt "Select a directory:"'),
335335
);
336336
});
337+
338+
test('should generate the arguments for picking a file when an initial directory is given', () {
339+
final picker = FilePickerMacOS();
340+
341+
final cliArguments = picker.generateCommandLineArguments(
342+
'Pick a file:',
343+
initialDirectory: '/Users/john/Desktop',
344+
);
345+
346+
expect(
347+
cliArguments.join(' '),
348+
equals('-e choose file default location "/Users/john/Desktop" with prompt "Pick a file:"'),
349+
);
350+
});
351+
352+
test('should generate the arguments for picking a directory when an initial directory is given', () {
353+
final picker = FilePickerMacOS();
354+
355+
final cliArguments = picker.generateCommandLineArguments(
356+
'Pick directory:',
357+
fileName: 'output.pdf',
358+
initialDirectory: '/Users/john/workspace',
359+
pickDirectory: true,
360+
);
361+
362+
expect(
363+
cliArguments.join(' '),
364+
equals('-e choose folder default location "/Users/john/workspace" with prompt "Pick directory:"'),
365+
);
366+
});
367+
368+
test('should generate the arguments for saving a file when an initial directory is given', () {
369+
final picker = FilePickerMacOS();
370+
371+
final cliArguments = picker.generateCommandLineArguments(
372+
'Save as:',
373+
fileName: 'output.pdf',
374+
initialDirectory: '/Users/john/Downloads',
375+
saveFile: true,
376+
);
377+
378+
expect(
379+
cliArguments.join(' '),
380+
equals('-e choose file name default name "output.pdf" default location "/Users/john/Downloads" with prompt "Save as:"'),
381+
);
382+
});
337383
});
338384
}

0 commit comments

Comments
 (0)