- MacOS shell command to create a new Terminal Window. Of course, you can easily create a new Terminal window from the ‘Shell’ menu or by using the ⌘N (or ⌘T) keyboard shortcut. But in some cases, it can be more useful to use a shell command. New windows created with the keyboard shortcut or from the menu will always have the home.
- Ok, just for (nerd) fun, here’s a Bash shell script that will output a Matrix-ish display in Terminal. I have no idea if it works outside of BSD on OS X, but it has a chance. There’s a much better version of this effect written in C called “cmatrix” that you can install via Homebrew.
Calling Command-Line Tools
In AppleScript, the
do shell script command is used to execute command-line tools. This command is implemented by the Standard Additions scripting addition included with OS X.
In this post, we show you some of the most interesting ways you can customize your Mac’s Terminal windows. Related: How to Open Any Folder from the Mac Terminal. Tweaking the Terminal Theme. Terminal has the built-in ability to theme up your shell windows, although you may not notice at first glance.
The Terminal app in
/Applications/Utilities/ is scriptable and provides another way to execute command-line tools from scripts.
The direct parameter of the
do shell script command is a string containing the shell code you want to execute, as demonstrated in Listing 39-1, which simply lists a directory.
APPLESCRIPTListing 39-1AppleScript: Executing a simple shell command that lists the contents of a directory
do shell script 'ls /Applications/'
Since the direct parameter of
do shell script is a string, you can concatenate it with other strings at run time. Listing 39-2, for example, concatenates a shell command to a previously defined parameter value.
APPLESCRIPTListing 39-2AppleScript: Concatenating a command with a value
set theHostName to 'www.apple.com'
do shell script 'ping -c1 ' & theHostName
The shell uses space characters to separate parameters and gives special meaning to certain punctuation marks, such as
*. To ensure that strings are treated as expected—for example, spaces aren’t seen as delimiters—it’s best to wrap strings in quotes. This process is known as quoting. If your string contains quotes, they must also be escaped (preceded by a
/ character) so they are interpreted as part of the string. Listing 39-3 shows an example of an error occurring as a result of a parameter that contains a space.
APPLESCRIPTListing 39-3AppleScript: An error resulting from a string containing a space
set thePath to '/Library/Application Support/'
do shell script 'ls ' & thePath
--> Result: error 'ls: /Library/Application: No such file or directoryrls: Support: No such file or directory' number 1
The easiest way to quote a string is to use the
quoted form property of the text class, as demonstrated in Listing 39-4. This property returns the string in a form that’s safe from further interpretation by the shell, regardless of its contents.
APPLESCRIPTListing 39-4AppleScript: Quoting a string to prevent errors
set thePath to quoted form of '/Library/Application Support/'
--> Result: '/Library/Application Support/'
do shell script 'ls ' & thePath
For more information about the
do shell script command, see Commands Reference in AppleScript Language Guide and Technical Note TN2065.
Add App To Mac Using Terminal Shell Script Example