How to add a badge to your app's dock icon...

I converted an objective-c example to ASOC that demonstrated how to add a badge to your dock icon. Thought it might be useful to someone. Here is the code:

on setDockBadgeString_(dockBadgeString)
	set appDockTile to current application's NSApp's dockTile()
	appDockTile's setBadgeLabel_(dockBadgeString)
end setDockBadgeString_

You simply call it like this from your code:

my setDockBadgeString_("9999")

Documentation says to pass a string. Didn’t try with an integer or float yet, could work. Also, if you want to remove the badge, call the routine again by passing “” or missing value.


P.S.: I’m madly in love with AppleScriptObjC!

Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)

That is awesome. I’ve always been curious how this was done.

Ain’t it! It’s awesome indeed… and so easy! Eventually I want to figure out how to add things like progress bars or other elements over the dock icon. I’ll post my successes on these forums of course…

Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)

I believe progress bars are done from pre-generated images of the icon with bars, so you’d just be temporarily setting the icon to another image.

I don’t know, sounds like it. I know this method of NSDockTile will do the trick:

- (void)setContentView:(NSView *)view

So I guess I’ll have to create an NSView in IB and then assign it with this method to the dock’s icon and tell it to display itself. I can’t do any testing yet, but for sure i’m gonna try it! Too exciting!

Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)


what do I need to get your example to work?
My AppleScript returns the error: “NSApp doesn’t understand the dockTile message.”

Delivery Boy

Model: MacBook Pro
AppleScript: 2.2.1
Browser: Safari 534.57.2
Operating System: Mac OS X (10.7)

Nothing special. Works just fine in any ASOC application. The trick it to call NSApp (which is just the short version of NSApplication). NSapp is your application, currently running. So you’re telling it to get its dock icon, then set a badge onto it. As far as I know, this is part of appkit, so there’s no need to import any framework or libraries.

Which version of Xcode are you working on?

So was I. Objective-C says just:

Anywhere in the code. Objective-C, I’m crazy about you :slight_smile:

Same here! Furiously in love with objective-c now! All thanks to applescriptobjc!

Soooo much better… And the speed! The speed! I still use ASOC when I need to talk to other apps, but recently I found how to use scripting bridge in objc. There’s a tool that converts the AppleScript dictionary of an app (sdef) and creates a .h file with it. Genius! So I think that in the not too far future it’ll be bye bye AppleScript… Our love was good, but I found better :slight_smile:

For me, ApplescriptObjC is the first stage of the Objective-C rocket. I would never have a takeoff without it.

I think this “bridge” is a brilliant idea: you come from Applescript, you enter the ASOC door to Cocoa, and you start learning the framework without noticing it, translating them into your loved Applescript syntax. And after a year, tired to coerce your variables, you find yourself programming in a language you would never dare to deal with.

The second stage is, of course, this forum. It is cleverly built, and you get fast answers from very skilled people. Have a look on other Objective-C sites and you will know what I’m talking about.

I suggest you hunt around a bit. Genius is definitely not the word most used to describe it…

I meant for the tool to convert the sdef file… I haven’t started playing around with it, i’ll start with simple tasks and medium-sized apps like Numbers before attacking bigger ones like InDesign.

But at least, that’s a start. I had nothing before, so I consider this great, been looking for awhile… :slight_smile:

Oh, and besides these forums, I find Stack Overflow to be of nice help, most questions I have have beeen answered in one form or another in the past (mostly because of my beginner level limitations :wink: ), and people are usually quite friendly… usually.

You might want to start here:


is it possible to have an example of the applescript?
If I execute, it dosn’t appear the badge number on the applescript editor icon.

What code did you try?

I tried this:

on setDockBadgeString_("9")
	set appDockTile to current application's NSApp's dockTile()
	appDockTile's setBadgeLabel_("9")
end setDockBadgeString_

Should it add a [9] badge on the Applescript Editor app icon?

I’m a noob in AppleScript…

Look at post #1, there is is two parts to this. There’s a method that you put somewhere in your code then you call this method with the second part.

Btw, this is applescriptobjc, not plain AppleScript. Thus your project needs to be done under Xcode or Shane’s app, AppleScript explorer if I remember correctly.

Is it Applescript Studio?
Basically I want to add the badge icon to the FileMaker Icon in the dock.
maybe this post can help you to help me? :stuck_out_tongue:

Nope, this is not AppleScript Studio. AppleScript Studio has been deprecated when OS X Snow Leopard came out a few years ago. It’s replacement is AppleScriptObjC, which is completely different, but a huge improvement.

And sorry, but you can’t add a badge to another app (as far as I know). This code is for your own app while it’s running, and will not work the way you want it to. Sorry.