Quick Explanation of the Term "Application Bundle"

Follow-up to my previous post about receipt files. Again, this is Mac-centric.

WHAT IS AN APPLICATION BUNDLE?

"Application bundle" is Cocoa developer terminology for an application as it exists in the file system of your computer. The application consists of a number of files organized in a directory. The directory has special attributes and has a specific subdirectory structure.

WHERE IS THIS DIRECTORY? ALL I SEE IS THE APPLICATION FILE.

You only think you see a regular file. When you see an application icon in the Finder, such as the Safari icon, it looks like any other file icon. However, the Finder is telling at least two and possibly three lies about the application.

LIE #1: HIDING THE .app EXTENSION

On the file system, the name of the application is not actually "Safari" (for example) but "Safari.app", with a ".app" extension.

You can see all the .app extensions in Terminal:

ls /Applications

You can also see an application's filename in the Finder by selecting the application icon and doing a Get Info. You'll see the ".app" extension. Alternatively, if you go to Preferences > Advanced and turn on "Show all filename extensions", you'll see the .app extensions for all apps.

LIE #2: MAKING A DIRECTORY LOOK LIKE A FILE

An application is not a regular file, as it appears to be in the Finder, but a directory with special attributes and specific subdirectory structure. Such a directory is called a "bundle".

You can see the subdirectory structure in Terminal:

ls -l /Applications/Safari.app
ls -l /Applications/Safari.app/Contents

You can also see the directory structure in the Finder by right-clicking the application icon and selecting "Show Package Contents". You will see there is a subdirectory called "Contents", and within that there is a bunch of other stuff.

LIE #3: POSSIBLY A DIFFERENT NAME ALTOGETHER

Usually the Finder displays the name of the application as the directory name minus the ".app" extension. Thus, "Safari" instead of "Safari.app", "Contacts" instead of "Contacts.app", etc.

However, it is possible for applications to specify a different name to display in the Finder. The main reason for this is to localize the displayed application name. It is very rare to encounter this "lie" if English is your Mac's primary language.

Search for the term "CFBundleDisplayName" for the nuts and bolts of how developers get their apps to do this.

Quick Explanation of the Receipt File

I was explaining the MAS receipt file to a friend and decided to write a blog post instead of an email, on the principle Scott Hanselman has brilliantly advocated that we should conserve and amplify the limited number of keystrokes we have left in us.

I'm purely a Mac developer at the moment, but as far as I know this high-level explanation applies to iOS as well. No idea about watchOS.

WHAT IS THE RECEIPT?

The receipt is a file on your computer used for DRM of purchases from the App Store. There is a separate receipt for each app.

The receipt file contains:

  • Identifying information about the computer on which the app is installed — specifically, the MAC address of the device's primary network interface. Note that "MAC address", with the "MAC" in all caps, is a networking term. Nothing to do with "Mac" as in "Macintosh".

  • Identifying information about the app that the receipt is for — specifically, the app's bundle ID and version number.

  • Purchase history. Information about the app purchase and any subsequent in-app purchases.

WHERE DOES THE RECEIPT LIVE?

The receipt file is located inside the application bundle. On the Mac, it is at Contents/_MASReceipt/receipt. (If you don't know what the application bundle is, I've written another quickie blog post explaining it.)

WHO IS RESPONSIBLE FOR DRM?

The system is responsible for creating the receipt. By "system" I mean the OS in conjunction with the App Store. The system code-signs the receipt to prevent tampering. It's the job of Apple's developers to have gotten this right.

The application is responsible for validating the receipt. It's the job of the app developers to get this right.

WHEN DOES THE RECEIPT GET WRITTEN?

The receipt gets created on your computer when you download the app from the App Store. The receipt is then updated in the following situations:

  • If a newer version of the app becomes available on the App Store, the receipt is updated when you upgrade to the newer version.

  • If the app supports in-app purchases, the receipt is updated whenever you do an in-app purchase.

  • When you "Restore Purchases", the receipt is updated.

Here's what I mean by "Restore Purchases". Certain types of in-app purchases apply to all computers on which you have installed the app, not just the computer on which you made the purchase. A typical example would be an IAP that unlocks premium features. If you made such a purchase on a different computer, you may need to explicitly "restore" that purchase on the computer you are using. Apple's App Store rules require the app to provide a way for you to do this. Usually the app provides a button or menu item with a name like "Restore Purchases".

HOW DOES THE APPLICATION USE THE RECEIPT?

The application does two things with the receipt:

  • It validates the file contents to confirm the receipt hasn't been tampered with.

  • It checks in-app purchases to see what enhanced functionality the user has purchased. In-app purchases may include permanent "unlocking" of features, or they may be subscription-based, in which case the app should check the expiration dates of the subscriptions.

Both of these steps are performed by parsing the receipt file, which is a science in itself. There's an excellent article on objc.io that explains what is required:

You may wonder why Apple hasn’t provided a simple API to validate the receipt. For the sake of demonstration, imagine that such a method exists (for example, [[NSBundle mainBundle] validateReceipt]). An attacker would simply look for this selector inside the binary and patch the code to skip the call. Since every developer would use the same validation method, hacking would be too easy.

Instead, Apple made the choice to use standard cryptography and encoding techniques, and to provide some help — in the form of documentation and WWDC sessions — for implementing your own receipt validation code. However, this is not an easy process, and it requires a good understanding of cryptography and of a variety of secure coding techniques.

Of course, there are several off-the-shelf implementations available (for example, on GitHub), but they are often just reference implementations and suffer from the same problem outlined above if everybody uses them: it becomes very easy for attackers to crack the validation code. So it’s important to develop a solution that is unique and secure enough to resist common attacks.

MORE LINKS

Some Ways I Use Keyboard Maestro

Today, thanks to Michael Tsai, I found out there's a major new release of Keyboard Maestro. I use Keyboard Maestro all day long, and look forward to checking out the upgrade when I have time.

Here are some ways Keyboard Maestro makes my day go much more smoothly:

  • I use the palette feature to navigate to frequently used apps and documents in exactly two keystrokes.

  • I have a "Preferred Window Frame" macro that sets the active window's size and position. My preferred frame varies from app to app, and the macro does the right thing depending on which app is active. Just one keystroke to remember. The macro invokes keyboard shortcuts I've configured in Divvy, but I could also have entered window coordinates directly into Keyboard Maestro.

  • I map ^P/^N to UpArrow/DownArrow so I can use those emacs keys to go up and down in lists, menus, and other places outside of text editing. The ^P/^N keys already work in many apps, but not all, and not in all places within the same app. By setting up this global mapping, I don't have to remember where they work and where they don't.

  • You can create text expansion macros. I have macros for inserting date and time, for a few phrases I type on a regular basis, and for emoji characters. I was happy using Keyboard Maestro for all this but recently moved my text expansion macros to aText, a very nice app at a very reasonable price. My reasons for switching were minor and arguably not worth the time when I had a perfectly good solution. I wouldn't and perhaps shouldn't have bothered, except I am such an obsessive yak shaver.

  • I sometimes use Keyboard Maestro instead of System Preferences to provide alternate menu shortcuts. I do this because Keyboard Maestro syncs my macro definitions on Dropbox, which means that when I make a change, all my Macs automatically get it.

  • I open Xcode's Recent Files menu with one keystroke (I use ^R) instead of three (^1, DownArrow, RightArrow).

  • I have a scratch macro named "Pipe Text" that replaces the currently selected text by piping it through a shell script and pasting the result. The macro has an "Execute Shell Script" action that I edit to do what I have in mind, typically a grep.

  • I keep a personal log in the form of daily text files. I have a macro that opens today's log file in BBEdit, after creating the file if necessary. I use this throughout the day whenever I think of something to add to the file.

I'm happy to share how I do any of the above.

I'm Fine With the Accents on "Fresh Off the Boat"

Yesterday I watched the first two episodes of Fresh Off the Boat, a sitcom loosely based on the childhood of Eddie Huang. It's the first network show in 20 years starring an Asian-American family. The previous one, Margaret Cho's All-American Girl, didn't do too well, so anticipation and expectations were high for Fresh Off the Boat.

Last year when the first teasers came out for the show, viewers' reactions included some concern about the accents of the parents, played by Constance Wu and Randall Park. To me, the accents did indeed sound off-key, but based on everything else I saw, I reserved judgment. I figured maybe I wasn't familiar enough with Taiwanese accents, since I'm more used to the Hong Kong accents in my own family.

Mainly I wanted to take the attitude that I take with Apple product announcements. Whatever optimism or doubts I may have, I always reserve final judgment until I've had hands-on experience. I learned my lesson about that in 2007, when Apple shipped the first iPhone. I was excited about it but thought I could hold off on getting one. I figured I'd wait for other people to find the bugs and for Apple to work out any manufacturing glitches. But then a friend let me play with his iPhone, and I was immediately hooked. The hands-on experience far exceeded my expectations. Minutes later, my friend walked me to the Apple Store and I bought my own iPhone.

This week ABC "shipped" the first two episodes of Fresh Off the Boat and I finally got to go "hands-on". And you know what? I loved the show so much I watched both episodes twice. The show is honest about race — there's a scene in the pilot episode that I think people will be talking about for a long time — but at heart it's a funny show with some poignant moments and some moments of dark humor (though not too dark; remember, this is ABC, not HBO). The narration by the real Eddie Huang adds great depth to the flavor of the show.

I found that the more I watched, the less I cared about the accents. They might be pitch-perfect, or they might be as questionable as James Doohan's impression of a Scot. I'm still not sure, but I don't care any more because I like the characters, and I like them just as they are. I doubt most of non-Asian America will care either.

Put it this way: I find Jackie Chan's genuine accent way more distracting than I ever found Wu's and Park's simulated ones, and I still love Jackie Chan movies.

On Tim Cook being "the first"

Alanna Petroff, writing for CNN.com:

It's a landmark moment for both the gay community and the business world. Tim Cook is now the first and only openly gay chief executive in the Fortune 500.

Of course, many lesbian, gay, bisexual and transgendered employees still struggle with discrimination at work. The executive suite also remains extremely closeted, but there are a few high-ranking openly gay businesspeople.

Not to detract from their accomplishments or their struggles, but none of these examples come close to Tim Cook in terms of power or visibility. It's like pointing out the US had prominent black politicians before Obama; not to take away from any of them, but there is a clear difference. Cook is the first openly gay CEO of a Fortune 500 company, and it's a company with a track record the other 499 would kill to have.

I remember when "Apple is so gay" was a common juvenile taunt. For all I know, it still is. Regardless, times have changed, and we should all be comfortable responding to that taunt with "You say that like it's a bad thing."