Icon on Desktop to Run Terminal Command

Discussion in 'Mac Programming' started by Borja1055, Mar 22, 2016.

  1. Borja1055 macrumors regular

    Joined:
    Jul 25, 2010
    #1
    Hi,

    I need to create an icon on the desktop to run a terminal command by double clicking. Or even better, on computer startup. Please let me know if this is possible. Thank you.
     
  2. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #2
    Sure. Probably the easiest way is to make an Automator action with a "Run Shell Script" action, then save it as an application.

    You can add that application to your login items to make it run on startup.

    I implemented something similar recently myself and documented the process here:

    https://www.ghostotter.com/making-shell-scripts-little-user-friendly/

    Hope that's some help
     
  3. Borja1055 thread starter macrumors regular

    Joined:
    Jul 25, 2010
    #3
    Awesome. I'll give it a go this afternoon and get back to you. Thanks.
     
  4. Weaselboy Moderator

    Weaselboy

    Staff Member

    Joined:
    Jan 23, 2005
    Location:
    California
    #4
    Kind of a variation of what superscape suggested, buy you could also use Script Editor to make an application to do what you want. Just put your Terminal command inside the quotes in place of say hello, then save as an application.

    Code:
    do shell script "say hello"
     
  5. Borja1055 thread starter macrumors regular

    Joined:
    Jul 25, 2010
    #5
    Cool! Ill play around with it later and see what works better. Thanks.
     
  6. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #6
    Code:
    do shell script "say hello"
    Yup, that's will work fine too.

    It can get a bit confusing when you get into the realms of quoting things, but if you're confident with that then there's no reason not to do it that way.
     
  7. Borja1055, Mar 23, 2016
    Last edited: Mar 23, 2016

    Borja1055 thread starter macrumors regular

    Joined:
    Jul 25, 2010
    #7
    Ahh. It won't work because it is a sudo command that disables the internal keyboard, therefore it needs a password when opened.

    Message is:
    sudo: no tty present and no ask pass program specified (1)

    EDIT:

    FYI, the code I am using is.

    Code:
     sudo kextunload /System/Library/Extensions/AppleUSBTopCase.kext/Contents/PlugIns/AppleUSBTCKeyboard.kext/ 
     
  8. theluggage macrumors 68030

    Joined:
    Jul 29, 2011
    #8
    Alternative to the Automator method:

    just create a plain text file (use a programmer's text editor, not TextEdit which will try to create a rich text file) containing your terminal commands, give it the the extension ".command", and then make it executable (needs a terminal command).

    To do this all from the terminal:

    Code:
    cd ~/Desktop
    cat > test.command
    echo Hello World!
    ^D
    chmod u+x test.command
    
    I usually include the bash shebang so you can also run the file from the terminal. I.e. the file becomes:
    Code:
    #!/bin/bash
    echo Hello World!
    
    Con: starts up terminal and leaves a terminal window open cluttering up your desktop.
    Pro: starts up terminal and leaves a terminal window open, so you can see any output and respond to requests for input.
     
  9. Borja1055 thread starter macrumors regular

    Joined:
    Jul 25, 2010
    #9
    Thanks for the response, and excuse my ignorance but...

    Can you explain the process with a little more detail. Looks like you have included plenty of detail but this is my first dive into terminal/scripts.
     
  10. theluggage macrumors 68030

    Joined:
    Jul 29, 2011
    #10
    What you want to do is:

    (a) Get hold of a programmer's text editor (there are at least 3 already installed that you can use in terminal, or download TextWrangler, Atom, Sublime Text, XCode).

    (b) Create a file containing:
    Code:
    #!/bin/bash
    sudo kextunload /System/Library/Extensions/AppleUSBTopCase.kext/Contents/PlugIns/AppleUSBTCKeyboard.kext/
    [/bash]
    
    The first line ensures that the script file is run in "bash" - I'm not sure if its necessary for (or even used in) a .command file  but it does mean you can also run the file directly from terminal if you wish.
    
    (c) Save it to the desktop as 'mycommand.command'
    (d) Now open the terminal, change directory to the desktop ("~/" means your home directory) and make the file executable by the owner:
    [code]
    cd ~/Desktop
    chmod u+x mycommand.command
    
    ...and you should have a double-clickable icon on your desktop that pops up a terminal window & runs the command in it.
     
  11. Alrescha macrumors 68020

    Joined:
    Jan 1, 2008
    #11
    Or just use TextEdit and choose 'Plain Text' in the Format menu. Why make it hard?

    A.
     
  12. Borja1055 thread starter macrumors regular

    Joined:
    Jul 25, 2010
    #12
    Awesome. Sounds simple enough. Ill give it a try this afternoon. Thanks.
    --- Post Merged, Mar 24, 2016 ---
    Noted. Thank you.
     
  13. theluggage macrumors 68030

    Joined:
    Jul 29, 2011
    #13
    Honestly? Because I never use TextEdit, knew that it defaulted to rich text, and when I looked quickly it didn't have a "plain text" option under "file format" in the "Save" dialog box where it logically should be so I thought it might have been "improved" away.

    The most important thing about a "programmer's text editor" is not that it does magical super-secret programmers stuff, just that it doesn't even try to be a word processor.

    Borja1055 is lucky: plenty of Unix-heads would have told them that using anything other than "vi" :confused: would cause hair loss and sterility.
     
  14. Alrescha macrumors 68020

    Joined:
    Jan 1, 2008
    #14
    Agreed. I did not intend to be (too) snarky, I just fear that sometimes we forget who we are: some person comes along and wants to do some simple and easy thing (like make this shortcut) and our first suggestion is 'download (and learn) this new text editor'. Seems unfortunate.

    A.
     
  15. theluggage macrumors 68030

    Joined:
    Jul 29, 2011
    #15
    Or, to put it another way, "first get the correct tool for the job". As I said, I don't use TextEdit much, but it has evolved into a 'lite' word processor rather than a text editor so apart from the text format 'gotcha!' I had no idea what the defaults for word-wrap, autocorrect etc. (which are poison for editing scripts) were.

    Also, while, you can spend forever becoming a Sublime Text Ninja or a Senior TextWrangler Wrangler, for the purposes of this task, all you need to 'learn' with TextWrangler/Atom/Sublime is "File->Save as..."
     

Share This Page