resultStringToFilePaths method

List<String> resultStringToFilePaths(
  1. String fileSelectionResult
)

Transforms the result string (stdout) of osascript into a List of POSIX file paths.

Implementation

List<String> resultStringToFilePaths(String fileSelectionResult) {
  if (fileSelectionResult.trim().isEmpty) {
    return [];
  }

  final paths = fileSelectionResult
      .trim()
      .split(', alias ')
      .map((String path) => path.trim())
      .where((String path) => path.isNotEmpty)
      .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);
  }

  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();
}