Open an iTerm tab at the current Finder selection

by 1/04/2007 03:13:00 PM 3 comments

Open an iTerm tab at the current Finder selection

For you do-it-yourselfers, Here's the Applescript for opening an iTerm tab for the current finder window. To use, open Apple's Script Editor, paste it in and press the compile button. Then 'Save As...' an application; I usually save it into ~/Library/Scripts as 'iTermHere'. Drag the saved application onto your finder toolbar and your ready to roll.
(* 

    Open Terminal Here 
     
    A toolbar script for Mac OS X 10.3/10.4
     
    Written by Marc Liyanage 

     
    See http://www.apple.com/applescript/macosx/toolbar_scripts/ for 
    more information about toolbar scripts. 
     
    See http://www.entropy.ch/software/applescript/ for the latest 
    version of this script. 
     
     
    History: 
    22-Feb-2012: No change. But tested with iTerm 2 and it works fine.
    04-Jan-2007: Version 2.2.1 by Brian Schlining. Modified to work with iterm
    11-AUG-2005: Version 2.1.1 by magnamous. minor changes to process_item(this_item)
    21-MAR-2005: Version 2.1 by Will Norris. code cleanup and minor additions 
    18-AUG-2004: Version 2.0 by Allan Marcus. uses posix path    
    30-OCT-2001: Version 1.0, adapted from one of the example toolbar scripts 
    30-OCT-2001: Now handles embedded single quote characters in file names 
    30-OCT-2001: Now handles folders on volumes other than the startup volume 
    30-OCT-2001: Now handles click on icon in top-level (machine) window 
    31-OCT-2001: Now displays a nicer terminal window title, courtesy of Alain Content 
    11-NOV-2001: Now folders within application packages (.app directories) and has a new icon 
    12-NOV-2001: New properties to set terminal columns and rows as the Terminal does not use default settings 
    14-NOV-2001: Major change, now handles 8-bit characters in all shells, and quotes and spaces in tcsh 
    18-NOV-2001: Version 1.1: Rewrite, now uses a temporary file  ~/.OpenTerminalHere to communicate 
    the directory name between AppleScript and the shell because this is much more reliable for 8-bit characters 
     
 *)


property debug : false

-- when the toolbar script icon is clicked 
-- 
on run
 tell application "Finder"
  try
   set this_folder to (the target of the front window) as alias
  on error
   set this_folder to startup disk
  end try
  
  my process_item(this_folder)
  
 end tell
end run


-- This handler processes folders dropped onto the toolbar script icon 
-- 
on open these_items
 repeat with i from 1 to the count of these_items
  set this_item to item i of these_items
  my process_item(this_item)
 end repeat
end open


-- this subroutine processes does the actual work 
-- this version can handle this weirdo case: a folder named "te'st"ö te%s`t"

on process_item(this_item)
 
 set thePath to quoted form of POSIX path of this_item
 
 tell application "iTerm"
  activate
  -- just open a terminal and cd to thePath
  make new terminal
  tell the first terminal
   activate current session
   launch session "Default Session"
   tell the last session
    write text "cd '" & thePath & "'; clear"
   end tell
  end tell
 end tell
 
 
end process_item
This script is based on one that's been circulated around for a while. I found the original script here,

22-Feb-2012 UPDATE: I tested this script with iTerm 2 and it works just fine with it.

hohonuuli

Developer

Cras justo odio, dapibus ac facilisis in, egestas eget quam. Curabitur blandit tempus porttitor. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.

3 comments:

Anonymous said...

Hi,

Good shoot, I like it. The only problem I've found is that when I'm starting iTerm for fist time (via tool bar) it's opening TWO tabs - 1st one is in user's home directory and the 2nd - in desired folder.

Any thoughts?

Regards, Jarek

BillSaysThis said...

jaroslaw: Just leave out the line launch session "Default Session" and that goes away.

ankur said...

Thanks!