示例#1
0
def start_listener(port):

    # Launch the app
    AppKit.NSWorkspace.sharedWorkspace().launchApplication_("iTerm2")

    async def main(connection):
        app = await iterm2.async_get_app(connection)
        window = app.current_window
        if window is not None:
            cmd = "nc -lnv {}".format(port)
            await window.async_create_tab(command=cmd)
        else:
            print("No current window")

    iterm2.run_until_complete(main)
示例#2
0
def main(cwd, command):
    """
        Author: Sandro Braun - 2021
        
        create new iterm tab and run command

    Parameters
    ----------
    cwd : [str]
        directory to cd into before running command
    command: [str]
        command to exectue, i.e. the full command will be 'cd $path; command'

    Examples
    --------
        ./iterm.py ~/Desktop vi

    References
    ----------
    * https://iterm2.com/python-api/examples/launch_and_run.html
    * https://raymondjulin.com/blog/exploring-the-iterm2-python-api
    * https://gist.github.com/theRealSuperMario/31f7429d9c805165e1dd30ccbd744ec7
    """
    # Launch the app
    AppKit.NSWorkspace.sharedWorkspace().launchApplication_("iTerm2")

    async def _main(connection):
        app = await iterm2.async_get_app(connection)

        # Foreground the app
        await app.async_activate()

        window = app.current_window
        if window is not None:
            new_tab = await window.async_create_tab()

            cmd = f"/bin/zsh -l -c 'cd {cwd};{command}'\n"

            await new_tab.async_select()
            await new_tab.current_session.async_send_text(cmd)
    iterm2.run_until_complete(_main, True)
示例#3
0
    if window is not None:
        # Run ryanair service
        await window.async_create_tab()
        session = app.current_terminal_window.current_tab.current_session
        await session.async_send_text('cd ~/code/indie-ryanair-service\nys\n')
        time.sleep(timeout)
        change = iterm2.LocalWriteOnlyProfile()
        color = iterm2.Color(204, 0, 126)
        change.set_tab_color(color)
        change.set_use_tab_color(True)
        await session.async_set_profile_properties(change)
        await session.async_set_name("ryanair serv")

        # Run ryanair frontend
        await window.async_create_tab()
        session = app.current_terminal_window.current_tab.current_session
        await session.async_send_text('cd ~/code/indie-ryanair-frontend\nys\n')
        time.sleep(timeout)
        change = iterm2.LocalWriteOnlyProfile()
        color = iterm2.Color(0, 140, 240)
        change.set_tab_color(color)
        change.set_use_tab_color(True)
        await session.async_set_profile_properties(change)
        await session.async_set_name("ryanair front")

    else:
        # You can view this message in the script console.
        print("No current window")

iterm2.run_until_complete(main)
示例#4
0
def main():
    iterm2.run_until_complete(activate)
示例#5
0
# https://iterm2.com/python-api/examples/index.html

import sys, os
import iterm2
import asyncio

from mpiterm2.lib.session import get_session_by_path

PATH = None
if len(sys.argv) >= 2:
    PATH = sys.argv[1]

if PATH == "--help" or PATH == "-h":
    print("USAGE: ./go_to_session.py [PATH]")
    os.exit(1)


async def go_to(connection):
    app = await iterm2.async_get_app(connection)
    if PATH is None:
        session = app.current_terminal_window.current_tab.current_session
    else:
        session = await get_session_by_path(app, PATH.rstrip("/"))

    await session.async_activate(True, True)
    await app.async_activate(True, True)
    print("Done")


iterm2.run_until_complete(go_to)
# brew install python3
# pip3 install --user iterm2
# pip3 install --user pyobjc
# chmod +x iterm_command.py

# import string
import sys

# 2019-May-21: Requires iTerm that has the Python API enabled (beta (v3.3+))

import iterm2
import AppKit

bundle = "com.googlecode.iterm2"
if not AppKit.NSRunningApplication.runningApplicationsWithBundleIdentifier_(bundle):
    AppKit.NSWorkspace.sharedWorkspace().launchApplication_("iTerm")


async def main(connection):
    """Main routine."""
    app = await iterm2.async_get_app(connection)
    await app.async_activate()

    # This will run 'ssh' on macOS with the arguments supplied
    my_cmd = "/usr/bin/ssh " + " ".join(sys.argv[1:])
    # print(my_cmd)
    await iterm2.Window.async_create(connection, command=my_cmd)


iterm2.run_until_complete(main, True)
示例#7
0
#!/usr/bin/env python3.7

import iterm2
# This script was created with the "basic" environment which does not support adding dependencies
# with pip.

async def main(connection):
    # Your code goes here. Here's a bit of example code that adds a tab to the current window:
    app = await iterm2.async_get_app(connection)
    window = app.current_terminal_window
    if window is not None:
        await window.async_create_tab()
    else:
        # You can view this message in the script console.
        print("No current window")

iterm2.run_until_complete(main)
示例#8
0
def main():
    init(sys.argv)
    iterm2.run_until_complete(iTerm)
示例#9
0
        return iterm2.Color(59, 109, 124)
    else:
        return iterm2.Color(114, 114, 124)

async def get_colour_preset(connection, is_dark):
    """Return the dark or light colour preset."""
    if is_dark:
        return await iterm2.ColorPreset.async_get(connection, "Solarized Dark")
    else:
        return await iterm2.ColorPreset.async_get(connection, "Solarized Light")

async def set_colours(connection, preset, ansi_8_color):
    """Set the colour preset for all profiles."""
    profiles = await iterm2.PartialProfile.async_query(connection)
    for partial in profiles:
        profile = await partial.async_get_full_profile()
        await profile.async_set_color_preset(preset)
        await profile.async_set_ansi_8_color(ansi_8_color)

async def switch_now(connection):
    """Set the correct colour preset based on the current theme."""
    app = await iterm2.async_get_app(connection)
    theme = await app.async_get_variable("effectiveTheme")
    is_dark = is_dark_theme(theme)
    preset = await get_colour_preset(connection, is_dark)
    ansi_8_color = get_ansi_8_color(is_dark)
    await set_colours(connection, preset, ansi_8_color)

if __name__ == '__main__':
    iterm2.run_until_complete(switch_now)
示例#10
0
#!/usr/bin/env python3
import iterm2
import os
import subprocess

paths = [
    '~/Projects/Qissa/api-gateway/',
    '~/Projects/Qissa/dataset-handler/',
    '~/Projects/Qissa/qissa-creator/',
    '~/Projects/Qissa/user-control/',
    '~/Projects/Qissa/qissa-ui/',
]


async def tab(connection):
    app = await iterm2.async_get_app(connection)
    window = app.current_terminal_window
    if window is not None:
        await window.async_create_tab()
    else:
        print("No current window")


for x in paths:
    print(x)
    iterm2.run_until_complete(tab)
    os.chdir(x)
示例#11
0
def set_colors(which, restore=False):
    # Set terminal colors.
    #
    # There are several possible terminals I might be in, and the behavior I
    # want is different for each one.
    if os.getenv('VIMRUNTIME'):
        # neovim terminal
        #
        # Do nothing to the terminal, but continue with other changes.
        pass
    elif 'kitty' in os.getenv('TERM'):
        # kitty
        run([
            'kitty',
            '@',
            'set-colors',
            '--configured',
            '--all',
            f'~/.nix-profile/config/kitty/{which}.conf',
        ],
            check=True)
    elif os.getenv('TERM_PROGRAM') == 'iTerm.app':
        # iterm2
        #
        # We don't need to restore on iterm2, because the color is stored in the profile.
        # And every time we change the color, iterm2 pops up a confirmation box, which
        # we don't want on every new tab and window.
        if not restore:

            async def iterm2_set_colors(connection):
                preset = await iterm2.ColorPreset.async_get(
                    connection,
                    f'Solarized {"Dark" if which == "dark" else "Light"}',
                )
                for partial in (await
                                iterm2.PartialProfile.async_query(connection)):
                    if partial.name in ["Default"]:
                        await partial.async_set_color_preset(preset)

            iterm2.run_until_complete(iterm2_set_colors)
    elif os.getenv('SSH_TTY'):
        # Connected over ssh.
        #
        # The terminal is therefore not running on this machine, and we
        # will not try to manipulate its colors.
        #
        # Do nothing to the terminal, but continue with other changes.
        pass
    else:
        print(
            "I don't recognize this terminal, so not trying to change its color.",
            # important to print to stderr, because stdout gets consumed as
            # a shell script
            file=sys.stderr,
        )

    # Set bat config var.
    print(f'export BAT_THEME="Solarized ({which})"')

    # Persist for next time.
    # My vim config also reads this file to determine colors on startup.
    with open(color_config_path, 'w') as f:
        f.write(which)