Gmail - Compose Macro

Hey Folks,

This updated AppleScript will reveal the Gmail tab in Chrome if it is already open and then open a compose panel – otherwise a new window with a Gmail compose panel will be opened.

-Chris

---------------------------------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2015/05/14 12:30
# dMod: 2016/06/01 17:23
# Appl: Google Chrome
# Task: Bring Chrome Tab with Gmail to front if it exists - otherwise open Gmail in new window a.
#     : Then open a compose panel.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Google_Chrome, @System_Events, @Gmail, @Compose
---------------------------------------------------------------------------------
set gmailComposeURL to "https://mail.google.com/mail/u/0/#inbox?compose=new"
set gmailInboxURL to "https://mail.google.com/mail/u/0/?tab=wm#inbox"
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 linefeed

if (urlList as text) contains "https://mail.google.com/mail/" = true then
  set theWin to 1
  repeat with urlSubList in urlList
    set theTab to 1
    repeat with theURL in urlSubList
      if theURL 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
      set active tab index to _tab
      set URL of active tab to gmailComposeURL
    end tell
  end tell
else
  tell application "Google Chrome"
    set newWin to make new window
    tell newWin to set URL of active tab to gmailComposeURL
  end tell
end if

---------------------------------------------------------------------------------
2 Likes