OS X: How to Convert a Terminal Command Into a Double-Clickable Desktop File

| How-To

Very often, in OS X, when one has a terminal command that needs to be used often, it's convenient to turn that UNIX command into a double-clickable desktop file with a recognizable icon. How can that possibly be done? I'll show you how.

OS X is based on BSD UNIX

Here's the basic outline of this tip.

  1. Create a UNIX script with a text editor that contains one or more terminal commands.
  2. Make it executable.
  3. Double click it in the Finder.

It's really quite simple, but there are a few things to be aware of. I'll walk you through it and add some notes as needed.

I. Create a script. UNIX scripts are similar to AppleScript. There are commands and a syntax. From time to time you'll see articles that show how to change an OS X preference from the command line. For example, you may have seen this terminal command that strips the drop shadow from your screenshots:

defaults write com.apple.screencapture disable-shadow -bool TRUE

Note #1. The Terminal app location is /Applications/Utilities/Terminal.app

Scripts are a sequence of commands, managed by the scripting language, to a achieve a task. The easiest way to write a script is to use a text editor, like OS X's built-in TextEdit, found in the Applications folder.

Note #2. Make sure the Preferences for TextEdit are set correctly. Preferences > New Document > Plain text. Once set, relaunch TextEdit.

Here's a simple script that uses the "uptime" command to display how long it's been since your Mac was rebooted.

#!/bin/bash

echo “My script is running.”

# Your command is next.
uptime

echo "Done!"

Note #3. The first line tells OS X to use the Bash scripting language. There are several to chose from. We won't dig into that here.

Copy and paste this script into a new TextEdit file. Call it "ByYourCommand.txt" Save this file on your desktop.

II. Make the Script Executable.

1. In the Finder, delete the file extension ".txt" The Finder will ask for confirmation.

2. Open the Terminal app and navigate to the file. Substitute your own login name instead of mine. Like this:

 cd /Users/john/Desktop 

3. Still in the terminal, execute this UNIX command:

 chmod 744 ByYourCommand 

Note #4. This UNIX command makes the file executable, that is, double-clickable.

Note #5. If you have antivirus software installed, it may object, depending on its preferences, to an executable script being inserted into a file that was created by an OS X text editor. Just ignore the warning.

At this point, you'll notice that the file's icon has changed to this:

III. Double click the file "ByYourCommand" on the desktop. The Terminal app will launch, the script will be executed, and you'll see the results, like this:

Uptime: one day, 21 h since my last reboot.

If you need to edit the script, you'll have to add the ".txt" extension back. (Or, as the geeks will point out, edit with a UNIX editor like vi. I had to say that.)

This is as far as we'll go with a one line command. Once you get the hang of this and learn a whole lot more, you'll find yourself happily creating your own custom scripts—if you decide to learn Bash or one of the other UNIX shells available. A new door is open to create your own scripts, but you should, of course, learn much more about UNIX and scripting before you go wild with this newfound superpower. A really careless act could render your OS X corrupted and/or unbootable.

Comments

Nom

Or …

Step 1: download TextWrangler from Bare Bones Software

Free, and a really good editor for plain text files (including simple scripts).

John Martellaro

Nom.  TextWrangler is great software. However, there’s a tradition in articles like this to stick to already available system software, if possible, so the user doesn’t have to go get anything, even if free.

That said, if the reader has TextWrangler already available, it’s an excellent choice. Just be sure to double-check the Preferences > Text Files > Line breaks > Unix (Linefeed).

Scott B in DC

If you’re already in the terminal, you can skip the renaming process from the Finder that asks too many questions. You can just use:

Prompt> mv ByYourCommand.txt ByYourCommand

Also, doing the chmod makes it executable by the owner but read-only to anyone else who might log into the computer. If there’s nobody else on the machine you can use “700” for the permissions since it will give you what you need and nobody else can get to it. Conversely, you can forget about mucking with it and just say add the execute permission:

Prompt> chmod +x ByYourCommand

Finally, instead of adding the .txt to open it, you can just Command-Click or right-click on the file, select “Open With” and the editor of your choice.

Oh… and one more thing… you do know that you can wrap a terminal command around an AppleScript or Automator action. Somewhere I think I have a script that will take a shell script and wrap it up so that it is executable from the Finder, if that’s what you want.

webjprgm

You can replace the .txt extension with .command to do the same thing, since Terminal will be run when double-clicking a .command file.

I usually keep TextWrangler in my dock so I can drag-and-drop Unix files onto it to edit them. I think you can do the same with TextEdit but I’m not 100% sure on that.

John Martellaro

Scott B, webjprgm: These are all wonderful notes. But I was trying to keep things simple for the beginner who’s doing this for the first time.  Too much UNIX geekdom would have made the article too hard for the newbie.

Readers, as you can see, there are lots of nifty things you can do once you get started down this path. It’s why UNIX on your Mac is so darn cool.

Log in to comment (TMO, Twitter or Facebook) or Register for a TMO account