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
-----------------------------------------------------------------
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.
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.
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
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 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
Yep; it's dead simple – if you know how. If you don't then it's a headache.
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!
property serviceURL : "https://calendar.google.com/calendar/r"
on run
tell application "Google Chrome"
script
on |λ|(w)
set ts to tabs of w where URL begins with serviceURL
if length of ts > 0 then
{w, item 1 of ts}
else
{}
end if
end |λ|
end script
set winTab to my concatMap(result, windows)
if length of winTab > 0 then
set {oWin, oTab} to winTab
tell application "System Events"
if quit delay ≠ 0 then set quit delay to 0
tell process "Google Chrome" to ¬
perform action "AXRaise" of window (index of oWin)
end tell
set idTab to id of oTab
script
on |λ|(x)
idTab = id of x
end |λ|
end script
tell oWin to set active tab index to |Just| of (my findIndex(result, tabs of oWin))
else
make new tab at end of tabs of front window with properties {URL:serviceURL}
end if
end tell
end run
-- GENERIC FUNCTIONS -------------------------------------------------------------
-- Just :: a -> Just a
on Just(x)
{type:"Maybe", Nothing:false, Just:x}
end Just
-- Nothing :: () -> Nothing
on Nothing()
{type:"Maybe", Nothing:true}
end Nothing
-- concatMap :: (a -> [b]) -> [a] -> [b]
on concatMap(f, xs)
tell mReturn(f)
set lng to length of xs
set acc to {}
repeat with i from 1 to lng
set acc to acc & |λ|(item i of xs, i, xs)
end repeat
end tell
return acc
end concatMap
-- findIndex :: (a -> Bool) -> [a] -> Maybe Int
on findIndex(f, xs)
tell mReturn(f)
set lng to length of xs
repeat with i from 1 to lng
if |λ|(item i of xs) then return Just(i)
end repeat
return Nothing()
end tell
end findIndex
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
if class of f is script then
f
else
script
property |λ| : f
end script
end if
end mReturn
set googleServiceURL to "https://calendar.google.com/calendar/"
tell application "Google Chrome"
set tabList to tabs of windows whose URL starts with googleServiceURL
repeat with i in tabList
if contents of i = {} then
set contents of i to 0
end if
end repeat
set tabList to lists of tabList
set theTab to item 1 of item 1 of tabList
try
theTab / 0
on error errMsg
set AppleScript's text item delimiters to {"window id ", " of application"}
set winID to text item 2 of errMsg
end try
set index of window id winID to 1
end tell
The trouble is that setting the index doesn’t' work properly due to a nearly decade-old bug in the macOS.
In any case this task is not as easy as it should be.
This is awesome Chris. I have one question/request - how can we add/specify the page title to target specific Google accounts. Many of us utilize multiple Gmail and/or GSuite accounts. Each account includes its unique email address in the title.
By adding a title parameter, we could specify activating our Personal vs. Business inbox.