メニューのキャプチャ画像を作る。前回までのプログラムに CGWindowListCreateImage( )を加えてキャプチャ画像を作る。
これも以前の検証のコードを流用するとすぐできる。
AppController.m
w = CFArrayGetValueAtIndex(list, i);
CFNumberGetValue(CFDictionaryGetValue(w, kCGWindowLayer),
kCFNumberIntType, &layer);
CFNumberGetValue(CFDictionaryGetValue(w, kCGWindowNumber),
kCGWindowIDCFNumberType, &window_id);
if (layer == kCGPopUpMenuWindowLevel) {
NSString* path = [NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* filename = [path stringByAppendingPathComponent:@"cocoa_days_menu.png"];
CGImageRef cgimage = CGWindowListCreateImage(CGRectZero, kCGWindowListOptionIncludingWindow, window_id, kCGWindowImageDefault);
NSBitmapImageRep *bitmap_rep = [[[NSBitmapImageRep alloc] initWithCGImage:cgimage] autorelease];
NSImage *image = [[[NSImage alloc] init] autorelease];
[image addRepresentation:bitmap_rep];
NSData* data = [bitmap_rep representationUsingType:NSPNGFileType
properties:[NSDictionary dictionary]];
[data writeToFile:filename atomically:YES];
}
}
実行後、タイマーを起動させ、メニューを開いて少し待つと cocoa_days_menu.png がデスクトップに作成される。

こんな感じ。

メニューのキャプチャも容易にできることがわかった。
ソース:MenuCapture-02.zip