5 Replies Latest reply: Dec 30, 2012 8:01 PM by BobHarris
Col1107 Level 1 Level 1 (0 points)

I have a python script that I run in terminal but inorder to launch it I have to type:

 

~/Documents/python\ stuff

 

and then

 

python apps.py

 

I want to make a shortsut or app so I dont have to do that every time.

 

 

Thanks.


MacBook Pro, OS X Mountain Lion (10.8.2)
  • 1. Re: I Want to make a shortcut to a terminal command
    BobHarris Level 6 Level 6 (13,090 points)

    Do you

     

    cd ~/Documents/python\ stuff
    

     

    or is

     

    ~/Documents/python\ stuff
    

     

    a program/script that you run first?

     

    Can you enter:

     

    python ~/Documents/python\ stuff/apps.py
    

     

    Anyway, if you create a ~/.bash_profile shell initialization script, you can create any number of ways to run the python script.

     

    nano ~/.bash_profile
    

     

    will allow you to edit the shell initialization script.

     

    Things you can create

     

    myCommandName()
    {
        (  cd ~/Documents/python\ stuff
           python apps.py
    
           #
           # or whatever commands you need to execute.
           #
        )
    }
    

     

    Save the .bash_profile, and restart you Terminal session so the .bash_profile shell initialization script is run as your shell startup in a new Terminal session.

     

    if "python ~/Documents/python\ stuff/apps.py" works, then you could use

     

    alias myCommandName "python ~/Documents/python\ stuff/apps.py"
    

     

    again restart you Terminal session.

  • 2. Re: I Want to make a shortcut to a terminal command
    Col1107 Level 1 Level 1 (0 points)

    Thanks for the quick reply, but I'm really not very advanced with terminal so.. I have no idea what you you just said. I was thinking that there may be a way to do it in AppleScripEditor and make an executable program. Is This possible?

     

    Yes I do type:

       cd ~/Documents/python\ stuff    
    

    And

      python ~/Documents/python\ stuff/apps.py
    

    does work.

     

     

     

    When I tries to  type in

     

    alias apps "python ~/Documents/python\ stuff/python.py"
    

     

    I get:

     

     

    -bash: alias: apps: not found
    -bash: alias: python ~/Documents/python\ stuff/python.py: not found
    

     

     

     

    Thanks for any help!

  • 3. Re: I Want to make a shortcut to a terminal command
    BobHarris Level 6 Level 6 (13,090 points)

    My bad.  I messed up the 'alias' syntax.  Try the following:

     

    alias apps="python '~/Documents/python stuff/python.py'"

     

    Once you have this in your .bash_profile, do not forget to restart your Terminal session.

  • 4. Re: I Want to make a shortcut to a terminal command
    Col1107 Level 1 Level 1 (0 points)

    Im sorry I still don't understand. You'll have to talk to me like im 5.

     

    So i go to terminal and i can type

     

    python ~/Documents/python\ stuff/apps.py
    

    and my program runs.

     

    And then I type in

     


    alias apps="python '~/Documents/python\ stuff/apps.py' "
    

    and I get a new terminal command. I restart my terminal session.

     

    Then what? If I type is "apps" I get:

    -bash: apps: command not found
    

     

    If I dont restart my terminal session After typing in the alias thing, I get:

     

    /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't open file '~/Documents/python\ stuff/apps.py': [Errno 2] No such file or directory
    

     

     

    Thanks again! Im sorry I don't understand.

  • 5. Re: I Want to make a shortcut to a terminal command
    BobHarris Level 6 Level 6 (13,090 points)

    I restart my terminal session.

     

    Then what? If I type is "apps" I get:

    -bash: apps: command not found 

    The 'alias' command only lives in the current session.  if you start a new Terminal session or Window, then you have a new session.

     

    If you wish to have your 'alias' everytime you start a Terminal session or window, then you need to put the 'alias' command in a shell initialization file:

     

    nano ~/.bash_profile

     

    and add the 'alias' command here.  Now everytime you start Terminal or a new Terminal window, the .bash_profile will be run, and your 'alias' will be defined.

     

    Message was edited by: BobHarris