Created
January 5, 2010 00:30
-
-
Save matthewmccullough/269022 to your computer and use it in GitHub Desktop.
AppleScript to open a Terminal tab at the specified path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
----------------------------------------------------------------------- | |
-- A Script that opens a specified command line folder name in a new | |
-- terminal tab. | |
-- | |
-- Composed by Matthew McCullough, 2010 | |
-- MIT License. Share freely. | |
----------------------------------------------------------------------- | |
--Reference for tab opening | |
-- http://lambie.org/2007/11/03/tabs-in-terminal-using-applescript-on-leopard/ | |
--Reference for other scripting approaches with Terminal | |
-- http://www.entropy.ch/software/applescript/ | |
-- http://forums.macosxhints.com/showthread.php?p=426240#post426240 | |
-- http://octidextro.us/2008/01/11/openterminalhereapp-update/ | |
----------------------------------------------------------------------- | |
on run argv | |
--log "Item 1: " & item 1 of argv -- Folder path | |
set folderName to item 1 of argv -- Folder path | |
log "Folder Name: " & folderName | |
tell application "Terminal" | |
activate | |
set window_id to id of first window whose frontmost is true | |
--repeat with i from 1 to 2 | |
--Two possible approaches for sending the New Tab command. | |
--First is via a menu click, but requires knowing the New Tab type Name | |
--Second just sends the keystroke and accepts the default user's tab style | |
--my menu_click({"Terminal", "Shell", "New Tab", "Basic"}) | |
tell application "System Events" | |
keystroke "t" using {command down} | |
end tell | |
set commandToRun to "cd " & quoted form of (folderName as string) --Quoted to handle spaces in foldernames | |
--set tabNum to i + 1 | |
--set tab_id to index of first tab of first window | |
--log "Tab id: " & tab_id | |
--do script commandToRun in tab tabNum of window id window_id of application "Terminal" | |
do script commandToRun in window id window_id of application "Terminal" | |
do script "clear;pwd" in window id window_id of application "Terminal" | |
--end repeat | |
end tell | |
end run | |
-- `menu_click`, by Jacob Rus, September 2006 | |
-- http://www.macosxhints.com/article.php?story=20060921045743404 | |
-- | |
-- Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}` | |
-- Execute the specified menu item. In this case, assuming the Finder | |
-- is the active application, arranging the frontmost folder by date. | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ -z $1 ]; then | |
echo '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' | |
echo 'Launch a terminal window tab and CD to the specified location' | |
echo 'USAGE:' | |
echo 'terminal.sh <desiredpath>' | |
echo 'Param1 = path to CD to' | |
echo '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' | |
exit | |
fi | |
#================================================= | |
# bash - find path to currently running script | |
# which is necessary because we need to find our | |
# adjacent terminal.scpt file | |
#================================================= | |
abspath="$(cd "${0%/*}" 2>/dev/null; echo "$PWD"/"${0##*/}")" | |
# to get the path only - not the script name - add | |
path_only=`dirname "$abspath"` | |
#Pass in the folder you want to CD to | |
echo Opening a terminal tab to \'$1\' | |
osascript "$path_only/terminal.scpt" "$1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment