moveToTrash static method

Future<bool> moveToTrash(
  1. String pathString
)

Moves a file or directory to trash (OS-dependent). Returns true if successful, false otherwise. Falls back to direct deletion if trash is not available.

Implementation

static Future<bool> moveToTrash(String pathString) async {
  try {
    if (Platform.isMacOS) {
      // Use macOS trash command
      final result = await Process.run('osascript', [
        '-e',
        'tell application "Finder" to move POSIX file "$pathString" to trash'
      ]);
      return result.exitCode == 0;
    } else if (Platform.isLinux) {
      // Use gio trash (GNOME) or fallback
      final result = await Process.run('gio', ['trash', pathString]);
      if (result.exitCode == 0) return true;
      // Fallback to direct deletion
      return _deleteDirectly(pathString);
    } else if (Platform.isWindows) {
      // Use PowerShell to move to Recycle Bin
      final result = await Process.run('powershell', [
        '-Command',
        'Add-Type -AssemblyName Microsoft.VisualBasic; [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile("$pathString", \'OnlyErrorDialogs\', \'SendToRecycleBin\')'
      ]);
      return result.exitCode == 0;
    }
    // Fallback to direct deletion
    return _deleteDirectly(pathString);
  } catch (e) {
    return _deleteDirectly(pathString);
  }
}