Skip to content

Instantly share code, notes, and snippets.

@anecdata
Last active January 26, 2022 06:38
Show Gist options
  • Save anecdata/0df3d9bf8ddee2e06f51ccbccfa0feb9 to your computer and use it in GitHub Desktop.
Save anecdata/0df3d9bf8ddee2e06f51ccbccfa0feb9 to your computer and use it in GitHub Desktop.
macOS Terminal: open new tab for each serial device, connect with tio, rename the tab to the volume name
import discotool
import subprocess
devicesList = discotool.get_identified_devices(drive_info=True)
for device in devicesList:
if device.repl is None: continue
commands = [
# open a new Terminal tab and connect to the device
["osascript", "-e", 'tell application "Terminal" to activate',],
["osascript", "-e", 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down',],
["osascript", "-e", f'''tell application "Terminal" to do script "tio {device.repl}" in selected tab of the front window''',],
]
if device.volume:
volume_commands = [
# rename the Terminal tab to the volume name [e.g., CIRCUITPY]
["osascript", "-e", 'tell application "System Events" to keystroke "i" using {shift down,command down}',],
["osascript", "-e", 'tell application "System Events" to keystroke Tab',],
["osascript", "-e", f'''tell application "System Events" to keystroke "{device.volume.split("/")[-1]}"''',],
["osascript", "-e", 'tell application "System Events" to key code 53',],
]
commands.extend(volume_commands)
for command in commands:
subprocess.call(command)
@anecdata
Copy link
Author

Thanks, @Neradoc, for discotool and starting code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment