Is there any way to create a file which, when clicked, opens up Terminal and types in and executes a specified line? (eg: say hello world)


The simple, but completely fallible, approach is to just write a shell script. Save this to a file called mysay.sh:

say hello world
say number five is alive

As long as you don't have XCode installed that should open up in Terminal when you double click on on it.

You can also do this with AppleScript and it's a little more durable since you don't have to worry if the default application for .sh files isn't Terminal:

tell application "Terminal"
  set currentTab to do script "/usr/bin/say hello world"
  do script "/usr/bin/say number five is alive" in currentTab
end tell
delay 5
tell application "Terminal" to quit

Save that as an Application from the AppleScript Editor and you can double click on it and your Mac will talk to you. You can wait and close the Terminal session afterwards with a little more AppleScript if you like.

| improve this answer | |
  • Oh man, that's perfect. Thanks so much. The extra steps you mention about waiting then closing Terminal would be great to know... – bravokiloecho Apr 22 '12 at 18:52
  • With the delay. – Ian C. Apr 22 '12 at 21:02
  • I followed the second method and saved it to my desktop and it runs perfectly. However, if I try emailing it to myself and then opening the downloaded it file it gives the error message that I can't open it because "the Classic environment is no longer supported". Any idea how to solve this? – bravokiloecho Apr 22 '12 at 22:04
  • That's odd. What email program? Virus scanner? Sounds like something munged the app bundle on you. – Ian C. Apr 22 '12 at 23:15
  • I'm using gmail. If I use the Console force Script Editor to open the downloaded file I get this error message: Unable to read the dictionary of the application or extension because it is not scriptable. – bravokiloecho Apr 23 '12 at 10:30

What you are trying to do is write a "shell script" (in case you need to google it later on).

The first line of a shell script needs to specify what interpreter to use. In your case, use sh for the regular Bourne shell. You write this with what's called a 'shebang' like so:


after this write whatever commands you want to run, in your case, say hello world. The shell interpreter will read this the same as if you had typed it in terminal.

In your case, your final file should be:

say hello world

If you write this in TextEdit.app make sure you are saving in plain text mode (which is toggled through Cmd + Shift + T). You can choose to save with either the extension .command or .sh.

If you want to run it by double clicking I recommend the .command extension.

And that's it!

Possible error: if when running it you receive an error saying:

The file “[filename]” could not be executed because you do not have appropriate access privileges.

you need to give yourself execute permission. To do this run:

chmod u+x path/to/my/file

If you don't know what the file-path is just type:

chmod u+x

type a space, and drag the file into the window.

If you're curious, u+x means give user (owner) execution rights.

| improve this answer | |
  • That's all really helpful, thanks so much. Is there any way to include the execution rights within the shell script itself (so that if I email it to someone, it works straight away without them having to run chmod u+x ? – bravokiloecho Apr 23 '12 at 11:13
  • @negatron haha, no, its a chicken-egg style thing, you need execution rights to run the script; and if you want the script to give itself execution rights you have to run it. Besides, imagine how easy it would be to infect machines if you didn't need to grant execute rights; there's a reason they exist in the first place. – Hawken Apr 23 '12 at 21:33
  • You have a point! Shame for me though! – bravokiloecho Apr 24 '12 at 10:43

You can also do this with AppleScript as mentioned above, by Ian C. However, you can do the single command in fewer lines, and with a delay. Here is an AppleScript which will run the command, then wait 5 seconds, and then quit the terminal.

tell application "Terminal" to do script "say hello world"
delay 5
tell application "Terminal" to do script "logout"
tell application "Terminal" to quit

Just an alternate method, compared to @Ian C.'s way. You could also take Ian's .sh file, and rename it .command if you do have XCode, which will open it in Terminal, and do the same thing as the AppleScript, just without the delay. Hope this helps.

| improve this answer | |
  • Thanks really neat. Thank you. Is there any way for the quit command to execute once the speaking has finished rather than based just on a timer? – bravokiloecho Apr 22 '12 at 19:10
  • Yup, just delete the "delay 5" line. – ruddfawcett Apr 22 '12 at 20:03
  • This can cause some truly unwanted affects as it will kill the entire Terminal application – Hawken Apr 24 '12 at 23:30
  • I thought that the asker wanted Terminal to close after the command has been executed? – ruddfawcett Apr 24 '12 at 23:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .