Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Share:
Notifications
Clear all

[Solved] Lazarus - MacOS X - How to find the DropBox path

1 Posts
1 Users
0 Reactions
1,467 Views
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter  

When integrating the use of DropBox in your application, you'd first need to be able to find the path to the DropBox directory.
Dropbox documented this well (see this link), and a JSON file is available in the user's home directory:

~/.dropbox/info.json

This file contains the needed info an is formatted as such (if you have only a personal, or only a business DropBox account, then obviously the other one does not exist):

{
    "business": {
        "host": 123456789,
        "path": "/Users/<USERNAME>/Dropbox (<BUSINESS_NAME>)"
    },
    "personal": {
        "host": 123456789,
        "path": "/Users/<USERNAME>/Dropbox (Personal)"
    }
}
  • business/personal: The type of account.
  • host: An identifier that uniquely specifies a particular user account and computer pair.
  • path: The path to this Dropbox folder.
To read this DropBox configuration file, we will need a JSON parser (see Lazarus Wiki).
A simple example on how to retrieve the DropBox path:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, fpjson, jsonparser;
type
  { TForm1 }
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;
var
  Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
var
   jData : TJSONData;
   jObject : TJSONObject;
   jArray : TJSONArray;
   jText : TStringList;
   DropBoxPath : string;
begin
  if FileExists(ExpandFileName('~/.dropbox/info.json')) then
    begin
      jText:=TStringList.Create;
      jText.LoadFromFile(ExpandFileName('~/.dropbox/info.json'));
      jData := GetJSON(jText.Text);
      jObject := TJSONObject(jData);
      DropBoxPath:='';
      try
        DropBoxPath:=jData.FindPath('personal.path').AsString;
      except
        // Do nothing
      end;
      if DropBoxPath='' then
        try
        DropBoxPath:=jData.FindPath('business.path').AsString;
      except
        // Do Nothing
      end;
      if DropBoxPath<>'' then
        Label1.Caption:=DropBoxPath;
      else
        Label1.Caption:='Not found';
      jText.Free;
      jObject.Free;
      jData.Free;
    end;
end;
end.


   
ReplyQuote
Share: