How to Badge an App’s Icon in the Dock

An app icon badged with 'Badge'

A recent conversation I had with Jeff Johnson (of ClickToFlash fame, @lapcatsoftware on twitter) prompted me to look into how badging an app’s icon in macOS’ Dock works.

There were a couple of questions that needed answering:

  • Does badging happen automatically when sending an NSUserNotification?
  • If not, how do I apply a badge to my app’s icon in the first place?
  • How do I retrieve and respect the user’s Notification settings for the app in System Preferences?

Automatic Badging by Sending a NSUserNotification?

NSUserNotification Notification

Nope, automatic badging doesn’t happen. Even though System Preferences -> Notifications suggests that badges and notifications belong together, in code, they’re separated.
That means you have to add the badge to your app’s icon yourself. Also, you need to keep track of whatever number you want to display with the badge and update or clear it at appropriate times.

For example, Mail.app uses the badge to display the amount of unread eMails you have. The badge doesn’t clear when you activate Mail and only decreases when you mark mails as read.
Transmission, on the other hand, informs you about how many downloads have finished since you last had the app active. Once you activate the app again, the badge gets cleared and re-starts from zero.

How to Badge the App’s Icon?

Badging is straightforward:

NSApp.dockTile.badgeLabel = @"Badge";

Please note that putting text into the badge is not a good idea – I did it here just for fun, but text in the badge is very limited and if you have a longer string, what you’ll end up with is something like “A….z”.
It’s best to stick with numbers.

Respecting System Preferences’ Notifications Settings?

If you only use badges (without notifications), you’ll notice your app is missing from System Preferences’ Notifications preference pane.
That can pose a problem, because now you either have to create your own user-setting for badges or the user will have no way of turning them off.
The trick, then, lies with NSUserNotification. Not in the API itself, but in two crucial steps:

  1. Code sign your app.
  2. Add this key-value pair to your Info.plist: NSUserNotificationAlertStyle with a string value of either banner (recommended by Apple) or alert.
    Supposedly, there’s another value, none, but that hasn’t worked for me yet – the app won’t appear in the Notifications preference pane.

Having the key-value pair in your Info.plist has no downside if you don’t use NSUserNotifications. There’s only the upside of having the user be able to disable your app’s badges if they like.

Now that the user can change the setting for your app’s badges, how do you read it out to see if you should badge or not? It’s easier than you think: you don’t.
Just like the system doesn’t show your NSUserNotifications if the user has disabled them for your app, the Dock simply doesn’t display your badge if the user has disabled it in System Preferences.

All you have to do is keep track of the number that should be displayed in the badge, and that you update or clear it at appropriate times.
For example, you might not want to have the badge visible when the user quits your app, so you could set -badgeLabel to nil in -applicationWillTerminate:.

By the way, if you ever need to reset System Preferences – Notifications for your app (or all apps), there’s a nice how-to on stackoverflow.

Happy badging :)

Update: Jeff Johnson follows up with some more tips and tricks about NSUserNotification in this blog post.

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 :)

Advertisements

Mac Developer Tip: How to Simulate Memory Pressure

In iOS Simulator, you can, via a menu item, quickly simulate memory pressure for the simulated device to see if your app behaves correctly under low memory conditions (releasing caches, cleaning up resources, and so on).

Recently, I had the need for it on the Mac (where such a menu item is not available), while working on an XPC service for Yoink, to see if it terminated properly under certain conditions.

Meet the memory_pressure tool

OS X comes with the Terminal, and with the Terminal come some awesome tools. Among them is memory_pressure. Its man page says it all:

Apply real or simulate memory pressure on the system.

Usage of memory_tool

sudo memory_pressure -S -l critical

What this does is simulate (-S) a memory pressure of a critical level (-l critical).
sudo is needed, otherwise it will probably fail with an “Operation not permitted” error.

memory_tool’s options

-l:   Two levels of pressure are supported: ‘warn’ and ‘critical’.
-p:  For real pressure, this lets you define how much memory should remain free, in percent of total memory.
-S:  Simulate memory pressure, don’t really allocate memory.
-s:   If applying real pressure, this is the duration to wait before allocating or freeing memory.
       If it’s simulated pressure, the system will be maintained at an artificial memory level for this duration.

Introducing Glimpses for Mac – turn your photos and music into stunning Still Motion videos

Glimpses App Icon

Glimpses 2.0 for Mac

After a lot of hard work, I’m very happy to finally be able to announce the immediate availability of Glimpses 2.0 (formerly known as Briefly) on the Mac App Store.
The app has undergone some major changes, and I wanted that to be reflected in the app- name and icon (wonderful work by the very talented Alexander Käßner).

What Does Glimpses Do?

Still Motion videos typically show photos for not much longer than the fraction of a second.
Glimpses is designed to let you create these videos with just a few clicks, with stunning results.

The app produces high-quality video with a resolution of up to 4K and lets you add a soundtrack to make your video really sing.

Glimpses with soundtrack selection

What’s New?

– Soundtracks: Glimpses 2.0 makes it much easier to add multiple soundtracks. It also lets you select the parts of the soundtracks you really like to make your Still Motion video really shine.
– Face Detection: Glimpses analyses your photos for faces and places the photos in your video accordingly for much nicer and more personal results.
– 4K Resolution: Going beyond 1080p, Glimpses now renders your videos with a resolution of up to 4K.
– Projects: You can now save your projects. Up until now, the app only remembered the last used photos and soundtrack.

… as well as many more, minor improvements (like improved support for panorama photos) and fixes.

Details

Soundtracks

Soundtracks are a vital part of a Still Motion video. Glimpses makes it so much easier to add and edit one or several soundtracks.
With its new selection feature, you can select just the part of a song you like best that should be used for your video.

If a soundtrack is too short, you can either loop it or add another one. The order of appearance can be changed at any time.
Should a soundtrack be longer than your video, Glimpses will automatically fade it out at the end without you having to do anything.

Face Detection

In earlier versions, Glimpses used to center your photos within the video’s frame.
Glimpses 2.0 is much smarter and analyses your photos for faces and if detected, moves them into the video’s frame, resulting in much nicer and more personal videos.

Briefly without and with face detectionA still from a video created with Briefly 1.5.2 (top) compared to the same video created with Glimpses 2.0 (below)

Resolutions and Video Settings

With cameras getting better and better, it was only logical to include new resolutions in Glimpses 2.0 – the app now goes beyond 1080p and adds support for 2K and 4K resolutions. You can set the resolution manually or let Glimpses decide, based on the smallest photo.

Glimpses Video SettingsGlimpses Video Settings

A photo’s duration can be set from 0.1 seconds up to 4 seconds in 0.1-second-increments, an improvement that allows for more fine-grained control over the resulting video. Glimpses can also automatically do it for you, based on the duration of the soundtracks you selected.

Pricing and Availability

Glimpses is available exclusively on the Mac App Store (click) for the introductory price of $9.99 for a limited time (about 60% off), after which the price will revert to $24.99.
A free, 15-day trial can be downloaded from the website (click) (direct download). If you’re from the media and would like to get more info about the app, please download the press kit (click), thank you.

Glimpses Website: http://eternalstorms.at/glimpses
Glimpses on the Mac App Store: https://itunes.apple.com/app/glimpses/id637565340?mt=12
Glimpses Demo Download: http://eternalstorms.at/glimpses/Glimpses.zip
Glimpses on Twitter:https://twitter.com/GlimpsesApp
Glimpses Press Kit: http://eternalstorms.at/press/Glimpses-2-Press-Kit.zip
Glimpses on Product Hunt: https://www.producthunt.com/posts/glimpses

Thank you for your time, I hope you enjoy this great new version of Glimpses :)

If you have any feedback or questions, please be sure to let me know via twitter (click) or eMail (click). I’m looking forward to hearing from you.