Created
November 24, 2019 20:56
-
-
Save isaacl/00d75c66029381f76d3abf6a72697ef3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import iterm2 | |
import AppKit | |
import asyncio | |
# Each command runs in its own tab. | |
commands = [ | |
"echo 1", | |
"echo 2" | |
] | |
# Launch app if needed. | |
assert AppKit.NSWorkspace.sharedWorkspace().launchApplication_("iTerm") | |
async def run_cmd(window, cmd, first): | |
tab = window.tabs[0] if first else await window.async_create_tab() | |
await tab.sessions[0].async_send_text(cmd + '\n') | |
async def main(conn): | |
window = await iterm2.Window.async_create(conn) | |
async_tabs = (run_cmd(window, cmd, idx == 0) for idx, cmd in enumerate(commands)) | |
# Create tabs, send text and bring window to foreground in parallel. | |
await asyncio.gather(window.async_activate(), *async_tabs) | |
# Passing True for the second parameter means keep trying to | |
# connect until the app launches. | |
iterm2.run_until_complete(main, True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment