Archive for the ‘OS X’ Category

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

Screen shot 2011-09-29 at 3.05.12 PM

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:

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:

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:

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…

OS X : Folder Comparison with FileMerge

I know a number of people in the Delphi and C++Builder world are taking their first steps into OS X. To be honest, I’m not that many steps in either. So I thought I’d start posting some tips as I strike problems and solve them, so hopefully it’ll help someone else.

Today’s problem (and solution) was the need to do a file and folder comparison on OS X. I’d had a few reports that this blog was not working properly on older versions of Firefox (Firefox 3.6) and wanted to see if I’d mucked up anything when changing the theme. I downloaded a fresh copy of the base theme and wanted to see all the differences between it and the version I’m using.

Read On…