Creating an Image/PDF from contents of a view?

So I have a view in my window with a number of images that can either be turned on or off (hidden or not). Of course, which images are shown will change depending on what the user picks.

Is there a way I can put what is displayed in this view as a JPG? I’ll settle for PDF, but JPG is much preferred. :wink: 50 bonus points for PNG! :smiley:

My only thought is to “print” the view, and save it as a PDF somewhere. However, I don’t want the user to see this happening. I want it to be a bit more professional than having them see a print dialog come and go. :slight_smile:



AppleScript Studio can’t do this directly.
It’s possible in Objective-C by subclassing the view and add a method like

[code]- (void)printViewToPNG

NSData *data;
NSBitmapImageRep *rep;
NSDictionary *properties;

rep = [self bitmapImageRepForCachingDisplayInRect:[self frame]];
[self cacheDisplayInRect:[self frame] toBitmapImageRep:rep];
data = [rep TIFFRepresentation];
properties = [NSDictionary  dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:NSImageInterlaced];
data = [rep representationUsingType:NSPNGFileType properties:properties];
[data writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/test.png"] atomically:YES];	


I have a question about the writeToFile:automatically: command. I know the function of the automattically part, but I’m wondering what’s the best? YES or NO? Is it a difference in speed? Safety, maybe?

Thank you,

Only safety (YES), a human being is not able to notice the speed difference

PS: Here a way without subclassing NSView.

Name the view in Interface Builder as “mainView” and connect it to awake From Nib
In Xcode create a new Objective-C class file (.m and .h), name it SKPrint

replace the contents of SKPrint.h with

[code]#import <Cocoa/Cocoa.h>

@interface SKPrint : NSObject {}

  • (void)printView:(NSView *)view inPath:(NSString *)path mode:(NSString *)mode;

replace the contents of SKPrint.m with

[code]#import “SKPrint.h”

@implementation SKPrint

  • (void)printView:(NSView *)view inPath:(NSString *)path mode:(NSString *)mode
    NSData *data;
    NSBitmapImageRep *rep;
    NSDictionary *properties;

    if ([mode isEqual:@“pdf”]) {
    data = [view dataWithPDFInsideRect:[view bounds]];
    [data writeToFile:path atomically:YES];
    else if ([mode isEqual:@“jpg”] || [mode isEqual:@“png”]) {
    rep = [view bitmapImageRepForCachingDisplayInRect:[view frame]];
    [view cacheDisplayInRect:[view frame] toBitmapImageRep:rep];
    data = [rep TIFFRepresentation];
    if ([mode isEqual:@“jpg”]) {
    properties = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:0.9], NSImageCompressionFactor, [NSNumber numberWithBool:YES], NSImageProgressive, nil];
    data = [rep representationUsingType:NSJPEGFileType properties:properties];
    else {
    properties = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:NSImageInterlaced];
    data = [rep representationUsingType:NSPNGFileType properties:properties];
    [data writeToFile:path atomically:YES];

in AppleScript add a property and some lines in the awake form nib handler

property mainView : null

on awake from nib theObject
    if name of theObject is "mainView" then
        set mainView to theObject
    end if
end awake from nib

then you can call the print method from AppleScript with

call method "printView:inPath:mode:" of class "SKPrint" with parameters {mainView, POSIXPath, "png"}


mainView is the reference to the View
POSIX path is the full POSIX path to the file (quotation is not required)
“png” is the mode (possible values: “pdf”, “jpg” and “png”

Superb - thanks a bunch!

As a bit of a side note, what’s a good resource for learning Cocoa? All of these commands seem so awesome, and I would love to be able to write full Cocoa Apps. I do know the basics, and some Obj-C, but I’m having a hard time finding a good resource for learning.

Any recommendations?


I studied with the book “Programming in Objective-C” by Stephen Kochan and the video tutorials at