maulana-y
3/4/2017 - 7:34 AM

Add an icon to your finder toolbar to open iTerm in the current folder.

Add an icon to your finder toolbar to open iTerm in the current folder.


on run {input, parameters}
  tell application "Finder"
    set dir_path to quoted form of (POSIX path of (folder of the front window as alias))
  end tell
  CD_to(dir_path)
end run

on CD_to(theDir)
  tell application "iTerm"
    activate

    try
      set sesh to current session of current terminal
    on error
      set term to (make new terminal)
      tell term
        launch session "Default"
        set sesh to current session
      end tell
    end try

    tell sesh
      write text "cd " & theDir & ";clear;"
    end tell
  end tell
end CD_to

Open iTerm from finder

The code and instructions in this gist are from http://peterdowns.com/posts/open-iterm-finder-service.html. I've had to do this a few times and wanted to distill it the basics.

  1. Open Automator
  2. Create an Application
  3. Choose Actions > Utilities > Run Applescript
  4. Paste the contents of open_in_iterm.app into the window.
  5. Save the script somewhere convenient
  6. Find the script, then drag the script onto the Finder window while holding the command key (or in Yosemite, the command + option keys)
(* 
    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