Tuesday, 30 September 2014

How to open apps from terminal on mac

I switched to mac from ubuntu recently. I noticed that apps installed from .dmg files are not accessible from terminal. In ubuntu to open say, gvim (MacVim in mac) I used to simply type gvim in the terminal. This doesn't work on mac :(

After doing some analysis on how apps are maintained, I noticed that each application (.app) is stored in /Applications folder. And .app file is simply a directory holding contents of the app. Say I open firefox, the executable which launches the app (firefox), stored in /Applications/Firefox.app/Contents/MacOS is executed.

So if I execute:
/Applications/Firefox.app/Contents/MacOS/firefox
from terminal, firefox is launched!

However I do not want to type this huge path every time I want to launch firefox nor do I want to alias each and every app I install nor do I want to add all of them to $PATH. So I wrote a simple script to automatically open any app I want to.

Step1


Download this gist

Step2


Make it executable and move it to /usr/bin. You are done!

$ chmod +x app

$ sudo mv app /usr/bin/


How to use it?


Say you want to open chrome from terminal to open a html file

$ app chrome index.html

Say you want to launch vlc from terminal to open a file

$ app vlc file.mp4

Say you want to open MacVim from terminal and open a file. Note that it is case insensitive.

$ app macvim file