Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac Quick Look Preview in an NSView or NSImage?

I am looking for a way (public or otherwise) to get an NSView, NSImage, CGImageRef, etc that shows the QuickLook preview for a file. Essentially the equivalent of QLThumbnailImageCreate() but for the preview.

The public APIs I can find do not support this. They allow the creation of a thumbnail image or a QLPreviewPanel. The panel does in fact display the quick look preview, but I cannot get access to the preview's appearance to embed it in other views, nor can I display multiple previews at once.

For background, I am writing an app where users can embed links to other files that should be displayed inline, kind of like an <img> tag in HTML. For images like JPGs and PDFs it's easy to figure out what to display. I thought that for other formats I would use Quick Look to generate a nice visual representation of the file's contents. This way the set of formats supported by my application would be easily extensible (just download new Quick Look generators).

like image 581
Niko Matsakis Avatar asked Jan 30 '10 21:01

Niko Matsakis


People also ask

What is the shortcut to Quick Look on a Mac?

Quicklook – Spacebar (or Command + Y)

How do I quickly see files on a Mac?

Just press Command-O in your app, navigate to the file, select it, and click Open or press Return. If you want to view a file when you're in an Open dialog, you can select and it press the space bar to view it in Quick Look.

How do you zoom in on Quick Look on a Mac?

Click on the file you'd like to use Quick Look on to highlight it and hit the space bar to trigger Quick Look. Once the Quick Look Window opens, hold down the alt - option key on your Mac's keyboard. While holding down the alt - option key scroll around or zoom in and out.


1 Answers

I looked into this extensively once a while back and was not able to find an easy way to do it. Depending on the type of file, QuickLook generates different kinds of output. For example, for iWork files, the generator makes HTML that it displays in a WebView. For other types it returns different types of data.

I never ended up using the code, but here's some code I dug up and some private APIs that might be handy:

id QLPreviewCreate(CFAllocatorRef allocator, CFURLRef url,  CFDictionaryRef options);
id QLPreviewCopyBitmapImage(id preview);
id QLPreviewCopyData(id preview);
NSString* QLPreviewGetPreviewType(id preview);
id QLPreviewCopyProperties(id preview);

- (NSData *)getDataForFile:(NSString *)path
{

    NSURL *fileURL = [NSURL fileURLWithPath:path];

    id preview = QLPreviewCreate(kCFAllocatorDefault, fileURL, 0);

    if (preview)
    {
        NSString* previewType = QLPreviewGetPreviewType(preview);

        if ([previewType isEqualToString:@"public.webcontent"])
        {
            // this preview is HTML data
            return QLPreviewCopyData(preview);
        }
        else
        {
           NSLog(@"this type is: %@", previewType);
           // do something else
        }

    }

    return nil;
}
like image 199
Ken Aspeslagh Avatar answered Oct 21 '22 09:10

Ken Aspeslagh