I recently reviewed my new third-party mouse, the Logitech MX Master 2s ($70 at Amazon). I love all the customizable buttons on this mouse, and in particular, the ability to create gestures (which are simply a directional drag while holding down a button).
I've used two buttons to create a total of 10 gesture actions on my mouse:
I created the above image from the Logitech Options app, so I can refer to it if I forget which action I've assigned to which gesture. But it's a pain to find the folder on the disk and open it just to see the image. Then I remembered I had an unused button on my mouse1The upper button of the two near the side scroll wheel…
Note: While the following is specific to my Logitech mouse in terms of implementation, read on if you're curious about how to access QuickLook previews from Terminal and/or via a simple AppleScript.
I could simply assign the button to open the image file, and that would be that—but it'd open in Preview, which I'd then have to quit when done—and it would take me out of whatever app I was working in at the time.
So I didn't really want to open the image file; what I really wanted to do was the equivalent of a Finder Quick Look on it: Select the image and press the Space Bar to open its Quick Look preview…but do it not from Finder. This would keep me in the app I was using, and I could dismiss the window with the Escape key or by clicking the 'x' button.
I vaguely remembered something about using Quick Look from Terminal, so off I went to the macosxhints' archives, where I found the solution (from 12 years ago!):
For all you Leopard users out there, here's a handy tip on how to use Quick Look from the command line. Leopard ships with a command called qlmanage. The -p option shows a preview of the file passed to the command. In the terminal, type the following:
qlmanage -p thefile
As you'll see, it also generates a ton of text output…
With the command in hand, all that was left was to turn it into an application, so that I could assign it to the button in Logitech's software. I could do so from Terminal, but that would open Terminal, defeating the purpose. Instead, I created an incredibly simple one-line AppleScript, and saved it as an application:
do shell script "qlmanage -p /full/path/to/image/file.png"
If your path name contains spaces, as mine did, you need to double-escape the spaces:
1 | /Users/someuser/Documents/My\\ Images/Look\\ Here/file.png |
After testing in AppleScript, I used File > Save As to save the one liner as an application. I then assigned my free mouse button to launch that application, and it works perfectly. Now, if I forget which gesture opens which folder, one button click shows me the answer within a second.