cloudcreatordotio
2/19/2020 - 2:00 AM

Iterm function which will create multiple tabs in a grid or vertical panes and execute the command or commands passed to it. ie. splat "pin

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"

#!/usr/local/bin/ruby
require 'shellwords'

args = ARGV
size = args.size
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|
  command_action = nil
  pane_action = <<-TEXT
                  tell application "iTerm"
                    activate
                    #{tell}
                  end tell
                TEXT
  if command
    command_action = <<-TEXT
                       tell application "iTerm"
                         activate
                         tell the front terminal
                           activate current session
                           tell the current session
                             write text "#{command}"
                           end tell
                         end tell
                       end tell
                     TEXT
  end

  pane_script = Shellwords.escape(pane_action)
  system("osascript -e #{pane_script}")
  if command_action
    command_script = Shellwords.escape(command_action)
    system("osascript -e #{command_script}")
  end
end
# script_builder.call(tab_open, args.shift)
# script_builder.call(v_pane_open, args.shift)
# script_builder.call(h_pane_open, args.shift)
# script_builder.call(go_left_one_pane)
# script_builder.call(h_pane_open, args.shift)
exit 1 if args.empty?
pattern = size % 2 == 0 ? :grid : :lanes
script_builder.call(tab_open, args.shift)
if ( pattern == :grid )
  args.shift((size / 2) -1 ).each do |arg|
    script_builder.call(v_pane_open, arg)
  end
  args.each do |arg|
    script_builder.call(go_left_one_pane) unless size == 2
    script_builder.call(h_pane_open, arg)
  end
else
  args.each do |arg|
    script_builder.call(v_pane_open, arg)
  end
end
# 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
}