Posts RSS Comments RSS 32 Posts and 983 Comments till now

Quick Look APIs

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 /System/Library/PrivateFrameworks/QuickLookUI.framework

if([[NSBundle bundleWithPath:@"/System/…/QuickLookUI.framework"] load])
   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:

#define QLPreviewPanel NSClassFromString(@"QLPreviewPanel")

To allow 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 NSArray of NSURLs like so:

[[QLPreviewPanel sharedPreviewPanel] setURLs:URLs currentIndex:0 preservingDisplayState:YES]

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

[[QLPreviewPanel sharedPreviewPanel] makeKeyAndOrderFrontWithEffect:1]

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:

[[[QLPreviewPanel sharedPreviewPanel] windowController] setDelegate:theDelegate]

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.

17 Responses to “Quick Look APIs”

  1. on 11 Dec 2007 at 2:59 pmXavier C.

    Great job!!

    I can’t code with Cocoa (though I should learn), but could you provide (with not so much effort, supposingly) another code in which the files to be displayed in QL would be passed to the app through argv?

    I’m asking because it would allow to add a QL functionnality to app-launchers (QS, Launchbar, Butler,…). Example: (Selected Files) -> Open With -> QLbyCiaran

    What do you think? Is it easily feasible?

  2. on 11 Dec 2007 at 6:27 pmCiarán

    Leopard ships with a tool which you can use to preview files, just run

    qlmanage -p «file»
    

    To view a Quick Look preview.

  3. on 11 Dec 2007 at 8:52 pmXavier C.

    But qlmanage doesn’t have the fancy zooming effect nor the spacebar closing functionnality… which you provide with your example code

  4. on 28 Jan 2008 at 1:50 pmWarren

    This works great, thanks for the info! Saved me a ton of work.

  5. on 23 Jul 2008 at 3:06 pmNorbert M. Doerner

    Thank you so much for this really good introduction!

    One question, though:

    When the QuickLook window is the active one, and a single item is selected, in Finder, I can use the arrow keys to navigate through the current view. In your sample (and also in my own implementation), that only works if the original view is clicked onto (thus made active again).

    How could I set up the responder chain or the delegate, so that it would forward me keyDowns to my own view?

    (I have tried to add my own keyDown method to the delegate for QuickLook, but that was never called…)

    Thanks again!!

  6. on 03 Oct 2008 at 10:24 pmCarbcoa

    Just curious. Why are your implementation files Obj C++?

    I changed them to Obj-C and it they compile just fine. I just had to enable C99 to compile one of your for loops. I didn’t see any C++ in any of your code either.

  7. [...] Griekspoor of mekentosj fame pointed me in the direction of Ciarán Walsh’s reverse-engineering of the QuickLook private framework, and I decided to get [...]

  8. on 21 Oct 2008 at 1:35 pmMo

    Hi Ciarán,

    Your reverse-engineering efforts helped me to throw together DropLook—a little applet that previews, via QuickLook, whatever file (or files) you drop onto it in windows with standard decorations and previews.

    So, many thanks—you’ve assisted in me getting around to doing a smattering of Cocoa and I’ve worked around one of my biggest bugbears with QuickLook (the fact the windows vanish when you switch to another app).

  9. on 16 Dec 2008 at 8:35 pmTom

    This is great . .. Amy hints on navigating the file previewed for example a powerpoint file as a slide show in front row

  10. on 05 Mar 2009 at 6:17 pmkrasnojask

    thanks a lot for your reverse engineering efforts!

  11. on 19 May 2009 at 4:56 amPico

    This is great, but for some crazy reason, with Garbage Collection supported or required in the app, everything works fine except for music files which show a big red “Can’t load Display Bundles” message in the QLView and in the console these errors are printed: 2009-05-18 20:53:35.595 QuickLookSample[34122:10b] [QL ERROR] com.apple.qldisplay.Music failed to load QuickLookSample(34122,0xb0103000) malloc: reference count underflow for 0×10db890, break on autorefcountunderflow_error to debug.

    I understand this is an issue with quicklook and it’s music plugin, but is there any solution for this?

    Thank you!

  12. on 24 May 2009 at 4:36 pmNick Rogers

    Hi, Thanks very much for this great effort. Incredible! I am a new mac programmer and since you did the reverse engineering, you might know more about this: Is there any legality involved if I use and distribute it in my small utility program? It will be just a small part of the complete program.

    Thanks, Nick

  13. on 16 Jun 2009 at 2:31 pm.max

    Thank you for the post. I’ve used this approach to customize Quick Look behavior a little further for what I need. By the way Quick Look UI appears to be a part of public Quartz framework in Snow Leopard. Which is good. Kinda. Most private APIs are deprecated though and it might get tricky to use Quick Look in both Leopard and Snow Leopard.

  14. on 11 Jul 2009 at 9:29 pmYon

    Ciarán,

    Brilliant job! Really cool!

    I’m a bit new to cocoa development and I would definitely like to avoid direct linking the framework, but I’m stuck. Please excuse my ignorance, but how would I go about loading the framework with NSBundle? I know you posted the code to load the framework (which, I copied and pasted here below), but I just don’t know where to put it in my code. Can you please tell me where to include the code?

    Thanks!

    if([[NSBundle bundleWithPath:@"/System/…/QuickLookUI.framework"] load]) NSLog(@”Quick Look loaded!”);

  15. [...] Ciarán Walsh’s Blog » Quick Look APIs ciaranwal.sh/2007/12/07/quick-look-apis – view page – cached 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. — From the page [...]

  16. on 11 Jan 2010 at 2:09 pmtax jobs

    Tips to help you find jobs for using the internet for your jobsearch.just logon to http://taxvacancies.com

  17. on 02 Mar 2010 at 9:14 amMikko

    Is it possible to launch quick look from a command line tool using this approach? I am aware of the qlmanage tool, but it provides only a subset of features from what launching quick look from Finder does.

Trackback this post | Feed on Comments to this post

Leave a Reply

 Comments are Markdown formatted

Fork me on GitHub