I recently did some extensive reverse-engineering of Quick Look to investigate the API, mainly to track down how to get the cool zooming effect when Quick-Looking a file in Finder. It turns out it’s pretty simple to use Quick Look from your own code.
First you’ll need to load the framework found at
if( ) NSLog(@"Quick Look loaded!");
Loading the framework with NSBundle is preferable to linking directly for 2 reasons:
- If your app is to support Tiger then we can try to load the framework and only use Quick Look if it succeeds
- Since the framework is in PrivateFrameworks there is the possibility that Apple may move it – e.g. if it becomes public
All your interaction with the QL system will be through the QLPreviewPanel singleton class, accessed using
[QLPreviewPanel sharedPreviewPanel] – you’ll need to include this header so that you can use the methods without warnings. I use a define like this:
QLPreviewPanel to be used in code.
Quick Look works with URLs (provided as
NSURL instances) – to tell it which items to display you pass it an
NSURLs like so:
The other 2 parameters should be self explanatory.
Then you need to tell the Quick Look panel to display. The simplest method is to have it fade in by using
And that’s it! To change the current set of items (e.g. when the user changes the selection) just call
setURLs:current:preservingDisplayState: again with the new URLs. Check
[[QLPreviewPanel sharedPreviewPanel] isOpen] to determine whether to use
makeKeyAndOrderFrontWithEffect: or not. Call
closeWithEffect: to close the panel.
To get the zoom effect we need a little more work. First we need to set up a delegate, like this:
This delegate must implement
- (NSRect)previewPanel:(NSPanel*)panel frameForURL:(NSURL*)URL, returning the rect which the item represented by
URL originates from.
With a delegate set, simply change your
makeKeyAndOrderFrontWithEffect: call to use
2 and Quick Look will automatically request the frame when required.
I’ve put together a simple project which demonstrates using Quick Look, you can download the source code here.
13 Responses to “Quick Look APIs”