2

I'm hoping something like this already exists out there but I know enough scripting/programming that maybe with a bit of direction I could create something to do this?

If I select a file in Finder and hit CMD+O, of course that file will open with the default application. Often that is fine, but other times I want to open a file in an editor instead. What I'm kind of looking for is a secondary default for Edit instead of Open that I can assigned a shortcut (CMD+E) to. The result would be CMD+O still opens images in Preview but CMD+E opens them in Photoshop for example. I want the same shortcut to work regardless of file type so long as I have a matching editor configured for that type somewhere, otherwise fall back to the open default.

Here's what I've found in my searches around this:

2
  • 1
    "Having a matching editor configured for that file type" is non-trivial. You would have to have a list or array of data linking file types with apps, independent of LaunchServices database. You could just use an AppleScript with logic to check the file type and then open with <app>. Commented 12 hours ago
  • @benwiggy we must be speaking different languages because from where I sit, you're saying the same thing I did (check file type, open with given app based on type)? LOL. Anyway, question answered by Aivar Paalberg more or less on those lines. :) Commented 3 hours ago

1 Answer 1

3

One way is to use Automator Quick Action (assign keyboard shortcut to it) and bash script (I don't use zhs).

In Automator:

  • select "Quick Action"
  • select "Run Shell Script"
  • for "Workflow receives current" select "files or folders" and "Finder"
  • "Shell" select "/bin/bash" (which is ancient system bash)
  • "Pass input" select "as arguments"

Script determines file type by file extension, opens pdf files with Preview and csv files with Excel (I have system defaults to other applications for those file extensions so I used these to test if script works) and if nothing specified falls back to system default

for f in "$@"; do
  [[ -e "$f" ]] || continue

  filename=${f##*/}
  ext=""

  if [[ "$filename" == *.* && "$filename" != .* ]]; then
    ext=${filename##*.}
    ext=$(printf '%s' "$ext" | tr '[:upper:]' '[:lower:]')
  fi

  case "$ext" in
    pdf)
      open -a "Preview" "$f"
      ;;
    csv)
      open -a "Microsoft Excel" "$f"
      ;;
    *)
      open "$f"
      ;;
  esac
done

I saved it as Smart Open

Then in System Settings -> Keyboard Shortcuts -> Services -> Files and Folders there should be visible "Smart Open". Assign keyboard shorcut to your liking. In Finder using this keyboard shortcut will open pdf files with Preview and csv files with Excel and all others with system default.

Known limitations: multi-part extensions are not recognized - archive.tar.gz is treated as gz, not tar.gz; files starting with . will fall to system default. Most certainly there are others limitations and gotchas as well but my proof-of-concept usage confirmed that it worked as expected.

1
  • That worked. Thank you!!! Commented 7 hours ago

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.