MATTREAGAN
iOS & macOS engineer,
designer, game creator
Author: Matt Reagan
« Previous | Next »

App launchability via LSItemInfoRecord
This article was originally posted on my HumbleBee Blog in 2012, and republished here.

If you need to determine whether an .app is launchable on your user’s Mac, you’ll be disappointed to find out this is not a straightforward process. There are numerous reasons why an app would not be launchable: unsupported architecture (PPC vs Intel, 32 vs 64 bit), minimum OS requirements, etc.

Unfortunately there is nothing in NSWorkspace or any other Apple frameworks we can use to discover this at a glance. For developers working on file utilities, knowing this can be quite useful for things like prioritizing NSMetaDataQuery results.

The solution? You could dig through the app bundle, examine the Info.plist, and perhaps run `lipo -info` or `file` on the binary to check things like supported architecture and min. OS, and then compare that with information gleaned from Gestalt. That’s quite a painful process though for something that OS X should be able to do for us.

And in fact, OS X can do it for us. The simple solution uses LSItemInfoRecord to check an undocumented bit (0×00400000) which is flipped on if a binary cannot be opened.

Step 1. Get the LSItemInfoRecord for the target executable within your .app bundle (in Contents/MacOS):
LSItemInfoRecord info;
LSCopyItemInfoForURL((__bridge CFURLRef)[NSURL fileURLWithString: pathToAppExecutable], kLSRequestAllInfo, &info);

Step 2. Check the flag. If it’s on, the executable should be considered un-launchable:

if (info.flags & 0x00400000) { NSLog(@"Unlaunchable."); }

If you need a simple and efficient way of determining app launchability, this is a good place to look.

(Thanks to Seth Willits of Araelium Group for his help with this.)