iTerm2 version 3 Open iTerm Here Script

by 2/18/2016 01:40:00 PM 11 comments
I just got a notice that iTerm2 has new major upcoming release (v. 3). I upgraded but found that the AppleScript dialect that iTerm used has changed. This change broke my handy little script that launches iTerm from a Finder window. So I took a few minutes to update it. The new code for iTerm2 v3 is below. Just copy and paste it into a new ScriptEditor window and save as iTermHere.scpt. You can then export it as an Application from ScriptEditor and drag it to your Finder toolbar. Here's the code:

(* 

    Open Terminal Here 
     
    A toolbar script for Mac OS X 10.3/10.4
     
    Written by Brian Schlining
 *)


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
 set theCmd to "cd " & thePath & ";clear;"
 
 tell application "iTerm"
  activate
  -- just open a terminal and cd to thePath
  --set newWindow to (create window with default profile)
  
  set aWindow to current window
  
  if aWindow is equal to missing value then
   set aWindow to (create window with default profile)
  else
   tell aWindow
    set t to (create tab with default profile)
   end tell
  end if
  
  
  tell current session of aWindow
   write text "cd " & thePath & ";clear;"
  end tell
  
 end tell
 
 
end process_item

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.

11 comments:

Julien said...

Thank you so much. It works perfectly :)

hohonuuli said...

You're welcome!

hohonuuli said...

Just a note that a friendly developer has packaged this tip up as a TextMate bundle at https://github.com/loadedsith/SourceTree.tmbundle

Mike Furlender said...

I found that this works a bit better (faster):

tell aWindow
set t to (create tab with default profile command "cd " & thePath & ";clear;")
end tell

hohonuuli said...

Did you test your version when iTerm is active but doesn't have any windows open? Also, the version posted is very fast already.

Unknown said...
This comment has been removed by the author.
Unknown said...

It works well on OS X 10.12
Thank your share!
But I do use PathFinder instead of Finder, can you teach me how to modify the script?
Though the PathFinder has its own tool for opening in terminal , its speed is very slow.

andra said...

Than you very much for sharing this! Work for me on Mac High Sierra.

Unknown said...

This worked great! However, to add to your instructions, when dragging the application to Finder, HOLD THE COMMAND KEY before you drag and drop it to Finder. As a new Mac user, this took a while to figure out.

hohonuuli said...

Hi Anthony, Thanks for pointing out my ommision. You can also right click on the toolbar in the Finder and select "Customize Toolbar", then drag the app to the toolbar. I've posted an updated version of this post at https://medium.com/@bschlining/integrate-iterm2-v-3-with-your-macs-finder-f3825acd3e0b

Rajesh said...

thanks for sharing this information
Android Training in Bangalore
Android Training in Btm
informatica Training in BTM
informatica Training in Bangalore
Blue Prism Training in BTM
Blue Prism Training in Bangalore
MERN Stack Training in BTM