Want to add keyboard shortcut to open selected file with specific mac app

I want to select a file and press a key combo to open the selected file with a specific application that's installed on my mac running Catalina. I don't want to always open this file type with this program so I can't create default settings.

Is there any way to map keyboard shortcut like the above? Please advise. Thanks!


Solution 1:

I have seen similar mappings done in Karabiner - a more flexible system for mapping keys. It allows even funny mappings - like on the right shift key, or functions that happen if you hold a key. But they require some scripting and while it's easy to get something that works on my machine only its hard to figure out something sharable. I will outline what I think needs to be done.

Karabiner a feature called "Complex modifications" that allows this. I find the documentation sparse but it's simple enough to figure out.

There is a number of pre-made mappings where the code can be inspected at comples_modifications (use the "show JSON" that reveals when you click the triangle next to "import").

Custom mappings can be just put under ~/.config/karabiner/assets/complex_modifications and can then be imported.

The launch app mappings all have a line like this:

"shell_command": "open '/Applications/TextEdit.app'"

All that's missing is the selected file.

oh-my-zsh has a function that does this. It's rather short, here in full:

pfs () {
    osascript 2> /dev/null <<EOF
    set output to ""
    tell application "Finder" to set the_selection to selection
    set item_count to count the_selection
    repeat with item_index from 1 to count the_selection
      if item_index is less than item_count then set the_delimiter to "\n"
      if item_index is item_count then set the_delimiter to ""
      set output to output & ((item item_index of the_selection as alias)'s POSIX path) & the_delimiter
    end repeat
EOF

From the command line a command like this works:

open -a TextEdit $(pfs)

All that would be left is to figure out how to get the pfs definition into Karabiner... I will leave that as exercise to the interested reader 😉