2012-04-28 51 views
10

你們知道如何使用Applescript激活第n個谷歌瀏覽器窗口嗎?例如,如果我打開了三個Google Chrome窗口,那麼如何激活第二個?Applescript - 谷歌瀏覽器激活某個窗口

我已經嘗試了許多不同的組合,如:窗口2 告訴標籤3激活

我也注意到:

告訴應用程序「谷歌瀏覽器」 告訴視窗1激活 端告訴

,並

告訴應用程序 「谷歌瀏覽器」 告訴窗口2激活 結束告訴

產生相同的結果,它只激活最後一個窗口 打開,這是一個錯誤嗎?

感謝提前:)

回答

8

TL;博士

使用set index of window <n> to 1不是完全有效的,因爲它沒有真正激活窗口 - 它確實使可見,但。

解決方法(例子假設要激活窗口2):

  • Yar's answer提供了一個務實的解決辦法,但它不是完全清楚爲什麼它的工作原理。但是,它具有而非的優點,要求呼叫應用程序爲authorized for assistive access,與以下解決方案不同。

  • user495470's answer提示在一個強大的和通用的解決方案,也可用於非AppleScriptable應用:

    tell application "System Events" to tell process "Google Chrome" 
        perform action "AXRaise" of window 2 
        set frontmost to true 
    end tell 
    
  • 或者,使用下面定義的AppleScript的處理程序如下:

    • tell application "Google Chrome" to my activateWin(it, window 2)

雖然adayzdone's answer應該工作和幾乎呢,有一個陷阱 - 這可能會或可能不會是一個問題(在Chrome 21.0.1180.89觀察山獅)更新:仍然適用,適用於Chrome在OSX 10.11上爲47.0.2526.106。2]:

雖然該解決方案將顯示期望窗口作爲前窗,Chrome不會治療它作爲前窗如果一個不同的窗口是以前的活動。您可以通過非活動的關閉/分鐘/縮放標題欄按鈕,旁邊複選標記的窗口標題以及諸如Cmd-L之類的鍵盤快捷鍵不適用於所需窗口這一事實來判斷。

如果你的下一個動作是點擊無論如何,這可能不是一個問題,因爲這樣的點擊將完全激活所需的窗口。

否則,你可以使用一個合理穩健的GUI腳本的解決方法(從一個通用的解決方案感激地適應here):

更新:可悲的是,不實際問題激活其索引設置窗口到1似乎影響所有應用程序(在OS X 10.8.3上經驗)。 這是一個通用函數,它可以使用GUI腳本在給定AppleScriptable應用程序中正確激活給定窗口。

# Activates the specified window (w) of the specified AppleScriptable 
# application (a). 
    # Note that both parameters must be *objects*. 
# Example: Activate the window that is currently the 2nd window in Chrome: 
# tell application "Google Chrome" 
#  my activateWin(it, window 2) 
# end tell 
on activateWin(a, w) 
    tell application "System Events" 
     click menu item (name of w) of menu 1 of menu bar item -2 ¬ 
        of menu bar 1 of process (name of a) 
    end tell 
    activate a 
end activateWin 

在一個側面說明,什麼OP嘗試 - 例如,activate window 1 - 似乎在OS X 10.8.3的所有應用程序被破壞,以及 - 而底層應用被激活時,窗口規範。被忽略。

這裏是原來的,更說教代碼

tell application "Google Chrome" 
    # Each window is represented by the title of its active tab 
    # in the "Window" menu. 
    # We use GUI scripting to select the matching "Window" menu item 
    # and thereby properly activate the window of interest. 
    # NOTE: Should there be another window with the exact same title, 
    # the wrong window could be activated. 

    set winOfInterest to window 2 # example 
    set winTitle to name of winOfInterest 

    tell application "System Events" 
     # Note that we must target the *process* here. 
     tell process "Google Chrome" 
      # The app's menu bar. 
      tell menu bar 1 
       # To avoid localization issues, 
       # we target the "Window" menu by position - the next-to-last 
       # rather than by name. 
       click menu item winTitle of menu 1 of menu bar item -2 
      end tell 
     end tell 
    end tell 
    # Finally, activate the app. 
    activate 
end tell 
7

嘗試:

tell application "Google Chrome" 
    activate 
    set index of window 1 to 1 
    delay 3 
    set index of window 2 to 1 
end tell 
+0

哇......這個完美的作品。我認爲這是不可能的:) – m41n1 2012-04-28 23:02:55

2

正如mklement提到,改變指數提高了窗口,但例如鍵盤快捷鍵仍然由先前最前面的窗口登記。

tell application "Google Chrome" 
    set index of window 2 to 1 
end tell 

另一種方法是,告訴系統事件,AXRaise窗口1:

tell application "Google Chrome" 
    set index of window 2 to 1 
end tell 
tell application "System Events" to tell process "Google Chrome" 
    perform action "AXRaise" of window 1 
end tell 
+0

很好 - 「AXRaise」確實是一個有效的解決方案;但是,您可以並且應該使用它_by本身_:'告訴應用程序「系統事件」來告訴進程「Google Chrome」¬ \t執行窗口2的操作「AXRaise」 \t將最前面設置爲true end tell是'足夠。 – mklement0 2015-12-21 04:50:31

1

設置索引,然後打開Chrome。

tell application "Google Chrome" 
    set index of window 2 to 1 
    delay 0.01 
    do shell script "open -a Google\\ Chrome" 
end tell 

Source

+0

+1;奇怪的是,這個_does_工作 - 不像使用看起來相同的'activate it'(或者試圖將過程對象的'frontmost'屬性設置爲'true') - 你能解釋爲什麼嗎? – mklement0 2015-12-21 04:58:59

+1

@ mklement0我無法用令人滿意的方式解釋它。你可以看到,如果沒有shell腳本,窗口焦點會改變,但不會改變firstResponder焦點(文本光標,基本上)......再次運行Chrome會強制它把焦點放在最前面的窗口上。所以這只是解釋發生了什麼,但不是如何,但希望它有幫助。 – 2015-12-21 20:59:27

-1

如果運行此

activate application "Google Chrome" 
tell application "System Events" to keystroke "`" using command down 

它會做整個事情,即使從腳本編輯器其運行。

繼承人另一種方式,這一個已經被其他人只是在更多的步驟闡明:

tell application "System Events" to tell process "Google Chrome" to perform action "AXRaise" of window 2 
activate application "Google Chrome" 

如果youre願意將它保存爲一個應用程序並將其分配給一個熱鍵,你可以這樣做:

tell application "Google Chrome" to set index of window 2 to 1 

雖然我想在這一點上,你可能只是簡單地命令+。除非當然,否則你的applescript會涉及一系列步驟,創建一系列步驟,情況可能如此。如果涉及一系列步驟,則只需將其分配給熱鍵並在Chrome已激活時點擊熱鍵即可(例如第二個代碼示例的第一行),或者在Chrome之後在你的代碼中激活)。

+0

正如我在我的回答中所述,雖然這將顯示指定的窗口,但它不會使其成爲_active_(根據輸入/鍵盤焦點)。 – mklement0 2015-12-21 04:29:03

+0

不,這不是重複,儘管其他人提到了「將窗口2的索引設置爲1」,他們的解釋是3行,我的是1.如果你想要它的重點,它似乎必須是一個額外的線,但仍然只有2行。它的重要而不是多餘的,因爲如果你不知道你可以縮小到一行,你可能會覺得這種語言太奇怪了,不值得試圖去掌握。我也編輯了我原來的東西,現在它的重點。 – pau 2015-12-21 04:47:16

+0

如果您確實認爲將_existing answer_中的3行轉換爲1會增加值,則應在您的答案中指出_。也就是說,您的解決方案嘗試使用稍後添加的附加行是行不通的:'activate application'語句重新激活之前活動的窗口。 – mklement0 2015-12-21 04:53:53

1

除了關於激活所需窗口的解決方案,(至少)新版Chrome還支持Windows的active tab index屬性:

tell window 1 
    set active tab index to 5 
end tell 
相關問題