簡體   English   中英

在Chrome中設置活動標簽並顯示在最前面

[英]Set active tab in Chrome and bring it to the front

我想使用Python和appscript將給定的Google標簽設置為最重要的標簽。 我可以這樣獲取標簽:

from appscript import *
chrome = app('Google Chrome')
tab_title = 'New York Times'
win_tabs = []
for win in chrome.windows.get():
    win_tabs.append([])
    tnames = map(lambda x: x.title(), win.tabs())
    rel_t = [t for t in win.tabs() if tab_title in t.title()]
    if len(rel_t):
        rel_t = rel_t[0]
        break

現在,我想將該選項卡設置為最前面的窗口。 有任何想法嗎? 我懷疑我必須使用類似

se = app('System Events')

從那里進行管理,但我不知道。

您可以使用win.active_tab_index.set(number)更改活動選項卡。 無需系統事件。 但是tab對象沒有對其自身索引的引用,因此將其更改為此(編輯:重寫以避免bug)

def activate_chrome_tab(title):
    for window in chrome.windows.get():
        for index, tab in enumerate(window.tabs()):
            if title in tab.title():
                window.active_tab_index.set(index + 1)
                window.activate()
                window.index.set(1)
                return

activate_chrome_tab(tab_title)

(如果有多個窗口使用相同的標簽名,則它將依次聚焦每個窗口-這就是原始窗口的方式

(來源: Chrome applescript 詞典 )(但注意appscript未維護

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM