PyObjc/AppKit在不窃取焦点的情况下启动Mac应用程序

2024-05-17 13:25:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我不确定我想做的是否可能。我正在使用PyObjc和iterm2api创建一个拆分并发送一些文本。工作得很好,但是如果用户正在做其他事情,我希望它不会窃取焦点;i、 e.如果iTerm不在前台,就不要把它放在前台

我现在拥有的(也是我有史以来第一个Python程序-be kind):

#!/usr/bin/env python3

'''Upgrade Homebew Casks in a new iTerm2 vertical split'''

import os
import iterm2
import osascript
import AppKit

def get_term_cookie():
    '''
    Launch iTerm and get a security token.

    Get a token from iTerm and store in $ITERM_COOKIE env variable.
    This is required to run iterm API's without a "Do you really trust this program?"
    dialog box from being presented.

    See https://gitlab.com/gnachman/iterm2/-/wikis/iTerm2-Version-3.3-Security-Updates
    '''

    iterm = AppKit.NSWorkspace.sharedWorkspace().launchApplication_("iTerm")
    if not iterm:
        print("Failed to launch iTerm2")
        return False

    ret_code,os.environ["ITERM2_COOKIE"],err = (
        osascript.run('tell application "iTerm2" to request cookie'))

    if os.environ["ITERM2_COOKIE"]:
        return True
    else:
        print("Failed to get a cookie from iTerm")
        print("ret_code: " + ret_code)
        print ("err: " + err)
        return False

async def upgrade_brew_casks(connection):
    '''Connect to iTerm2 and upgrade brew casks.'''

    # get a connection to iTerm app
    app = await iterm2.async_get_app(connection)

    # get the current session and make a new vertical split
    active_session = app.current_terminal_window.current_tab.current_session
    await active_session.async_split_pane(vertical=True)

    # run the cask upgrade in the new split
    new_session = app.current_terminal_window.current_tab.current_session
    await new_session.async_send_text('brew cu -ay --no-brew-update\n')

    # return focus to the original split
    await active_session.async_activate()

    return True

if __name__ == '__main__':
    if get_term_cookie():
        # Passing True for the second parameter means keep trying to
        # connect until the app launches.
        iterm2.run_until_complete(upgrade_brew_casks, True)

Tags: thetoimporttrueappnewgetasync