# Starting Applications from the console



## alexworden (Apr 19, 2008)

Hi,

Can anyone explain how I can start an application from the console. Often I want to view a file from the current directory and I simply want to start an application to view it. I'd also like to be able to start a Finder window in the current directory. I've added the ability to start a console from the Finder, but it would be very useful to do the reverse!

This was easy in Windows, you simply had to know the .exe name of the application. I've tried all sorts on OSX with no success. 

In particular, I want to start Safari, Firefox, TextEdit, and Finder..

Thanks for your suggestions! 

Alex


----------



## simbalala (Apr 19, 2008)

You need to know the path to the binary executable, What you normally see as an App is really a folder. If you right click on an app you&#8217;ll see an option to &#8220;Show Package Contents&#8221;. You&#8217;ll find the executable in the MacOS folder within the package.

For example &#8212; TextEdit, the command:

/Applications/TextEdit.app/Contents/MacOS/TextEdit

will launch TextEdit from within Terminal.


----------



## alexworden (Apr 19, 2008)

Thanks simbalala,

I can launch Safari with 

/Applications/Safari.app/Contents/MacOS/Safari

and it starts OK. I tried creating a symbolic link to it from my ~/bin directory using..

~/bin :ln -s /Applications/Safari.app/Contents/MacOS/Safari 
~/bin :ls -l
total 184
lrwxr-xr-x  1 alex  staff     46 Apr 19 19:53 Safari -> /Applications/Safari.app/Contents/MacOS/Safari

and sure enough got the link, but when I run it I get..

~/bin :Safari
2008-04-19 19:53:58.092 Safari[6756:10b] No Info.plist file in application bundle or no NSPrincipalClass in the Info.plist file, exiting

Any ideas what's wrong with that? Same thing happens with TextEdit symbolic link. 

I'm running Leopard if that's any help. 

Another thing that's been bugging me... some applications I can start from the console - that are simple executables - but the window focus doesn't switch to them when they start. I always have to click on them with the mouse. Does anyone know how to make that happen automatically? 

Thanks,

Alex


----------



## simbalala (Apr 20, 2008)

Im not quite sure what youre wanting to do but if your primary need is invoking TextEdit from the command line have a look at TextWrangler, its a free minimized version of BBEdit (which I have). You can install an included command line tool in both TextWrangler and BBEdit.

So you can be in Terminal in any directory and just open a file with this command (I'll use /private/etc/hosts as an example and I've already "cd'd" to /private/etc).

bbedit hosts (for TextWrangler the command is simply 'edit hosts')

This opens hosts in BBEdit and pops BBEdit to the front. You can also save files owned by root by clicking the pencil icon on the far left when it has a red slash though it.


----------



## Mikuro (Apr 20, 2008)

You can't separate the executable from the application package. Even creating a symbolic link will not work, because once launched, the process will only be aware of the symbolic link's location, not the exectutable's true location. It will then search for necessary files from the package relative to the path of the symbolic link, which obviously will not work.

I guess the easiest way to get equivalent functionality would be to create a shell script which simply launches TextEdit (using the full path), and then put that shell script in ~/bin and call it "TextEdit".

As for opening Finder windows, try this:
	
	



```
open .
```

Out of curiosity, how did you go about opening a console from the Finder? Sounds useful.


----------



## jbarley (Apr 20, 2008)

Mikuro said:


> Out of curiosity, how did you go about opening a console from the Finder? Sounds useful.


If by chance it's a Terminal window you want to open,
this little app will do it for you.
Its amazing what a person can find with a google search.

jb.


----------



## ElDiabloConCaca (Apr 20, 2008)

You can launch .app packages using the Terminal using the shell command "open", like Mikuro says.

For example, you can launch TextEdit from the Terminal by executing the following command:

```
open /Applications/TextEdit.app
```
As far as I can see, applications launched in this fashion are brought into focus when they launch.

You can also provide command-line arguments in this fashion with some applications.  For example, if I wished to launch TextEdit from the command line and have TextEdit open a document named "test.txt" on my Desktop (as if I simply double-clicked the document on my Desktop to open it in TextEdit), I could do the following:

```
open /Applications/TextEdit.app ~/Desktop/test.txt
```
...and TextEdit would be launched and brought into focus, with the text document "test.txt" open.


----------



## alexworden (Apr 20, 2008)

Brilliant! Thank you so much guys! "open" will save me so much time and tedium. 

FYI - Open Terminal is the Finder Extension I am already using. Highly recommended!asdf


----------



## kasajian (Aug 23, 2011)

You can also just do:


```
open ~/Desktop/test.txt
```


----------



## kasajian (Aug 23, 2011)

Also, note that if you do:

```
open xyz.cpp
```
it will launch it in the default editor for .cpp files, which will probably be xcode.

If you want to launch TextEdit, use -e:

```
open -e xyz.cpp
```

Alternatively, you can specify a particular application like so:

```
open -a TextEdit xyz.cpp
```


```
open -a XCode xyz.cpp
```


```
open -a "Microsoft Word" x.txt
```

You can also pipe text into the text editor.  For instance, pipe the contents of 'ls' into text edit:

```
ls | open -f
```


----------

