13

Applescriptでn番目のGoogle Chromeウィンドウをアクティブにする方法を知っていますか? たとえば、3 つの Google Chrome ウィンドウを開いている場合、2 つ目のウィンドウをアクティブにするにはどうすればよいですか?

さまざまな組み合わせを試しました。たとえば、ウィンドウ 2 のタブ 3 にアクティブにするように指示します。

また、次のことに気付きました。

アプリケーション「Google Chrome」に伝える ウィンドウ 1 をアクティブにするように伝える end 伝える

と、

アプリケーション「Google Chrome」に伝える ウィンドウ 2 をアクティブにするように伝える end 伝える

同じ結果を生成します。最後に開いたウィンドウのみをアクティブにします。これはバグですか?

前もって感謝します :)

4

6 に答える 6

16

tl;dr

を使用すると、ウィンドウが実際にアクティブ化されset index of window <n> to 1ないという点で完全には効果的ではありませんが、ウィンドウが表示されます。

回避策(例では、ウィンドウ 2 をアクティブにすることを想定しています):

  • Yar の回答は実用的な回避策を提供しますが、それが機能する理由は完全には明らかではありません。ただし、次のソリューションとは異なり、呼び出し元のアプリケーションが補助アクセスを許可されている必要がないという利点があります。

  • user495470 の回答は、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の答えは機能 するはずであり、ほとんど機能しますが、問題がある場合とそうでない場合があります(Mountain LionのChrome 21.0.1180.89で観察されます)[更新:OSX 10.11.2のChrome 47.0.2526.106の時点でも適用されます] :

ソリューションは目的のウィンドウを前面ウィンドウとして表示しますが、別のウィンドウが以前にアクティブだった場合、Chrome はそれを前面ウィンドウとして扱いません。非アクティブな閉じる/分/ズームのタイトル バー ボタン、チェックマークが横にあるウィンドウ タイトル、および などのキーボード ショートカットCmd-Lが目的のウィンドウに適用されないという事実によってわかります。

次のアクションがとにかくウィンドウのどこかをクリックすることである場合、これは問題ではないかもしれません。そのようなクリックは目的のウィンドウを完全にアクティブにするからです。

それ以外の場合は、かなり堅牢な GUI スクリプトの回避策を採用できます (こちらの一般的なソリューションからありがたいことに適応しています)。

更新:悲しいことに、インデックスを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
于 2012-09-08T05:25:58.343 に答える
8

試す:

tell application "Google Chrome"
    activate
    set index of window 1 to 1
    delay 3
    set index of window 2 to 1
end tell
于 2012-04-28T19:27:52.470 に答える
2

目的のウィンドウのアクティブ化に関する解決策に加えて、(少なくとも) 新しい Chrome バージョンでは、ウィンドウのactive tab indexプロパティもサポートされています。

tell window 1
    set active tab index to 5
end tell
于 2016-01-14T16:28:17.367 に答える
2

インデックスを設定して、Chrome を開きます。

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

ソース

于 2015-12-19T21:57:28.660 に答える
0

これを実行すると

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"

アプリケーションとして保存してホットキーに割り当てたい場合は、次のようにします。

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

その時点で、単に command+` を押すだけでよいと思います。もちろん、おそらくそうであるように、アップルスクリプトには一連のステップを作成する一連のステップが含まれます。一連の手順が含まれる場合は、2 番目のコード サンプルの最初の行のようなもので十分です。ただし、ホットキーに割り当てて、chrome が既にアクティブになっているときにホットキーを押します (または、行が chrome の後に来る場合)。コードでアクティブ化されます)。

于 2015-12-21T04:19:11.300 に答える