Explorar el Código

move to trash on MacOS

Johann ELSASS hace 5 años
padre
commit
986d7a21a2
Se han modificado 1 ficheros con 55 adiciones y 1 borrados
  1. 55 1
      lazpaint/ufilesystem.pas

+ 55 - 1
lazpaint/ufilesystem.pas

@@ -543,6 +543,56 @@ begin
 end;
 {$ENDIF}
 
+{$IFDEF DARWIN}
+function RunAppleScriptLine(AScriptLine: string): boolean;
+var
+  p: TProcess;
+begin
+  p := nil;
+  try
+    p := TProcess.Create(nil);
+    p.Executable := 'osascript';
+    p.Parameters.Add('-e');
+    p.Parameters.Add(AScriptLine);
+    p.Options := [poWaitOnExit];
+    p.Execute;
+    result := true;
+  except
+    result := false;
+  end;
+  p.Free;
+end;
+
+function AppleScriptEscape(AText: string): string;
+begin
+  result := StringReplace(AText, '\', '\\', [rfReplaceAll]);
+  result := StringReplace(result, '"', '\"', [rfReplaceAll]);
+end;
+
+function MoveToTrashOnMacOS(AForm: TForm; const AFilenamesUTF8: array of string; AConfirmationCallback: TDeleteConfirmationFunction): boolean;
+var
+  appleScript: String;
+  i: Integer;
+begin
+  if length(AFilenamesUTF8) = 0 then exit(true);
+  if Assigned(AConfirmationCallback) then
+  begin
+    if not AConfirmationCallback(AForm, AFilenamesUTF8, False) then exit(false);
+  end;
+  appleScript := 'tell application "Finder" to delete {';
+  for i := 0 to high(AFilenamesUTF8) do
+  begin
+    if i > 0 then appleScript += ', ';
+    appleScript += 'POSIX file "' + AppleScriptEscape(AFilenamesUTF8[i]) + '"';
+  end;
+  appleScript += '}';
+  if not RunAppleScriptLine(appleScript) then exit(false);
+  result := true;
+  for i := 0 to high(AFilenamesUTF8) do
+    if FileExists(AFilenamesUTF8[i]) then result := false;
+end;
+{$ENDIF}
+
 function IsMultiFileContainerName(AFilenameUTF8: string): boolean;
 var
   ext: String;
@@ -612,7 +662,11 @@ begin
       {$IFDEF WINDOWS}
       result := MoveToTrashOnWindows(AForm, realFiles, AConfirmationCallback);
       {$ELSE}
-      result := false;
+        {$IFDEF DARWIN}
+        result := MoveToTrashOnMacOS(AForm, realFiles, AConfirmationCallback);
+        {$ELSE}
+        result := false;
+        {$ENDIF}
       {$ENDIF}
     {$ENDIF}
   end;