Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

b1055

macrumors regular
Original poster
Jul 25, 2010
181
10
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.
 
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
 
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"
 
  • Like
Reactions: b1055
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"
Cool! Ill play around with it later and see what works better. Thanks.
 
  • Like
Reactions: Weaselboy
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.
 
  • Like
Reactions: Weaselboy
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.
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/
 
Last edited:
I need to create an icon on the desktop to run a terminal command by double clicking.

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.
 
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.

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.
 
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.

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.
 
(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).

Or just use TextEdit and choose 'Plain Text' in the Format menu. Why make it hard?

A.
 
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.

Awesome. Sounds simple enough. Ill give it a try this afternoon. Thanks.
[doublepost=1458822645][/doublepost]
Or just use TextEdit and choose 'Plain Text' in the Format menu. Why make it hard?

A.

Noted. Thank you.
 
Or just use TextEdit and choose 'Plain Text' in the Format menu. Why make it hard?

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.
 
  • Like
Reactions: superscape
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.

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.
 
our first suggestion is 'download (and learn) this new text editor'

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..."
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.