Script to generate a quick look based on a file path (URL format) located in the clipboard

I often paste file paths into Evernote (which requires URL path format).
To open the file, I obviously just have to click on the link. Often, all I want to do it have a quick look at the file (equivalent of Finder or Pathfinder → spacebar).
I wrote a macro to right click on the link and copy the URL file path to the clipboard, but that’s as far as I got.
Is there a way to create a script which would generate a quick look of a file whose URL path is in the clipboard ?
I tried the qlmanage shell script command, but it only works for images. I want to quick look mostly MindNode MindMaps, which can be viewed in quick look, as confirmed by both Finder or Pathfinder → spacebar, and using Scrivener which has a quick look function).

thanks in advance for your time and help

Can I just check, were you converting the URL to a real UNIX-style file path prior to passing it to qlmanage?

Note, qlmanage should be able to preview more than just images.

1 Like

thank you for your help.
I tried qlmanage -p within terminal with a pdf (grocery order, which is why you see order below), and all I get is the image below.
I will read the TidBITS article before going further.
Is there a simple script I could use to transform a URL path in the clipboard to a UNIX path ?
thanks again very much

if you qlmanage -p Unix-filepath, do you actually see a quick look of for example a pdf file ?

I certainly do. Here’s a one liner on the left that opened the specified PDF in my downloads folder and displayed it. You can see it on the right; note the ‘debug’ in the title indicating it is qlmanage.

The file URLs I use are just prefixes of the UNIX path with file://, so a simple single substitution suffices. The screenshot of the commands below shows a worked example.

But I guess the shortest path to things might be this; it works for me at least with a file URL on the clipboard.

qlmanage -p "${$(pbpaste):s/file\:\/\//}"

Hope that helps.

1 Like

My apologies for the delay.
Conversion URL to Unix: thanks to your explanation, it is now easy to do with keyboard maestro and very useful. I am very grateful !
I am still trying to understand why qlmanage does not work. I will figure it out.
thanks again for your very kind and informative help

You may have to specify a code generator and content type specifier so QL knows what type of data it’s dealing with.

1 Like

In that case it works, but this doesn’t work in all cases, it depends on the file name of the PDF. For instance if there are spaces in the file name, these are encoded as %20 in the URL. So if you want to convert the URL to a POSIX path you need to convert these %20 coding to a space.

If there are accented characters in the file name some more conversion is needed, eg.
the filename “Factsheet Günter 2019.pdf” is encoded in an URL as “Factsheet%20G%C3%BCnter%202019.pdf”.

For use in Keyboard Maestro cyrus can try this script:

set quicklookurl to (the clipboard as text)
--strip file://
set stringlength to length of quicklookurl
set quicklookurl to characters 8 thru stringlength of quicklookurl as text
--convert html-codes to characters
set this_file to do shell script "ruby -e 'require \"cgi\"; puts CGI.unescape(ARGV.join(\" \"))' " & quicklookurl
--run quick look
do shell script "qlmanage -p " & quoted form of this_file

Totally agree, and that’s why I included that caveat at the start saying where I use them - it’s actually very rare I do use base file hyperlinks (I’m more of a Hook user these days). I was thinking of spaces, but your point on accented characters, etc. is a very good point.

Looking at the syntax, I’m assuming that should be an AppleScript action in Keyboard Maestro.

1 Like

I now realize that path require quotes in shell scripts, so all is fine. thanks again

I do use KBM and thanks very much for the script !

I haven’t tested it in KBM, but it works in the AppleScript editor.

I realised that there are more characters besides the html characters in an URL that can give problems. Eg. if the file name/URL contains round brackets, the script gets an error message. This is because the script uses the do shell script command to go from AppleScript to the Shell and round brackets are special characters in Unix commands. So if you use them in the file names you first needs to escape them with a \ before you send the command to the shell.

1 Like

thank you very much Frankie !