Google Chrome – How to Bring an Existing GMail Tab to the Front

There is no need to “dis” able all the other macros.

If the hotkey combination is unique, the macro will execute.

If there are two “LIVE/enabled” macros with the same hotkey, Keyboard Maestro will display a small window and ask you which one you want to execute. (This can actually be useful as I have a set of macros I do not use often ALL with the same hotkey combination).

a: Renable all your other macros
b: I assume that you can run Chrome from the dock or the application folder
c: add a display message at the start of the macro to show you are running the macro OK, this is a useful debug technique.
d: create another macro to use openurl with chrome. (I have used CTL+OPT+CMD+G a hot key), change the display prompt
e: add “the status menu is selected” as well as the hot-key as this is another way to check you are activating the macro
(the macro will be listed as a dropdown for the Keyboard Maestro icon in your top menu bar)

The above should show Chrome running on you macbook.
Does this solve your problem? If not, then I still do not understand why you were typing “C”



Hi again!

I think I havn't explained this propperly.

I have a shortcut to open google email. But the problem is that it opens a
new tab every time I use it.
I want it to go to the tab for Mail who is lying there on top together with
google calendar, my disk and so on. Open it again.
Can you give me that?
Geir
[image: Innebygd bilde 1]

You will need to write some AppleScript to ask Chrome for the list of tabs (or even the list of all windows, and then list of all tabs in each window) and find the one that has the right URL. Then if found, bring that to the front and select it, otherwise open a new URL.

Alternative: You could try a Chrome add-on that manages tabs. Your email could always be Tab position 1, and then you could just switch to Chrome and use Keyboard Maestro to select the correct tab.

Thanks!
But I do not know anything about AppleScript.
Geir

Hey Geir,

This was a bit pesky to write due to a bug in OSX affecting changing the index of windows in an application, but it’s working.

The script is run from an ‘Execute AppleScript’ Action in Keyboard Maestro.

It will run faster if you save it as an AppleScript FILE using the Applescript Editor (pre-Yosemite) or the Script Editor (Yosemite) — then run it as a script file instead of as a text-script.

-Chris

-----------------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2015/05/14 12:30
# dMod: 2015/05/14 13:21
# Appl: Google Chrome, System Events
# Task: Bring Chrome Tab with Gmail to front if it exists - otherwise open Gmail in new window.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Google_Chrome, @System_Events, @GMail, @Open, @Switch, @Front
-----------------------------------------------------------------

set _win to false
tell application "Google Chrome"
   set {idList, urlList} to {id, URL} of every tab of every window
end tell

set AppleScript's text item delimiters to return

if (urlList as text) contains "https://mail.google.com/mail/" = true then
   set theWin to 1
   repeat with i in urlList
      set theTab to 1
      repeat with n in i
         if n starts with "https://mail.google.com/mail/" then
            set {_win, _tab} to {theWin, theTab}
         end if
         set theTab to theTab + 1
      end repeat
      set theWin to theWin + 1
   end repeat
end if

if _win ≠ false then
   tell application "System Events"
      if quit delay ≠ 0 then set quit delay to 0
      tell process "Google Chrome"
         perform action "AXRaise" of window _win
      end tell
   end tell
   tell application "Google Chrome"
      tell front window to set active tab index to _tab
   end tell
else
   tell application "Google Chrome"
      set newWin to make new window
      tell newWin to set URL of active tab to "https://mail.google.com/mail/u/0/?tab=wm#inbox"
   end tell
end if

-----------------------------------------------------------------

Tanks a lot to everybody to everybody who cares!
Geir

Thanks @ccstone for this, useful for me too.

Likewise, I just put this in place with a Hot Key and it works really nicely. Thanks very much for contributing the script. Being able to jump instantly to my Gmail tab from anywhere is just wonderful.

Hi !
Im not able to use this because I do nothing about script.
Is there a simple way to do it without script and a hotkey?

Hello Geir,

That's why I posted the script.

You have Keyboard Maestro — yes?

KM knows how to run a script using an 'Execute AppleScript' Action.

No.

You can use a trigger other than a hotkey like a menu trigger.

Here's the script as a downloadable macro with F1 as the hotkey. You can easily change the trigger to suit yourself.

-Chris


Google Chrome { Bring Gmail Tab to Front }.kmmacros (3.7 KB)

YES, YES!
This is the way to do it!
Thanks a lot for your time, Ccstone!

This is a great step forward.

Could it be possible to make this happen also to Google calendar?, Contacts and even Google Disc?

Geir

Hey Geir,

I've been meaning to get back to this. Here's the same macro with the URL in the script altered to work with Google Calendar.

Google Chrome { Bring Google-Calendar Tab to Front }.kmmacros (3.7 KB)

If you want to create macros that manage other Google services all you have to do is find the shortest portion of the URL of the service that is consistent and replace the URL in the script.

I have altered the script, so you only have to enter the URL in 1 place.

-Chris

I already installed chrome and just keep getting the sign in page. I guess I was between facebook and Pinterest more so they dropped my gmail acct. I created a new one but now the say they can’t find it! I’ve created another acct on google and this one is sticking with 2 emails from google. I gave the second email address to a friend who has also left facebook and now I have no way to contact him

Thanks @ccstone This is a very useful function!

1 Like

Hello everyone,

After using Keyboard Maestro for the sole purpose of opening the Finder with a shortcut button for more than a year, I finally started to make use of the combining powers of Alfred and KM, and am blown away by it. I am not a coder but look at optimizing workflows in daily operations for product management.

I stumbled across this one and got it working for my specific case, where I embed the Script in another Macro. However, what do you have to change if you want the new Gmail tab to open in the same window and not in a new one? I tried to steal from other Scripts, but it did not do the trick in my case.

I am sure it is something embarrassingly easy in the last part of the script, where I want to replace the “newWin” for newTab parameters:

tell application "Google Chrome"
   set newTab to make new tab
   tell newTab to set URL of active tab to "NEWURL"
end tell

Thanks a lot!

You don't need a script for this. There are several KM Actions that will do this for your directly.

image

There are identical Actions for Safari, and FrontMost Browser.

Thanks for the fast response! I know about the “new tab/new window” KM actions, but looked at something like that:
Scans Chrome for an open tab with certain values

  • if tab is in place
    –> do x

  • if tab is not in place,
    –> create new tab
    –> open URL in new tab,
    –> do x

Hey @Jarko,

Yep; it’s dead simple – if you know how. If you don’t then it’s a headache.  :sunglasses:

Here’s my script above updated to do what you want.

-Chris

----------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2015/05/14 12:30
# dMod: 2018/03/31 00:39
# Appl: Google Chrome, System Events
# Task: Bring Chrome Tab with Google Calendar to front if it exists - otherwise open Calendar in new tab.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Google_Chrome, @System_Events, @Tab
----------------------------------------------------

set googleServiceURL to "https://calendar.google.com/calendar/"

set _win to false
tell application "Google Chrome"
   set {idList, urlList} to {id, URL} of every tab of every window
end tell

set AppleScript's text item delimiters to return

set urlListText to urlList as text

if urlListText contains googleServiceURL = true then
   set theWin to 1
   repeat with i in urlList
      set theTab to 1
      repeat with n in i
         if n starts with googleServiceURL then
            set {_win, _tab} to {theWin, theTab}
         end if
         set theTab to theTab + 1
      end repeat
      set theWin to theWin + 1
   end repeat
end if

if _win ≠ false then
   tell application "System Events"
      if quit delay ≠ 0 then set quit delay to 0
      tell process "Google Chrome"
         perform action "AXRaise" of window _win
      end tell
   end tell
   tell application "Google Chrome"
      tell front window to set active tab index to _tab
   end tell
else
   tell application "Google Chrome"
      set newWin to make new tab at end of tabs of front window with properties {URL:googleServiceURL}
   end tell
end if

----------------------------------------------------

Chris, this is amazing. I just realized that the googleServiceURL is nothing but a placeholder in your script that is defined by the specific URL. Hence, I can use it for many different cases such as scanning for Jira, Intercom, Product-Tabs that are open, and embed the script in those workflows. Thanks a lot!

1 Like