Skip to content

Instantly share code, notes, and snippets.

@isaac-ped
Created October 24, 2017 21:34
Show Gist options
  • Save isaac-ped/e0b3d5f921cf73d7a83e2991eed58638 to your computer and use it in GitHub Desktop.
Save isaac-ped/e0b3d5f921cf73d7a83e2991eed58638 to your computer and use it in GitHub Desktop.
Launch split iTerm windows
on readFile( unixPath )
return (do shell script "cat '" & unixPath & "'")
end
on getSess(sessions_, names, name)
repeat with i from 1 to count of names
repeat with j from 1 to count of (item i of names)
if item j of (item i of names) equals name
return item j of (item i of sessions_)
end if
end repeat
end repeat
display dialog("fail.")
end
tell application "iTerm2"
set my_path to POSIX path of ((path to me as text) & "::")
set f to (choose file of type "cfg" default location POSIX file my_path)
set cfg_txt to my readFile(POSIX path of f)
set cfg_list to every paragraph of cfg_txt
set layout to {}
set vars to {}
set cmds to {}
set mode to "LAYOUT"
repeat with cfg_line in cfg_list
if (cfg_line as string ) is equal to "--"
if mode is "LAYOUT"
set mode to "VARS"
else if mode is "VARS"
set mode to "CODE"
end if
else
if mode is "LAYOUT"
set split_layout to every word of cfg_line
copy split_layout to end of layout
end if
if mode is "VARS"
if (text item 1 of (cfg_line as string)) is equal to "$"
set passwd to text returned of (display dialog cfg_line default answer "" with hidden answer)
set vars to vars & {passwd}
else if (text item 1 of (cfg_line as string)) is "&"
set var to text returned of (display dialog cfg_line default answer "")
set vars to vars & {var}
else
set vars to vars & {cfg_line}
end
end if
if mode is "CODE"
set AppleScript's text item delimiters to " "
set cmds to cmds & {cfg_line's text items}
set AppleScript's text item delimiters to {""}
end if
end if
end repeat
set names to {}
set sessions_ to {}
create window with default profile
set r_i to 1
set i to 1
repeat with row in layout
set names to names & {{}}
set sessions_ to sessions_ & {{}}
set curr_ses to (session i of current tab of current window)
set (item r_i of names) to (item r_i of names) & { item 1 of row }
set (item r_i of sessions_) to (item r_i of sessions_) & {curr_ses}
if i does not equal count of layout
tell curr_ses
split horizontally with same profile
end tell
end if
tell curr_ses
select
set name to item 1 of row
end tell
set r_i to r_i + 1
set i to i + 1
end repeat
set r_i to 1
repeat with row_ in layout
set c_i to 1
repeat with col in row_
set curr_ses to (item c_i of (item r_i of sessions_))
tell curr_ses
set name to item c_i of (item r_i of layout)
end tell
if c_i does not equal count of row_
tell curr_ses
select
split vertically with same profile
end tell
set next_ses to 0
repeat while next_ses = 0
repeat with j from 1 to i
set sess to session j of current tab of current window
if (name of sess as string) equals "bash"
set next_ses to sess
end
end
end
set i to i + 1
set (item r_i of names) to (item r_i of names) & {item (c_i + 1) of row_}
set (item r_i of sessions_) to (item r_i of sessions_) & {next_ses}
end
set c_i to c_i + 1
end
set r_i to r_i + 1
end
set total to i - 1
repeat with cmd in cmds
set w_i to 1
set name_ to item 1 of cmd
set sess to 0
if (text item 1 of name_) equals "!"
delay (text item 2 of name_ as integer)
else
if name_ does not equal "*"
set sess to my getSess(sessions_, names, name_)
if sess = 0
display dialog("ERROR")
end
end if
set text_cmd to ""
repeat with i from 2 to count of cmd
set w to item i of cmd
if (text item 1 of w) is "$"
set w to item ((text item 2 of w) as integer) of vars
end
set text_cmd to text_cmd & w
if i does not equal count of cmd
set text_cmd to text_cmd & " "
end if
end repeat
if name_ does not equal "*"
tell sess
write text text_cmd
end tell
else
repeat with i from 1 to total
tell session i of current tab of current window
write text text_cmd
end tell
end repeat
end if
end if
end repeat
end tell
term_A B
C D terminal_E
F
--
THINGS
&this will be prompted for
$this will be prompted for with hidden characters
--
term_A echo "Hello!"
B echo "Goodbye!"
!1
* echo "What's up"
!1
F echo $1
D echo $2
terminal_E echo $3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment