Opening a File with a Specific App from the Terminal on Mac
This article was automatically translated from Japanese by AI. Read the original Japanese version →
Table of Contents
TL; DR
Use terminal to open a file by Excel on Mac
Background
On a Mac, there are times when you want to open a file from the terminal using a specific app, right? If you want to open it with the default app assigned to that file extension, open hogehoge.csv works. On a Mac, simply running open on a CSV file launches Numbers.
Personally, though, I fairly often want to open CSVs in Excel. That's because my current company uses Microsoft Office products as standard tools, so sharing data is almost always done in Excel. Every so often I have to format a CSV nicely in Excel and save it as an Excel file to hand over.
And since engineers are basically glued to their terminals, we naturally want to open CSV files from the terminal too.
How to Do It
The solution is right there in the StackOverflow link above—you just specify it with an option.
open -a "/Applications/Microsoft Excel.app" hoge.csv
It looks simple enough, though typing this every single time is admittedly a bit of a hassle. Ideally I'd like to just type something like excel hoge.csv. So let's set up a quick alias. Since I'm a zsh user, I add the following to ~/.zshrc:
alias excel="open -a /Applications/Microsoft\ Excel.app"
Now running excel hoge.csv opens the csv in Excel.
Summary
So, that's how to open a file with a specific app from the terminal on a Mac.
By the way, code editors like Sublime Text or Atom can usually be invoked directly via commands like subl or atom, so you don't need little tricks like this for those.
The situations where you need it are pretty niche, but it's handy to know, so give it a try if you get the chance.