
Contents
ttab — Introduction
An OS X CLI for programmatically opening a new terminal tab/window in the standard terminal application, Terminal.app
, optionally with a command to execute and/or a specific title and specific display settings.
Installation
Important: Irrespective of installation method, ttab
needs to be granted access for assistive devices in order to operate, which is a one-time operation that requires administrative privileges.
If you're not prompted on first run and get an error message instead, go to System Preferences > Security & Privacy
, tab Privacy
, select Accessibility
, unlock, and make sure Terminal.app
is in the list on the right and has a checkmark.
For more information, see Apple's support article on the subject
Via the npm registry
With Node.js installed, install via the npm registry (you may have to prepend sudo
):
npm install ttab -g
Manual installation
- Download this
bash
script as ttab
.
- Make it executable with
chmod +x ttab
.
- Move it to a folder in your
$PATH
, such as /usr/local/bin
Examples
# Open a new tab in the current terminal window.
ttab
# Open a new tab in a new terminal window.
ttab -n
# Open a new tab and execute the specified command.
ttab ls -l "$HOME/Library/Application Support"
# Open a new tab, switch to the specified dir., then execute the specified command.
ttab -d ~/Library/Application\ Support ls -1
# Open a new tab with title 'How Green Was My Valley' and settings 'Grass'
ttab -t 'How Green Was My Valley' -s Grass
# Open a new tab and execute the specified script.
ttab /path/to/someScript
# Open a new tab, execute the specified script, then close the tab on termination.
ttab exec /path/to/someScript
# Open a new tab, execute a command, wait for a keypress, then close the tab.
ttab eval "ls \$HOME/Library/Application\ Support; echo Press a key to exit.; read -s -n 1; exit"
Usage
$ ttab --help
SYNOPSIS:
ttab [-w] [-s settings] [-t title] [-g|-G] [-d dir] [command [param1 ...]]
DESCRIPTION:
Opens a new Terminal.app tab and optionally executes a command and assigns settings,
among other options.
Important: Terminal must be allowed assistive access in order for this utility
to work, which requires one-time authorization with administrative privileges.
If you get error messages instead of being prompted, authorize Terminal.app via
System Preferences > Security & Privacy > Privacy > Accessibility.
The new tab will run a login shell (i.e., load the user's shell profile) and by default
inherit the working directory from the parent shell.
-w creates the new tab in a new window rather than in Terminal's front window.
-s specifies the settings (profiles) to apply to the new tab, as defined in Terminal.app's
Preferences > Profiles, such as 'Grass'; settings determine the appearance and behavior
of the new tab; name matching is case-insensitive.
-t specifies a custom title to assign to the new tab; otherwise, if a command is specified,
its first token will become the new tab's title.
-d explicitly specifies a working directory for the new tab; by default, the invoking shell's
working directory is inherited (even if -w is also specified).
-g (back*g*round) causes Terminal not to activate, but within Terminal, the new tab
will become the active tab; useful in scripts that launch other applications and
don't want Terminal to steal focus later.
-G causes Terminal not to activate *and* the active element within Terminal not to change;
i.e., the active window and tab stay the same. If Terminal happens to be active, the new
tab will effectively open in the background.
NOTE: With -g or -G specified, for technical reasons, Terminal or the new tab will still
activate *briefly, temporarily* in most scenarios.
Quoted parameters are handled properly and there's no need to quote the command as a whole,
provided it is a *single* command.
To specify *multiple* commands, use 'eval' followed by a single, *double*-quoted string
in which the commands are separated by ';' Do NOT use backslash-escaped double quotes inside
this string; rather, use single-character backslash-escaping as needed.
Use 'exit' as the last command to automatically close the tab when the command
terminates; precede it with 'read -s -n 1' to wait for a keystroke first.
Alternatively, pass a script name or path; prefix with 'exec' to automatically
close the tab when the script terminates.
EXAMPLES:
ttab -t Green -s Grass # create new tab with title 'Green' using settings 'Grass'
ttab ls -l "$HOME/Library/Application Support"
ttab -d "\~/Library/Application Support" ls -1
ttab eval "ls \$HOME/Library/Application\ Support; echo Press a key to exit.; read -s -n 1; exit"
ttab /path/to/someScript # execute a script and keep the tab open on termination
ttab exec /path/to/someScript # execute a script and close the tab on termination.
License
Copyright (c) 2015 Michael Klement mklement0@gmail.com (http://same2u.net), released under the MIT license.
Acknowledgements
This project gratefully depends on the following open-source components, according to the terms of their respective licenses.
npm dependencies below have optional suffixes denoting the type of dependency; the absence of a suffix denotes a required run-time dependency: (D)
denotes a development-time-only dependency, (O)
an optional dependency, and (P)
a peer dependency.
npm dependencies
Changelog
Versioning complies with semantic versioning (semver).
-
v0.1.4 (2015-06-01):
- [doc] Improved CLI usage help; keywords added to
package.json
.
- [dev]
make browse
now opens the GitHub repo in the default browser.
-
v0.1.3 (2015-06-01):
- [fix] The -g and -G options again correctly do not activate Terminal.app when creating the desired tab.
- [enhancement] Option parsing now accepts option-arguments directly attached to the option.
- [dev] Tests added.
-
v0.1.2 (2015-06-01):
- [doc] Manual-installation link and instructions fixed; examples fixed.
-
v0.1.1 (2015-06-01):
- [doc] README.md improved with respect to manual installation instructions.
-
v0.1.0 (2015-06-01):