Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save awksedperl/9ba777980a4c2a9ceb8e2221558686ca to your computer and use it in GitHub Desktop.
Save awksedperl/9ba777980a4c2a9ceb8e2221558686ca to your computer and use it in GitHub Desktop.
"Go Here in iTerm2" handler for BBEdit

“Go Here in iTerm2” handler for BBEdit

This is a little helper script for BBEdit to make View > Go Here in Terminal use iTerm2 instead of Terminal.app. You can't simply give BBEdit iTerm2's bundle ID directly because iTerm2 doesn't respond to the open instruction from BBEdit, for unknown reasons.

It's based on this pastebin which in turn was based on rwilcox/open_here.applescript. It differs from both of these in that it will open a new tab in the frontmost iTerm2 window if there is one, rather than always creating a new window.

You can just run this from the Scripts menu, but if you want to have BBEdit use it for View → Go Here in Terminal:

Open the .scpt file in AppleScript Editor. Run it to check it works, if you want. Then File → Export…, choose File Format "Application", and tick "Run-only".

You'll get an application called “Go here in iTerm2.app”. Put this somewhere suitable. /Applications/Utilities is a good choice.

Then from the command line issue this command:

defaults write com.barebones.bbedit TerminalBundleID -string "com.apple.ScriptEditor.id.Go-here-in-iTerm2"

If you changed the filename, you'll need to replace com.apple.ScriptEditor.id.Go-here-in-iTerm2 with whatever the new bundle ID is.

-- based on http://pastebin.com/MSPZWWAj
-- which was in turn based on https://gist.github.com/2006738
-- but will open a new tab in the current terminal if one is open, rather than always creating a new terminal
tell application "BBEdit" to set theFile to file of document 1
tell application "Finder" to set theFolder to (container of file theFile) as alias
set theUnixPath to POSIX path of theFolder
tell application "iTerm"
if exists current window then
set theTerm to current window
else
set theTerm to (make new tab)
end if
tell theTerm
launch session "Default"
tell the current session
write text "pushd \"" & theUnixPath & "\" && clear"
end tell
end tell
activate
end tell
@awksedperl
Copy link
Author

Replacing references to "terminal(s)" with windows or tabs, since
iTerm2 (3.1.6) doesn't recognize the former term. Also changed
"last session" to "current session", because the former doesn't
work.

Used/tested with macOS High Sierra (10.13.4.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment