SOLVED: Detect if macOS file is readable lazarus / 33-macOS - Mac OS X ---------------------------------------------------------------------- ChrisR · 2022-08-29 13:40 For GUI applications, a file may be readable based on file permissions, but not readable as it is outside the applications sand box. This is described hear with detection code:   https://wiki.freepascal.org/macOS_Programming_Tips#Determining_if_a_ file_is_readable However, this detection fails if the file path is longer than 255 bytes   https://github.com/neurolabusc/surf-ice/issues/35 Are there any comments regarding my suggested code, which handles long file names: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function IsReadable (fnm: string): boolean;//https://wiki.freepascal.org/macOS_ Programming_Tips#Determining_if_a_file_is_readablevar  fs : TFileStream;     b: byte;begin  result := false;  if not fileexists(fnm) then begin    //writeln('File does not exist: '+fnm);    exit;  end;  if FSize(fnm) < 2 then begin    //writeln('Empty file: '+fnm);    exit;  end;  {$IFDEF Darwin}  {$I-}  try  fs := TFileStream.Create (fnm, fmOpenRead or fmShareDenyWrite);  except    //writeln('Unable to open (permissions): '+fnm);    exit(false);  end;  try    fs.Seek(0, soFromBeginning);    fs.Read(b, 1);    //writeln(inttostr(b));  except    //writeln('Unable to read: '+fnm);    exit(false);  end;  {$ENDIF}  result := true;end; ---------------------------------------------------------------------- KodeZwerg · 2022-08-30 12:40 You forgot to free the created object. ---------------------------------------------------------------------- KodeZwerg · 2022-08-30 13:25 tested : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function IsReadable(const AFilename: string): Boolean;var  fs: TFileStream;  b : Byte;begin  // whatever happen, it is wrong :-)  Result := False;  try    // try connect to target    fs := TFileStream.Create(AFilename, fmOpenRead or fmShareDenyWrite);    try      // ensure that what we want to read is available      if (fs.Size >= SizeOf(Byte)) then      begin          try            // turn compiler warning off          b := 0;            // jump to offset            fs.Position := 0;          // compare if the size we want to test is given back            Result := (SizeOf(Byte) = fs.Read(b, SizeOf(Byte)));          except          // something went wrong at reading          end;        end;    finally      // free the object      fs.Free;    end;  except    // something went wrong on accessing or in general    // file not found  // access error like forbid, no permission etc...  end;end;  added explanation. ---------------------------------------------------------------------- ChrisR · 2022-09-02 18:32 Thanks for the elegant code! ---------------------------------------------------------------------- KodeZwerg · 2022-09-03 06:04 Quote from: ChrisR on September 02, 2022, 08:32:33 pmThanks for the elegant code! You are very welcome!