cocoa

Working on Transloader 3, I wanted to re-implement the Finder’s Open With… contextual menu:

The Finder's Open With contextual menu

Getting the apps in there turned out to be the easy part. What took some effort was the “App Store…” menu item, as I wanted to precisely replicate its functionality.

Transloader 3's Open With contextual menu

Right-click a file in Finder, select Open With -> App Store…, and it will launch the Mac App Store with a UTI search, for example: uti:public.zip-archive.
This will give you a list of all apps available in the Mac App Store that can handle that file type. Neat!

The ‘macappstores’ URL scheme

The Mac App Store app luckily offers a URL scheme:

  • macappstores://
    Launches the Mac App Store
  • macappstores://showUpdatesPage
    Launches the Mac App Store and takes you directly to your Updates page
  • macappstores://showPurchasesPage
    Launches the Mac App Store and takes you directly to your Purchased page
  • macappstores://itunes.apple.com/app/idYOURAPPID
    Launches the Mac App Store and takes you directly to the product page, identified by the product ID

That’s very practical, but not what I was looking for. I was in need of a way to start a search the way Finder does.

In order to find out the URL Finder uses, I wrote a quick throwaway-app that would overtake the Mac App Store’s URL schemes (using LSSetDefaultHandlerForURLScheme) and print out the URL that was opened.
Alas – no dice. Apparently, Finder uses Apple Events or some other magic that “can not be used in a third-party sandboxed app anyway”™ to do its bidding.

After googling the issue, I found a URL that supposedly worked: macappstores://ax.search.itunes.apple.com/WebObjects/MZSearch.woa/wa/search?q=searchterm, but just to pour more salt into an already wide-open wound, it only worked on pre-10.9 systems:

The search URL in 10.9-and-beyond systems

Another dead end. Or was it?
I guess I should have experimented with that URL a little, because Jan Vitturi (@jan4843 on twitter) had the answer: just remove “ax.” from the URL, and it works (on both pre-10.9 and post-10.9 systems)!

Using this URL:
macappstores://search.itunes.apple.com/WebObjects/MZSearch.woa/wa/search?q=searchterm
I was able to get a search going on the Mac App Store, right from within my app.

Sadly, there’s a caveat.
I can’t do a UTI search this way. When I pass (even a percent-escaped) search term along the lines of ‘uti:public.zip-archive’, the Mac App Store tells me there are no results. Reloading that very same page then does show the results – weird and annoying, but nothing I was able to work around.
Using extension:zip seemed to work a little better, but still didn’t return all results a reload would.

Jan Vitturi to the rescue again – the URL’s a little different for UTI or extension searches:
macappstores://search.itunes.apple.com/WebObjects/MZSearch.woa/wa/docTypeLookup?uti=youruti

Alternatively, instead of uti=…, you can use extension=… to search by file path extensions.

My sincere thanks go to Jan Vitturi!

 

Eternal Storms Software Logo

– – – Do you enjoy my blog and/or my software? – – –
Stay up-to-date on all things Eternal Storms Software and join my low-frequency newsletter (one mail a month at most).
Thank you 🙂

Read more

You’re working on your Share extension with support for MacBook Pro’s Touch Bar, but it’s empty and doesn’t appear when your extension is loaded?

NSTouchBar Empty

The solution is simple:

Solution for showing NSTouchBar from within a Share extension

Call [self.view.window makeFirstResponder:self.view]; and it will push your glorious NSTouchBar onto the stack:

NSTouchBar working in a Share extension

Hope it helps – it would have saved me 20 minutes if I’d known 😉

Eternal Storms Software Logo

– – – Do you enjoy my blog and/or my software? – – –
Stay up-to-date on all things Eternal Storms Software and join my low-frequency newsletter (one mail a month at most).
Thank you 🙂

Read more

In an effort to show preferences for configuring Force Touch in my apps (in particular, Yoink) only when a Force Touch device is actually available, I had to find a way to figure out how to detect Force Touch devices.

If this was iOS, I’d be done by now

On iOS, Apple provides a simple API for this:

UIForceTouchCapabilityUnavailable and UIForceTouchCapabilityAvailable

which you can check by calling UIView’s – (UIForceTouchCapability)traitCollection;. Lo and behold, a simple API like this is sadly not available on OS X. On the Mac, you have to do it yourself.

IOKit is where it’s at

Using IOKit, you can sort of set up a set of properties you’re looking for in a device and see if it returns anything. This does not only include an external or internal keyboard, mouse or trackpad, but also graphic cards, for example. To find out what the right properties are, I downloaded Apple’s Hardware IO Tools for Xcode 7.1 from their developer downloads site (Apple Developer account required) and launched the app IORegistryExplorer.

Digging down the IO Registry

I do have a Magic Trackpad 2 “attached” to my Mac via Bluetooth, so I tried searching for the term “Trackpad”, and sure enough, I saw my internal and external ones:

IORegistry TrackpadsThe Trackpads attached to my Mac, either via Bluetooth or internally.

Having found the Magic Trackpad 2, the next step is to see what properties it offers and if they are unique to the class of the device:

Trackpad PropertiesJackpot!

Sure enough, there it is – ForceSupported: True. I could not find such a key in the internal trackpad’s properties, hinting that it might be exclusive to devices that do support Force Touch. There’s also an entry for “Manufacturer”, which is “Apple Inc.”. Perfect.

Looking for Devices with IOKit

Now all I have to do is filtering devices by the Manufacturer – “Apple Inc.” -, iterate over the resulting devices, and filter out devices that have a DefaultMultitouchProperties key, containing a ForceSupported key with a value of true. If such a device is found, it means a device with Force Touch capabilities is available.

Code Listing #1

In this method, I create a dictionary mDict that is used to find matching devices. In this case, I’m looking for devices with the Manufacturer set to “Apple Inc.”. I query for possible devices using IOServiceGetMatchingServices. I can then iterate over the returned io_iterator_t iterator and recursively over the children in the core of all of this: – (BOOL)_containsForceTouchDevice:io_iterator_t)iterator;.

Code Listing #2

Here, we iterate recursively over iterator’s objects, checking for the DefaultMultitouchProperties key and, subsequently the ForceSupported key (and value).

Testing it with other Trackpads

The code you see above is final. However, in a previous version, all I could use to test it with was my Magic Trackpad 2 – an internal Force Touch Trackpad (the likes of which the new MacBooks and MacBook Pros feature) was not available to me directly. So I sent the first draft of the code to my friend (and fellow developer) Maurice Kelly (@mauricerkelly on twitter) who was kind enough to volunteer as my “test subject”; and – of course – it didn’t work. Turns out I shouldn’t assume the vendorID to be the same (which I used in the first draft just like the Manufacturer to filter the possible results). After leaving it out, it worked fine over all currently available Force Touch Trackpads.

Hot (Un-)Plugging

Sometimes you might want to get notified when devices are plugged in to or unplugged from the Mac. In Yoink, I’d like to display preferences specific to Force Touch if according hardware is available, but not show them if there isn’t any hardware connected that supports it. Also, as a nice touch, I’d like to hide the preferences if according hardware is disconnected and show it again when it is connected. We can accomplish this like that:

Code Listing #3

Sandbox

The code works just as well in the Sandbox environment, these entitlements have to be set, though, for the notifications to work:

Sandbox Entitlements

com.apple.security.device.usb and com.apple.security.device.bluetooth

Future Proof?

I’m not sure this code is future proof. The basic APIs will stay around, sure, but it all depends on the keys DefaultMultitouchProperties, ForceSupported and their according values which are not defined in the IOKit header files, which I unsuccessfully searched for constants pertaining to force/pressure. So, depending on this, the code might break with future versions of the Force Touch Trackpad and I’ll have to keep testing it as Apple releases new hardware. Nevertheless, I’m quite happy with the code and it works very well.

Getting the Source Code

I’ve uploaded a sample project to my server, you can download it here. The category on NSApplication in which this is available can be downloaded from Github. The example app is pretty simple:

Force Touch Device Detection Example App Screenshot

At launch, it asks if a Force Touch capable device is available and displays an according message. If subsequently the availability status changes, the message will be updated accordingly.

If you’d like to get in touch, you can mail me or write me on twitter. I’m looking forward to hearing from you 🙂

Read more