Tricks with Terminal.app

Now that applications requiring X11 can now be launched from within the Mac Terminal, it seems like a fine choice to use for all of your terminal needs. If nothing else, it allows you to use “⌘-C” and “⌘-P” to copy and paste text to/from the terminal just as you would expect it to work.

Provided below are a few shell-wrapped AppleScripts that extend the utility of the Terminal for those of us that are used to working with a number of X11 terminal windows open simultaneously. The first, ‘term’, allows you to launch a new terminal window from the command line with the working directory set to the working directory of the first window (i.e. type ‘xterm’ from xterm window). Maybe there’s an built-in way to do this, but I couldn’t find it.

term

#!/bin/sh
#
# Open a new terminal window in the current working directory
#
# https://osxastrotricks.wordpress.com/ (June 23, 2008 )

CWD=`pwd`
osascript<<END
set thePath to "$CWD"
set myPath to (POSIX file thePath as alias)
try
    tell application "Terminal"
        activate
        do script with command "cd \"" & thePath & "\""
        set background color of front window to "white" # {3341,0,32896,1}
        set normal text color of front window to "black"
        set cursor color of front window to "blue" # {52000,52000,52000}
		set font antialiasing of front window to true
    end tell

end try
END

Also provided is a pair of AppleScripts—‘saveTerminals’ and ‘restoreTerminals’—that allow you to save the state (color, size/posizion, CWD, running application if IDL or IRAF) of all of the currently open Terminal windows so you can easily restore the window set, e.g. after logging out or restarting your computer. This is similar to the Window Groups options supported by the Terminal application, however the Window Groups apparently don’t save the working directory or application information.

saveTerminals

#!/bin/sh

# saveTerminals: save current terminal window state for reloading later
#
# This shell/AppleScript cycles through all of your open Terminal.app
# terminals and records their current working directory, color preferences,
# and screen size/position. This information is saved to a file in the home
# directory that can be read by the companion script, 'restoreTerminals', to
# regenerate the current terminal state exactly as it appears on the screen.
#
# Additionally, if either IDL or IRAF are running (idle) in an open terminal,
# the restore script will automatically launch those programs in their current
# working directories.
#
# To install, put the two scripts somewhere in your $PATH and make sure that
# execute permissions are set for the scripts:
#
#     chmod +x saveTerminals restoreTerminals
#
# Usage:
#
#     $ saveTerminals [id_tag]
#
# Adding an optional 'id_tag' at the command line allows you to save multiple window
# sets. For example:
#
#     $ saveTerminals data_reduction
#
#     $ restoreTerminals data_reduction
#
# Note: Tabs not supported
#
# https://osxastrotricks.wordpress.com/ (June 23, 2008 )

pwd > /tmp/pwd.foo

osascript<<END

set useFileName to "${HOME}/.terminals.info.${1}" as text
do shell script "if [ -e " & useFileName & " ]; then rm " & useFileName & "; fi"
set theFile to (POSIX file useFileName)

tell application "Terminal"
	--activate
	set nwin to number of windows

	set wID to 0

	set FP to open for access theFile with write permission

	repeat while (wID  /tmp/pwd.foo"
		-- IDL running
		if proc is "idl" then
			set pwdScript to "spawn,'pwd > /tmp/pwd.foo'"
		end if
		-- IRAF running
		if proc is "x_system.e" then
			set pwdScript to "delete /tmp/pwd.foo; pwd > /tmp/pwd.foo"
		end if

		set pwd to "${HOME}"

		if bus and (proc is not "idl" and proc is not "osascript" and proc is not "x_system.e") then

			try
				display dialog proc & " is running.  Stop it and run 'saveTerminals' again to get working directory of window, " & wID
			end try

		else

				if proc is not "osascript" then
					    do script pwdScript in window wID
					    delay (0.2)
				end if
				set pwdFile to (POSIX file "/tmp/pwd.foo")
				set pwd to read file pwdFile

		end if

		if proc is "osascript" then set proc to "bash"
		if proc is "x_system.e" then set proc to "icl"

		if (wID is 1) then
			set theSettings to {{size_i, pos_i, background color of tab 1 of window wID, normal text color of tab 1 of window wID, bold text color of tab 1 of window wID, cursor color of tab 1 of window wID, font name of tab 1 of window wID, font size of tab 1 of window wID, font antialiasing of tab 1 of window wID, pwd, proc}}
		else
			set theSettings to theSettings }
		end if

	end repeat

	write theSettings to FP
	close access FP

end tell

END

restoreTerminals

#!/bin/sh

# restoreTerminals: load the terminal window state stored by 'saveTerminals'
#
# https://osxastrotricks.wordpress.com/ (June 23, 2008 )

osascript<<END
set useFileName to "${HOME}/.terminals.info.${1}" as text
set theFile to (POSIX file useFileName)

set dat to read file theFile as list

set nwin to number of items in dat

tell application "Terminal"
	set wID to 0
	repeat while (wID < nwin)
		set wID to wID + 1
		set pars to item wID of dat
		do script "cd " & (item 10 of pars)
		set size of window 1 to (item 1 of pars)
		set position of window 1 to (item 2 of pars)
		set background color of window 1 to (item 3 of pars)
		set normal text color of window 1 to (item 4 of pars)
		set bold text color of window 1 to (item 5 of pars)
		set cursor color of window 1 to (item 6 of pars)
		set font name of window 1 to (item 7 of pars)
		set font size of window 1 to (item 8 of pars)
		set font antialiasing of window 1 to (item 9 of pars)
		if (item 11 of pars is not "bash") then
			do script (item 11 of pars) in window 1
		end if
	end repeat

end tell

END
Advertisements
This entry was posted in Tips, Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s