Posts Tagged ‘OS X API’

App Tethering : Overview

One of the capabilities introduced in recent versions of RAD Studio and AppMethod is App Tethering. Over a number of posts I want to explore App Tethering further, starting from an overview of the technology and then going further down… down eventually to the network packet level. My aim is you’ll come out with a quite thorough understanding not just how to use App Tethering, but also what you might use it for.

In the absence of anywhere else to start, let’s start with a question.

What is App Tethering?

In its simplest terms, App Tethering makes it easy for apps to find and communicate with each other. Now, I accept that is so generic an explanation as to be almost useless. You could say something similar about DataSnap, or EMS, or plenty of other distributed systems. So let’s shed a bit more light.

Read On…

One man’s URL encoding is another man’s bug

I got an email last night from someone who was using my sample code to open URLs in the default browser on OSX. However, he was having trouble when his URL contained a string.

Easy peasy, I thought, just URL Encode it. (Actually, easy peasy isn’t the phrase I used, but I’m trying to keep this relatively clean). I jumped into Delphi for what I thought would be a quick 5 minutes to alter the code, and finished 2 hours later with it finally working, having learnt more than I really wanted to know about URL encoding along the way. Read On…

Opening files and URLs in default applications in OS X

After my article on Special Folders, a few people asked how to open a file in the default application.

For example, if I open a JPEG, whichever application is registered as the default app for JPEGs will execute. This is roughly analogous to ShellExecute in Windows.

The question was also asked about opening a URL in the default browser and also sending an email with the default mail application. These are closely related so I’ll cover them at the same time.

It’s actually pretty easy. I’ve got a TOpenDialog on a form, and the following code in a TButton.OnClick event:

procedure TForm2.Button1Click(Sender: TObject);
var
  Workspace : NSWorkspace;
begin
  if OpenDialog1.Execute then
  begin
    Label1.Text := OpenDialog1.FileName;
    Workspace := TNSWorkspace.Create;
    Workspace.openFile(NSSTR(Label1.Text));
  end;
end;

Once we have the filename from the TOpenDialog, we create a NSWorkspace reference and use the openFile method, converting the filename string to a NSString on the way. NSWorkspace is defined in Macapi.Appkit and the NSSTR function to convert a String to a NSString is defined in Macapi.Foundation;

Opening a URL in a browser is slightly longer but not much:

procedure TForm2.Button3Click(Sender: TObject);
var
  URL : NSURL;
  Workspace : NSWorkspace;
begin
    URL := TNSURL.Create;
    URL.initWithString(NSSTR('http://www.malcolmgroves.com'));

    Workspace := TNSWorkspace.Create;
    Workspace.openURL(URL);
end;

Instead of the NSWorkspace.openFile method, we need to use openURL. This expects a NSURL as a parameter, so we construct that and load it up with the initWithString method, again converting our String to a NSString along the way. Executing that code causes Safari (on my machine) to open my website.

Opening a new mail and setting the To, Subject and Body is much the same, just down to the string we pass into our NSURL:

procedure TForm2.Button2Click(Sender: TObject);
var
  URL : NSURL;
  Workspace : NSWorkspace;
begin
    URL := TNSURL.Create;
    URL.initWithString(NSSTR('mailto:fred@flintstones.com?subject=Hello&body=Hello%20Fred'));

    Workspace := TNSWorkspace.Create;
    Workspace.openURL(URL);
end;

You can download the sample project from my delphi-samples repository on github.

Cross-platform Special Folders in FireMonkey

There was a question on the ADUG list last week about how to retrieve “special folder” locations on OS X. By special folder, I mean locations like the user’s Home directory, the Documents directory, Temp directory, etc. I thought I’d write up the solution both because it’s probably something that more people will be wondering and also because it’s a nice little introduction to calling out to the OS X API.

If you want either the path to the Home or the Temp directory, this is ridiculously easy. The IOUtils unit already contains TPath.GetTempPath and TPath.GetHomePath, and these work on both Windows and OS X.

However, if you want another directory, such as the Documents directory, you need to do a little more work.

Read On…