user8864088
user8864088

Reputation:

Applescript/Bashscript to activate incognito google chrome running in background

I know how to open google chrome in incongito mode:

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --incognito

Also I found how to close incognito google chrome:

tell application "Google Chrome"
    close (every window whose mode is "incognito")
end tell

Also from this link

tell application "Google Chrome"

    set incognitoIsRunning to the (count of (get every window whose mode is "incognito")) is greater than 0

end tell

if (incognitoIsRunning) then
    say "Shh"
end if

Using above script I tried this:

tell application "Google Chrome"

    set incognitoIsRunning to the (count of (get every window whose mode is "incognito")) is greater than 0

end tell

if (incognitoIsRunning) then
    tell application "Google Chrome" to activate
end if

But this did not activate the incognito google chrome, it is activating the normal mode google chrome.

I am running both normal and incognito chromes. (incognito is playing songs, and working on normal chrome).

How can we activate incognito google chrome when also running normal mode google chrome?

Update Borrowing idea from this link

I got this:

tell application "System Events" to tell process "Google Chrome"
    perform action "AXRaise" of window 2
    set frontmost to true
end tell

But this kills second google chrome, I need to activate the incognito, even though there may be multiple normal google chromes running.

Related links:

How to check is Chrome is running in incognito mode using Applescript?

Applescript - Google Chrome activate a certain window

Upvotes: 1

Views: 543

Answers (1)

CJK
CJK

Reputation: 6092

I'm hoping to have interpreted your situation correctly, having understood the following:

  • You're running a single instance of Google Chrome, which has several open windows, at least one of which is in incognito mode;
  • You wish to switch application focus to Google Chrome, but the methods you have tried typically bring the normal windows into the foreground, leaving the incognito windows at the back;
  • You want the switch to Google Chrome, essentially, to do the opposite, which would be to bring the incognito windows to the front, and leave the normal windows remaining open but at the back.

If I've interpreted you correctly, then:

    tell application "Google Chrome"
        activate
        set index of (every window whose mode is "incognito") to 1
    end tell

System info: AppleScript version: "2.7", system version: "10.13.6"

The activate command does what you would expect, and brings focus to Google Chrome's normal windows. The next line sets the index of each incognito window to 1, displacing the one before to one place behind. The normal windows, thus, end up at the rear as a natural consequence of this process.

The great thing about it is that, quite surprisingly, Google Chrome executes these two commands in a seamless fashion (under regular conditions). So there's no observable flicker or visible rearranging of windows: the incognito window that ends up frontmost simply appears at the front at the same time as Google Chrome comes into the foreground.

I'm not going to guarantee that will be the case across all systems or with a large number of open windows. But, on my pretty low-spec Macbook 12", the switch was fluid when tested with 4 normal windows plus 4 incognito windows.

I think the order of the incognito windows ends up being the reverse of what it was, but if it's important to preserve relative order, then you can simply execute the set index... command twice, which will reverse the reversed order. This, however, does produce the tiniest of visible transitions as we switch from the front incognito window to the rear one.

Upvotes: 1

Related Questions