Advertisement

Dear Aunt TUAW: Help me expand Mail's tracking-fu

Dear Aunt TUAW,

I really like Mail 5's shipment tracking feature, but it only seems to look up shipments in USPS. What if I have a package arriving from some other service? Is there a way to change what site Mail looks up for tracking info?

Your loving nephew,

Bob V.

Dear Bob,

Those features that automatically connect to shipment look-up are called data detectors. OS X Mail has built them into Lion, allowing you to track packages sent by UPS, FedEx, and the US Postal Service.

They work like this. When Mail detects shipping information, hovering your cursor over a tracking number produces a dotted outline and a small button with a downward arrow. Click that arrow and choose Track Shipment from the contextual pop-up. Mail opens a QuickLook window populated with your shipping information.

You can enable and disable data detectors by issuing a command-line defaults request, e.g. defaults write com.apple.mail DisableDataDetectors YES, or NO. What you can't do is change what detectors there are, or add to them.

As far as Auntie can tell, data detectors seem to be built into apps via the underlying programming system rather than specified through defaults. Data detectors are actually quite complicated things. They are based on regular expressions that match the characteristics of the item they are detecting.

Apple provides built-in detectors for tracking numbers, phone numbers, dates, addresses and for URLs. A simple web search for regular expression and, say, phone number shows how deeply complex the problems are -- and how little suited they are for end-user expansion.

GummyDev offers an interactive regex checker. Its relatively simple regex for UPS is /\b(1Z ?[0-9A-Z]{3} ?[0-9A-Z]{3} ?[0-9A-Z]{2} ?[0-9A-Z]{4} ?[0-9A-Z]{3} ?[0-9A-Z]|[\dT]\d\d\d ?\d\d\d\d ?\d\d\d)\b/i. If UPS is simple, FedEx is less so. FedEx apparently keeps updating their number system based on their internal corporate structure and the number of packages delivered.

In Apple's SDK, the NSDataDetector class derives from the NSRegularExpression class and basically works the same way. Class instances can enumerate through matches found in a string and applies property updates based on those matches.

Here's a quick example of a custom data detector that matches against any occurrence of the string "auntie", marking it in orange.

So to sum things up, here's what Auntie has believes is likely going on. (Although if she got any of this wrong, do correct her -- as usual -- in the comments.)

  • As an end-user you can enable or disable all data detectors for mail.

  • You cannot add new data detectors, say, for the Canadian postal service.

  • Developers can build detectors into their own apps as needed, but these detectors and the regular expressions that power them are not shared between apps.

  • Regular expressions can be really, really ugly.

Here's hoping that helps.

Hugs and kissies,

Auntie T.