Project44.Customize the Terminal


Project 44. Customize the Terminal

"How do I make a clickable shell script?"

This project uses a few tricks to customize the look of Apple's Terminal application, saving settings and shell scripts in clickable documents. Other projects in the chapter look at customizing the shell.

Learn More

Refer to Project 9 if you are new to writing shell scripts.


Create a Clickable Shell Script

You might find yourself having to write a shell script for colleagues who refuse to be separated from their mice. To help you in this task, Terminal recognizes when a file contains a shell script. Double-clicking such a file launches a new Terminal window in which the script is automatically executed.

Let's write a simple script to illustrate this. A file that contains a script must be associated with Terminal; by using the extension .command we make the association automatic. The example script that follows displays the names of files in your Documents folder that were modified less than a day ago.

$ cat mod.command #!/bin/bash echo "All Documents modified less than a day ago" find ~/Documents -mtime -1


The script must be made executable before it can be run.

$ chmod +x mod.command


To run the script, select the file mod.command in the Finder, and double-click it. A new Terminal window will open and immediately start executing the script. The window will remain open so you can view the results, which will be followed by logout and [Process completed].

Save Custom Window Settings

Terminal allows you to define and save custom settings, such as the window size and its position onscreen, font and background colors, and the scrollback buffer size. You may wish to define several different looksa large black-on-white window for editing, for example, and a smaller green-on-black window for issuing commandsselecting the appropriate look when opening a new Terminal window.

Tip

When saving a custom look, you may set up several windows and select All Windows from the What to Save pop-up menu in the Save As dialog box. Subsequently opening such a custom window actually opens all the windows exactly as they were when the settings were saved.


To accomplish this, make a custom window by choosing Terminal > Window Settings. Set up the window as you wish; then save the settings in a dot-term file by choosing File > Save As. Ensure that the filename you give has the extension .term. After the file is saved, double-clicking the file in the Finder will open a new Terminal window sporting the preset look.

By default, Terminal should apply the correct extension and save to the directory ~/Library/Application Support/Terminal/. All dot-term files saved to this directory will appear when you choose File > Library, enabling you to open a new custom window easily from Terminal itself.

Learn More

Chapter 4 covers the Unix text editors, including nano.


Run a Script in a Custom Window

You can have your Unix cake and eat it, should you wish to create a clickable shell script that also opens in a custom window. To do so, use the dot-term file you created in the section above. Open it in a text editor such as nano, and search for the following lines.

<key>ExecutionString</key> <string></string>


When Terminal opens a custom window from a dot-term file, it automatically executes the commands it finds in the Execution String. Therefore, we might add the little script from "Create a Clickable Shell Script" earlier in this project by changing the Execution String to read

<key>ExecutionString</key> <string>     echo "All Documents modified less than a day ago"     find ~/Documents -mtime -1 </string>


When we use the edited dot-term file to open a new Terminal window, not only will it sport a custom look, but it will also display the names of all files in ~/Documents that were modified less than a day ago.

If you wish the window to close afterward, simply add an exit statement to the end of the Execution String and make sure Terminal is configured to close windows on exit: Choose Terminal > Window Settings to open the Terminal Inspector palette; choose Shell from its pop-up menu, and click the "close the window" button.

Tip

If the Execution String contains many commands, write the commands to a shell script and call the script instead. The effect is the same, but we put the commands in a more accessible script file, rather than embedding them in the middle of a dot-term file. Use an absolute pathname in the Execution String to refer to the shell script file.


Change the Title Bar

Terminal lets you customize what's displayed in its title bar. Choose Terminal > Window Settings to open the Terminal Inspector palette; then choose Window from its pop-up menu.

Of course, that's not a very Unix-like way of doing things: We can instead employ a neat trick from the command line that affords much more flexibility in what's displayed. Let's reflect the current username and hostname in the title bar. Issue the command

$ echo -e "\033]0;$USER@$HOSTNAME\007"


and check the title bar. You should see yourusername@yourhostname and perhaps some additional details displayed by Terminal itself (which can be removed by changing Terminal's Window Settings).

You'll no doubt want to know how this works! Option -e applied to the command echo tells it to interpret backslash sequences such as \033, which translates to Escape. The sequence Escape ] 0 ; tells Terminal to write the text that follows on its title bar (instead of onscreen), up to the sequence \007. The real flexibility lies with the Bash shell's expanding the text before it is echoed and sent to the title bar. In this example, Bash expands the variables $USER and $HOSTNAME.

Instead of echo, you may use printf to achieve the same effect.

$ printf "\033]0;$USER@$HOSTNAME\007"


Dynamic Title Bars

A static title bar is no good when the information being displayed changes from time to time; we want the title bar to reflect the changes. To this end, Bash has a useful trick up its sleeve: the prompt command. Set the shell variable PROMPT_COMMAND to any command sequence, and Bash will execute the commands immediately before it issues a prompt. This gives us the hook needed to implement a dynamic title bar; we set the prompt command to be exactly the command used in the previous section.

Note

To rename the title bar in the Tcsh shell, use the same printf command as for Bash, but replace $HOSTNAME with $HOST.


$ PROMPT_COMMAND='echo -n -e "\033]0;$USER@$HOSTNAME\007" '


If you're in the habit of logging into other machines, glancing at the title bar will remind you of where, and who, you currently are. If you're not, perhaps an alternative may be of more use. Try this.

$ PROMPT_COMMAND='echo -n -e "\033]0;$PWD - $(date)\007" '


Note

The Tcsh shell uses an alias called precmd to achieve the same effect as Bash's PROMPT_COMMAND shell variable. Try this.

% alias precmd 'printf ¬      "\033]0;$PWD - ¬      `date`\007" '



This information could be displayed in the prompt rather than the title bar (see "Think Different" in Project 45), but on the title bar it's less in your face and prevents an excessively long prompt.

You may want to make such changes permanent by adding the appropriate commands to a Bash configuration file (see Project 47).

Tip

You may use PROMPT_COMMAND for any purpose, not just setting the title bar.





Mac OS X UNIX 101 Byte-Sized Projects
Mac OS X Unix 101 Byte-Sized Projects
ISBN: 0321374118
EAN: 2147483647
Year: 2003
Pages: 153
Authors: Adrian Mayo

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net