Editing Images - Can It Be Done?

Thanks to the great information here, I have figured out how to load an image and place it onto my app’s Dock icon. But is it possible in AppleScript to alter that image before I place it on the Dock icon? Specifically, I would like to draw some text (a number) onto the image to convey some information, sort of like what Mail.app and iCal do. Thanks for any help.

Forget this post…read on… :wink:


Here it is…

  1. Create a new obj-c class file (File > New File… >>> Objective C class). Name it “IconController”. Choose to create only the “IconController.m” file and not the “.h” file. The code is set up to handle two different text positioning options. The first option (commented out by default) sets the position of the text to a static, left-justified coordinate. By default, it is set to insert the text 2 pixels from both the bottom and left edges of the icon. The second option, centers the text around coordinates you provide. For example, if you have a round badge in your image whose center is at 45,75 (left,bottom) you’d insert these numbers in the offsetX&Y variables. By default it is set to insert the text centered on the icon. If you fish around in the code a bit, you’ll also find where to set the size, color, and font of the text. Messing with changing that is up to you. Insert the code below into the “IconController.m” class file.
//  IconController.m
//  by jobu

#import "Cocoa/Cocoa.h"
#import "AppKit/AppKit.h"

@interface IconController : NSObject
+(void)setAppIconText:(NSImage *)appImage withString:(NSString *)theString;

@implementation IconController

+(void)setAppIconText:(NSImage *)theIcon withString:(NSString *)theString
	if (theString) {
		NSDictionary *attributes = [[NSDictionary alloc] initWithObjectsAndKeys:
			[NSFont fontWithName:@"Helvetica-Bold" size:26], NSFontAttributeName,
			[NSColor whiteColor], NSForegroundColorAttributeName, nil];
		NSImage *theIconBuffer = [theIcon copy];
		NSSize theStringSize = [theString sizeWithAttributes:attributes];
		[theIconBuffer lockFocus];
		// Insert text left-justified to a static point (left,bottom)
		//[theString drawAtPoint:NSMakePoint(2, 2) withAttributes:attributes];
		// Insert text centered around predefided offsets (X=dist. from left,Y=dist. from bottom)
		int offsetX = 64;
		int offsetY = 64;
		[theString drawAtPoint:NSMakePoint(offsetX - theStringSize.width / 2.0f, offsetY - theStringSize.height / 2.0f) withAttributes:attributes];
		[theIconBuffer unlockFocus];
		[NSApp setApplicationIconImage:theIconBuffer];
	else [NSApp setApplicationIconImage:theIcon];
  1. Decide how many icons you’ll display in the dock. For each one, create a script property variable to retain a permanent reference to the icon file, and place it in the top of your script. For example, if you have your main icon, and then a version of that icon with a “badge” or area that the icon’s text will hover over, you’ll need two properties like…
property baseImage : ""
  1. Initialize your images. I did this in the ‘launched’ handler of the app (file’s owner). This will place a permanent reference to the icon in the property, so you won’t have to deal with loading or unloading the icon(s) in the future. If you’re using ‘.icns’ files as I did, leave off the ‘.icns’ extension. For other image types, you may or may not have to include the extension.
on launched theObject
	set baseImage to load image "MyIcon"
end launched
  1. Call the obj-c. Anywhere in your script, you are now free to call the obj-c class’ code to change the icon to include some text. If you send a call to the class without a string, it will just display the image you send it. This is useful for returning your icon to it’s default state. Make sure you send an image in the first parameter even if you aren’t sending a string, and value that can be interpreted as a string as ‘theString’.
set theString to "jobu"
call method "setAppIconText:withString:" of class "IconController" with parameters {baseImage, theString}

Have fun… :smiley:

Thanks! I’ll take a look at this soon.

Success!!! I used your code to create a program called Humility, which will soon be posted here:


(Link updated to version 1.03.)

I think this version finally works!!! I also finally included a readme giving credit to the people here who helped write the code. Sorry for not doing that sooner.

If I try to build it it gets 172 errors, each is a stray 240 or 302. What does this mean/ how do i fix it?