github twitter email rss
Finder and iTerm working together
2013 Jun 11
3 minutes read

If you want button on Finder’s toolbar to open iTerm2, here is app to do that. Just drag it into the toolbar of any Finder window.

When activating it, it will open a new session. If iTerm2 is already open it always opens the new tabs at the beginning. After that it goes to the path of the selected folder or file in finder.

You can make that app by self:

  1. Open «AppleScript Editor» /Applications/Utilities
  2. In a new document, paste the AppleScript code you’ll find below
  3. Save the script in /Applications/Utilities, name it cd to, use the file format «Application» (do not select «run only» if you want to be able to change the script later on)
  4. Give the script a nice icon if you want
  5. Drag the script you saved in /Applications/Utilities into the toolbar of any Finder window

-- Script was opened by click in toolbar (file or folder selected optional)
on run
        tell application "Finder"
                        set thePath to selection as alias
                on error
                                set thePath to folder of the front window as string
                        end try
                end try
        end tell
                set thePath to thePath as string
        on error
                set thePath to folder of the desktop as string
        end try
        CD_to(thePath, false)
end run

-- Script run by draging file/folder to icon
on open (theList)
        set newWindow to false
        repeat with thePath in theList
                CD_to(thePath, newWindow)
                set newWindow to true -- create window for any other files/folders
        end repeat
end open

-- cd to the desired directory in iterm
on CD_to(thePath, newWindow)
        if not newWindow then
                -- Find out if iTerm (ITRM) is running...
                tell application "System Events"
                        set appInstances to count (every process whose creator type is "ITRM")
                        if appInstances > 0 then set newWindow to true
                end tell
        end if
        -- Get the folder of the file or folder alias:
        if not (thePath ends with ":") then
                set x to the offset of ":" in (the reverse of every character of thePath) as string
                set thePath to (characters 1 thru -(x) of thePath) as string
        end if
        -- Convert to POSIX path:
        set thePath to quoted form of POSIX path of thePath as string
        -- Now open the iTerm:
        tell application "iTerm"
                tell last terminal
                        if newWindow then
                                -- set mysession to (launch session "Default")
                                set mysession to (make new session at the beginning of sessions)
                                tell mysession
                                        set name to "Default"
                                        -- execute a command
                                        exec command "/bin/bash -l"
                                end tell
                                set mysession to last session
                        end if
                        tell mysession
                                -- cd to the finder window
                                write text "cd " & thePath
                        end tell
                end tell
        end tell
end CD_to

Back to posts

comments powered by Disqus