Skip to content

Instantly share code, notes, and snippets.

@apperside
Last active January 10, 2022 14:55
Show Gist options
  • Save apperside/165b091121c8404fcb11f81afa3a6049 to your computer and use it in GitHub Desktop.
Save apperside/165b091121c8404fcb11f81afa3a6049 to your computer and use it in GitHub Desktop.
on alfred_script(q)
tell application "iTerm2" to activate
-- CHANGE THIS VARIABLES ACCORDING TO YOUR NEEDS
-- set to null to use default profile
set iterm_profile_name to null
(*
each of these entries represent a tab slice
each slice has the following properties:
- path: the path which need to be opened
- commands: the list of commands to execute
- vscode: a boolean to indicate if the path should be opened in vscode
*)
set project1_path to "~/path/to/project1"
set project2_path to "~/path/to/project2"
set slice1_1 to { path : project1_path , commands: {"echo command 1", "echo command 2"}}
set slice1_2 to { path : project1_path , commands: {"echo command 3", "echo command 4"}}
set slice2_1 to { path : project2_path , commands: {"echo command 5", "echo command 6"}}
set slice2_2 to { path : project2_path , vscode: "true", commands: {"echo command 7", "echo command 8"}}
-- custom paths to open in vscode (if not specified vscode:"true" in slice)
set vscode_paths to {"/path/to/my.code-workspace"}
(*
this is the final window structure: an array of arrays where:
each top level array represents a column
each nested array represents a row
*)
set screen_columns to {{slice1_1, slice1_2}, {slice2_1, slice2_2}}
tell application "iTerm2"
tell current window
-- create new tab
if( iterm_profile_name is null)
set newTab to (create tab with default profile)
else
set newTab to (create tab with profile iterm_profile_name)
end if
repeat with vscodePath in vscode_paths
tell current session
write text "code " & vscodePath
end tell
end repeat
-- create as many columns as there are columns in screen_columns
repeat with i from 1 to number of items in screen_columns
if i >1
tell current session
split vertically with default profile
end tell
end if
end repeat
tell newTab
set counter to 0
repeat with i from 1 to number of items in screen_columns
set sess to item (i + counter) of sessions
set column to (item i of screen_columns)
tell sess
activate
write text "echo session " & i
repeat with j from 0 to ((number of items in column) - 1)
set row to (item (j+1) of column)
set slice_path to (get path of row)
set slice_commands to get commands of row
try
set shouldOpenInVsCode to (get vscode of row)
if shouldOpenInVsCode = "true"
write text "code " & slice_path
end if
end try
if j > 0
-- splitting the session will alter the sessions index so we need to keep track of how many new seessions we have created
set counter to (counter + 1)
set newSession to (split horizontally with default profile)
tell newSession
activate
write text "cd " & slice_path
repeat with command in slice_commands
write text command
end repeat
end tell
else
write text "cd " & slice_path
repeat with command in slice_commands
write text command
end repeat
end if
end repeat
end tell
end repeat
end tell
end tell
end tell
end alfred_script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment