Create a truly transparent Activity Monitor Dock icon

Aug 08, '07 07:30:00AM

Contributed by: dfe

After reading the prior hint on faking transparency for Activity Monitor's Dock icon, I decided that I could settle for no less than true transparency. Knowing that Cocoa's NSColor fully supports it, and acting on a hunch that Activity Monitor was probably archiving the chosen NSColor into the user defaults, I decided to view the com.apple.ActivityMonitor.plist file. Lo and behold, it does. So now the only thing between me and true transparency was an archived instance of NSColor.

The method is relatively simple. First you need a way to generate the archived NSColor.

I chose to write a very simple command-line Objective-C program (transparentcolor.m) as follows:

#import <AppKit/NSColor.h>
#import <Foundation/NSArchiver.h>
#import <Foundation/NSData.h>
#import <Foundation/NSAutoreleasePool.h>

#include <stdio.h>

int main()
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

	NSColor *transparentColor = [NSColor colorWithCalibratedRed:0.0 green:0.0 blue:0.0 alpha:0.0];
	NSData *theData = [NSArchiver archivedDataWithRootObject:transparentColor];
	size_t length = [theData length];
	unsigned char *bytes = malloc(length);
	[theData getBytes:bytes];
#if 1
	size_t i;
	for(i=0; i<length; ++i)
	{
		printf("%02x", bytes[i]);
	}
	printf("n");
#else
	fwrite(bytes, length, 1, stdout);
#endif

	[pool release];
	return 0;
}

You can change the #if 1 to if 0 if you want the raw data which you can pipe through hexdump or openssl base64, depending on how you want to use it. I wanted to use it with the defaults write command, so I chose to implement the hex dumping in the program. You can compile it quite simply with the following command: gcc -framework Cocoa transparentcolor.m -- you'll need Xcode installed, of course.

This will create an executable file a.out in the current directory. Make sure Activity Monitor is not running, and then run this command:
defaults write com.apple.ActivityMonitor CPUIdleColor -data `./a.out`
Take care to notice the back ticks, which cause the shell to use the output of the ./a.out command as the argument to the -data switch.

Now fire up Activity Monitor and enjoy your new transparency. Whatever you do, don't try to change the idle color, because this will immediately cause the alpha value to change to 1.0 instead of the fully transparent 0.0. I have not tried other combinations, but it should be possible to do a semi-transparent color if desired. A better program would have contained its own ability to change Activity Monitor's defaults, and would accept command-line options for the color values, but this one was just a quick hack. Hope you like it.

[robg adds: I tested this, and it works as described. Note that it will also affect the icon you see in the Command-Tab switcher, as Activity Monitor seems to accomplish its tricks by changing the actual application icon when you tell it to display a usage chart of some sort as its Dock icon.]

Comments (11)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20070808011626352