Skip to content

Instantly share code, notes, and snippets.

@jamesmacaulay
Created June 23, 2010 14:48
Show Gist options
  • Save jamesmacaulay/450018 to your computer and use it in GitHub Desktop.
Save jamesmacaulay/450018 to your computer and use it in GitHub Desktop.
-- Opens up terminal tabs and runs commands in them.
-- Modify switch_dir and/or tab_commands to taste.
-- If you switch away from Terminal.app in the middle
-- of the script running, you'll (probably) get an error.
-- Based on this script by Matthew Lambie:
-- http://lambie.org/2007/11/03/tabs-in-terminal-using-applescript-on-leopard/
-- Modified by James MacAulay 2009-2010
-- Each element of tab_commands is itself an array of
-- shell commands to run in a single tab:
set switch_dir to "cd shopify"
set tab_commands to {{switch_dir, "./script/server --debugger"}, ¬
{switch_dir, "./script/console"}, ¬
{switch_dir, "gitx .", "mate ."}}
----------------------------------------
on menu_click(mList)
local appName, topMenu, r
-- Validate our input
if mList's length < 3 then error "Menu list is not long enough"
-- Set these variables for clarity and brevity later on
set {appName, topMenu} to (items 1 through 2 of mList)
set r to (items 3 through (mList's length) of mList)
-- This overly-long line calls the menu_recurse function with
-- two arguments: r, and a reference to the top-level menu
tell application "System Events" to my menu_click_recurse(r, ((process appName)'s (menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menu_click
on menu_click_recurse(mList, parentObject)
local f, r
-- `f` = first item, `r` = rest of items
set f to item 1 of mList
if mList's length > 1 then set r to (items 2 through (mList's length) of mList)
-- either actually click the menu item, or recurse again
tell application "System Events"
if mList's length is 1 then
click parentObject's menu item f
else
my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
end if
end tell
end menu_click_recurse
global window_id
global window_name
set window_id to 0
set window_name to ""
-- MAINLINE
tell application "Terminal"
activate
-- make a new window with the execution of a trivial command
do script "clear"
-- load up the window id of the window we just created
set window_id to id of first window whose frontmost is true
repeat with tab_num from 1 to length of tab_commands
if tab_num > 1 then my menu_click({"Terminal", "Shell", "New Tab", "Custom"})
set these_cmds to item (tab_num) of tab_commands
repeat with cmd_num from 1 to length of these_cmds
set cmd to item (cmd_num) of these_cmds
do script cmd in tab (tab_num) of window id window_id of application "Terminal"
end repeat
end repeat
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment