Can't open apps from Terminal

Discussion in 'Mac Programming' started by tirwit, Jul 19, 2010.

  1. tirwit macrumors newbie

    Joined:
    Jan 20, 2010
    #1
    Hi

    I have seen some people opening apps (like Safari) from the Terminal jut by typing:

    Code:
    safari
    But for me to do so, I have to do:

    Code:
    open -a safari
    I know it's not important but it's a time saver -> What do I need to do to not have to type "open -a" to open a application?

    Thanks :)
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Most likely they have added the path to the actual executable within the .app wrapper to their PATH.
     
  3. tirwit thread starter macrumors newbie

    Joined:
    Jan 20, 2010
  4. misee macrumors member

    Joined:
    Jul 4, 2010
    #4
    I don't know of any way to open an application by only typing its name. The only solution I would have is to make a symbolic link of the executable <app_path>/Contents/MacOS/<app_name> and place it in some directory that you add to the PATH variable.

    I often use Spotlight to open apps if I don't want to switch to the finder or browse through the application directory (or wherever the app is located).

    Edit: You could also create an alias so that you can type
    Code:
    o safari
    You still have to type 2 additional characters, but it's better than open -a.
     
  5. sammich macrumors 601

    sammich

    Joined:
    Sep 26, 2006
    Location:
    Sarcasmville.
    #5
    You can make a shell script and put it in /usr/local/bin.

    #!/bin/sh
    open -a Safari

    Call it 'safari' then give it permissions to execute (i forgot the safest permissions for this).
     
  6. tirwit thread starter macrumors newbie

    Joined:
    Jan 20, 2010
    #6
    - What does the "#!" means?

    - Can I create the script using AppleScript Editor (never used this program before)?
     
  7. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
  8. tirwit thread starter macrumors newbie

    Joined:
    Jan 20, 2010
    #8
    Ok, thanks. I'll try it. It's very difficult for me since I don't have almost any programming teaching in college (3rd year physics engineering only gave me a very very basic introduction to C), but I'll go for it. :)
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    Or define an alias in the shell. The syntax differs between 'bash' and 'tcsh'.

    Bash:
    Code:
    alias safari='open -a Safari.app'
    Add it to your shell profile and it will be ready for use.
     
  10. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #10
    Or as I suggested in my first reply just add /Applications/Safari.app/Contents/MacOS/ to your PATH environment variable. This contains the actual Safari executable:

    Code:
    ls -l /Applications/Safari.app/Contents/MacOS/
    total 10528
    -rwxr-xr-x  1 root  wheel  13489168  7 Jun 01:12 Safari
    
     
  11. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #11
    Doing it that way has some undesirable side-effects.

    First, if the app is already open, another copy is launched. Second, the app is a child of the shell, and the shell will wait for it to terminate. Third, command-line args are not properly delivered to the app for opening. Fourth, the app can misbehave: when I tried it, it didn't show a menubar on some older OS versions.
     
  12. Dr Kevorkian94 macrumors 68020

    Joined:
    Jun 9, 2009
    Location:
    SI, NY
    #12
    I keep my main apps on the dock I only use terminal to open things when I do some serious work (witch is not alot). So open -a is fine, there are many good sugestions here so try them I fought of one but they beat me to it. So good luck.
     
  13. tirwit thread starter macrumors newbie

    Joined:
    Jan 20, 2010
    #13
    Problem solved. A friend of mine created the .bash_profile (which I thought I didn't need since I had .bashrc) and it was solved. Thanks anyway :)
     
  14. mac2x macrumors 65816

    Joined:
    Sep 19, 2009
    #14
    Here's my .bash_profile and .bashrc if you want something else to play with.

    I've removed some sensitive info from my bashrc, but it's mostly intact. There are a number of very handy aliases for common commands (e.g. ll for ls -l, la instead of ls -a, bye instead of exit or logout). Also, by editing the value in quotes next to PS1 you can have the prompt display whatever name you like. Be sure to un comment it before you save.

    The profile offers some handy features like color coding. Put these in a plain text file and rename each one to put a "." in front of the name, and place them in your home folder. Exit the shell and restart it, and you should see the effects.

    Feel free to edit them to your needs!

    .bashrc

    Code:
    # Source global definitions
    if [ -f /etc/bashrc ]; then
    	. /etc/bashrc
    fi
    
    # User specific aliases and functions
    
    alias a=alias
    alias la='ls -a'
    alias ll='ls -lt'
    alias ls='echo $PWD; ls'
    
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    alias ps='ps -aux'
    alias du='du -h'
    alias df='df -h'
    alias bye='logout'
    
    alias ssh='ssh -Y'
    
    # Reset the prompt
    # Directory:
    # PS1='[\w]$ '
    # Hostname:
    #PS1='whateveryouwant$ '
    
    Code:
    # .bash_profile
    
    # Get the aliases and functions
    if [ -f ~/.bashrc ]; then
    	. ~/.bashrc
    fi
    
    # BASH
    export BASH_ENV=$HOME/.bashrc
    export USERNAME=""
    
    # Set DISPLAY
    export DISPLAY=:0.0
    
    # My Paths
    export PATH=$PATH:$HOME/bin:./
    
    # PREFIX
    export PREFIX=/usr/local
    
    # Terminal colors
    export CLICOLOR=1
    export LSCOLORS=ExFxCxDxBxegedabagacad
    
     
  15. tirwit thread starter macrumors newbie

    Joined:
    Jan 20, 2010

Share This Page