1

我想使用 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')

并从那里管理事情,但我不知道。

4

1 回答 1

0

您可以使用win.active_tab_index.set(number)来更改活动选项卡。不需要系统事件。但是该tab对象没有对其自己的索引的引用,因此将其更改为此(编辑:重写以避免错误)

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 不维护

于 2014-08-14T17:46:26.330 回答