1

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...

Rohit
  • 909
  • 1
  • 9
  • 20
  • Are you sure you really need this curvy double negation logic? `DropboxExists := FileExists('~/.dropbox/info.json', True)` – Free Consulting Apr 18 '17 at 10:45
  • That is just a standalone code snippet to show the issue - not the actual code in my app. In my actual code I find the file and parse the json to get the actual location of the Dropbox folder. But I agree with you, it could have been as compact as you suggest :) – Rohit Apr 18 '17 at 10:48
  • 1
    If you remove the `FileExists()` check, are you able to load the file using the same path? If so, then just forget `FileExists()` and always load the file unconditionally and handle any load errors as needed. – Remy Lebeau Apr 18 '17 at 14:52
  • Thanks @RemyLebeau - will give that a try and let you know how it works out... – Rohit Apr 19 '17 at 08:57
  • @RemyLebeau - I got a `EFileNotFoundException` when I tried to load the file. But the file exists. Any idea what might be going on? – Rohit Apr 19 '17 at 14:07
  • I figured it out - will respond with an answer to the question... – Rohit Apr 19 '17 at 14:49

1 Answers1

2

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Rohit
  • 909
  • 1
  • 9
  • 20