Search code examples
delphifiremonkeydelphi-10.2-tokyo

Delphi - Checking for Dropbox on OSX


I am writing a multi-platform application using Delphi 10.2 Tokyo Firemonkey. One of the things I need to check is if Dropbox exists on the computer. For this I need to check for the presence of an info.json file and then process that json file to get the path of the Dropbox folder.

I wrote this function to check for the presence of Dropbox:

class function TUtilityMac.DropboxExists: Boolean;
var
  infojsonpath: String;
begin
  Result:=false;
  infojsonpath:='~/.dropbox/info.json';
  if not FileExists (infojsonpath, True) then
    exit;
  Result:=true;
end;

But when I run this on a Mac (that has Dropbox installed), the FileExists function returns false (regardless of the second parameter being True or False). If I open a terminal window and do a cd ~/.dropbox and then a dir, I do see the info.json file there.

Any thoughts around what I am missing? Would appreciate any pointers regarding this...


Solution

  • Well - I figured it out (by trial and error).

    The issue is that when we use the literal ~/.dropbox, Delphi is looking for that precise folder, which of course does not exist. The ~ on OSX refers to the user's directory (e.g. in my case it would be /Users/rohit). So if I replaced the ~ with /Users/rohit, the application found the file and everything worked as expected.

    Just for completeness of the answer, the function can be written as:

    class function TUtilityMac.DropboxExists: Boolean;
    var
      infojsonpath: String;
    begin
      infojsonpath := IncludeTrailingPathDelimiter(GetHomePath) + '.dropbox/info.json';
      Result := FileExists(infojsonpath, True);
    end;
    

    Note that the key here is to use GetHomePath() to get the current user's directory on OSX; on Windows, it returns the current user's %APPDATA% folder.