Skip to content

Instantly share code, notes, and snippets.

@earlonrails
Last active February 19, 2020 02:00
Show Gist options
  • Select an option

  • Save earlonrails/2624352 to your computer and use it in GitHub Desktop.

Select an option

Save earlonrails/2624352 to your computer and use it in GitHub Desktop.
Iterm function which will create multiple tabs in a grid or vertical panes and execute the command or commands passed to it. ie. splat "ping google.com" "ping cnn.com" "ping twitter.com" "ping yahoo.com"
# splat will make many terminal tabs with selected hosts connecting
splat(){
if [[ $TERM_PROGRAM = iTerm.app ]];then
ruby ~/Documents/splat.rb "$@"
else
echo "Must be using iTerm for that function!"
fi
}
#!/usr/local/bin/ruby
require 'shellwords'
args = ARGV
num_args = args.size
pattern = num_args % 2 == 0 ? :grid : :lanes
tab_open = 'tell i term application "System Events" to keystroke "t" using {command down}'
v_pane_open = 'tell i term application "System Events" to keystroke "d" using {command down}'
h_pane_open = 'tell i term application "System Events" to keystroke "d" using {command down, shift down}'
go_left_one_pane = 'tell i term application "System Events" to key code 123 using {command down, option down}'
script_builder = Proc.new do |tell, command|
script = Shellwords.escape(<<-TEXT
tell application "iTerm"
activate
#{tell}
tell the front terminal
activate current session
tell the current session
write text "#{command}"
end tell
end tell
end tell
TEXT
)
system("osascript -e #{script}")
end
script_builder.call(tab_open, args[0])
if num_args > 1
vertical_panes = num_args / 2
args[1..-1].each_index do |arg_idx|
arg = args[arg_idx + 1]
if ( pattern == :grid && arg_idx < (vertical_panes - 1 ) ) || pattern == :lanes
script_builder.call(v_pane_open, arg)
else
script_builder.call(go_left_one_pane, arg)
script_builder.call(h_pane_open, arg)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment