keyboard – How one can create a brand new iTerm2 with a keystroke and produce simply this window to entrance

[ad_1]

I’ve looked for fairly some time now, I talked to quite a few synthetic intelligences, actual intelligences and I attempted many issues together with apple script and iTerms python API. However it appears unimaginable to me to attain this.

TLDR

I wish to use a keyboard shortcut to open a brand new iTerm (or Terminal) window which has focus with out bringing all the prevailing iTerm (or Terminal) home windows to entrance.

I do not care if this resolution is for Terminal or iTerm2, to be trustworthy, though I appear to love iTerm2.

The lengthy story

Use case

Think about I’ve 5 iTerm home windows and safari open on a single display. Now I rapidly want one other iTerm window to do one thing, however I do not need the opposite iTerm home windows to litter safari, as a result of I wish to have a look on the net web page on the similar time I write into the terminal.

System information

I’m on a Mac M3 professional
macOS Sonoma 14.5
iTerm2 Construct 3.5.1

Present standing

At present I’m utilizing a python script which makes use of iTerm’s python API to create a brand new window. This works. Sadly this window doesn’t come to the entrance and doesn’t have focus. Its all the time second to the at the moment targeted window / app.
I exploit Hammerspoon to run the script.
The script is cluttered, as a result of i attempted totally different resolution.

The script

#!/usr/bin/python3
import iterm2

import subprocess
import asyncio

def focus_window_applescript(window_id):
    script = f"""
    inform utility "iTerm2"
        set id_list to each window's id
        set wnd_id to first merchandise of id_list
        # echo home windows
        set newWindow to (first window whose id is wnd_id)
        choose newWindow
        # activate
    finish inform
    """
    subprocess.run(["osascript", "-e", script])

def focus_window_applescript_2(window_id):
    script = f"""
    inform utility "iTerm2"
        -- Guarantee now we have home windows accessible
        set id_list to each window's id
        set wnd_id to first merchandise of id_list
        set wnd to first window whose id is wnd_id
        set wnd2 to first window
    finish inform
    inform utility "System Occasions"
        inform course of "iTerm2"
            -- Guarantee now we have home windows accessible
            #     set frontmost to true
            set index of wnd to 1
            carry out motion "AXRaise" of first window
        finish inform
    finish inform
    """
    subprocess.run(["osascript", "-e", script])

def focus_window_applescript_3(wnd_id):
    script = f"""
    inform utility "iTerm2"
        # set id_list to each window's id
        # set wnd_id to first merchandise of id_list
        # set wnd to first window whose id is wnd_id
        set wnd to first window
    finish inform

    inform utility "System Occasions"
        inform utility course of "iTerm2"
            # set oWin2 to first window whose title is winTitle
            set targeted of wnd to true
            inform wnd to carry out motion "AXRaise"
        finish inform
    finish inform
    """

async def major(connection):
    app = await iterm2.async_get_app(connection)
    wnd = await app.home windows[-1].async_create(connection)
    await wnd.async_activate()

    print(wnd)

    focus_window_applescript_3(wnd.window_id)

def is_iterm_running():
    strive:
        output = subprocess.check_output(["pgrep", "iTerm2"])
        return bool(output.strip())
    besides subprocess.CalledProcessError:
        return False

def check_focus_and_create_window():
    script = """
    inform utility "System Occasions"
        set frontmostApplication to call of first utility course of whose frontmost is true
    finish inform

    if frontmostApplication is "iTerm2" then
        inform utility "iTerm2"
            create window with default profile
        finish inform
        return "true"
    else
        return "false"
    finish if
    """
    consequence = subprocess.run(["osascript", "-e", script], capture_output=True, textual content=True)
    return consequence.stdout.strip() == "true"

if __name__ == "__main__":
    if check_focus_and_create_window():
        exit(True)
    if not is_iterm_running():
        subprocess.Popen(["/Applications/iTerm.app/Contents/MacOS/iTerm2"])
        # Watch for iTerm2 to start out up
        # for i in vary(5):
        #     print("sleep 1")
        #     sleep(1)  # Modify sleep length if needed
    iterm2.run_until_complete(major)
    exit(True)

# async def major(connection):
#   app = await iterm2.async_get_app(connection)
#   print(app.home windows)
#   await app.home windows[0].async_create(connection)

# iterm2.run_until_complete(major)

I’m new to mac and was utilizing Arch Linux with Gnome earlier than. There it was tremendous straightforward to attain this 🙁

I’m conscious, that there is perhaps a distinct work model to get round my calls for, however adapting the work model woould be the final resort.

Any recommendation is appreciated

Thanks prematurely

[ad_2]

Leave a Reply

Your email address will not be published. Required fields are marked *