Search code examples
stringgmailapplescript

Applescript for turning desktop Gmail link into iOS Gmail app link


I'm looking to build an Applescript that will take a desktop Gmail message link as input, and output a URL that will work on iOS to open the same message in the iOS Gmail app.

Here's a typical Gmail URL:

https://mail.google.com/mail/u/1/?zx=tso6hataagpp#inbox/143c5ddc34313e1f

And here's what that same URL would need to look like to work on iOS:

googlegmail:///cv=143c5ddc34313e1f/accountId=2

A couple notes:

The important part (the thread identifier) is the last part of the string in the original desktop URL. That's what needs to go after cv= in the mobile URL. However because Gmail allows multiple account log-in, we also need to note the account ID number after the /u/, which on the desktop is 1, but for mobile is 2. It looks like the desktop URLs are numbered starting at 0, while the mobile URLs are numbered starting at 1, based on which account you logged into first. So we need to increment the account ID number by 1 for the iOS URL.

Also, I'm not sure what the "?zx=tso6hataagpp" is before #inbox; I find that sometimes my desktop Gmail URLs include that part, other times they don't (but still include #inbox). I don't think it matters though, since the important parts we want are at the end of the string, and the number after the always-consistent "mail.google.com/mail/u/".

Ideally the Applescript would look at the clipboard for a desktop Gmail URL, and if it found one, it would output that same URL, then a line break, then the iOS URL immediately following it, like so:

https://mail.google.com/mail/u/1/?zx=tso6hataagpp#inbox/143c5ddc34313e1f googlegmail:///cv=143c5ddc34313e1f/accountId=2

Any Applescript gurus out there that can show me how to hack this together?


Solution

  • The simplest way I can think of is:

    This gets fixed path components. i.e component 5 and the last item

      --https://mail.google.com/mail/u/1/?zx=tso6hataagpp#inbox/143c5ddc34313e1f
    
    set pathUrl to (the clipboard)
    set pathComponents to words of pathUrl
    if item 1 of pathComponents is "https" and item 2 of pathComponents is "mail.google.com" then
    
        set composed to pathUrl & return & "googlegmail:///cv=" & last item of pathComponents & "/accountId=" & (((item 5 of pathComponents) as number) + 1)
    end if
    

    This makes sure it always get the component ( account) after the "u" regardless of where it is. You could do the same for the message id. by using "inbox"

        set composed to ""
        --set pathUrl to "https://mail.google.com/mail/u/1/?zx=tso6hataagpp#inbox/143c5ddc34313e1f"
        set pathUrl to (the clipboard)
        set pathComponents to words of pathUrl
        if item 1 of pathComponents is "https" and item 2 of pathComponents is "mail.google.com" then
    
            repeat with i from 1 to number of items in pathComponents
                set this_item to item i of pathComponents
                if this_item is "u" then
                    set this_item to item (i + 1) of pathComponents
                    set composed to pathUrl & return & "googlegmail:///cv=" & last item of pathComponents & "/accountId=" & ((this_item as number) + 1)
     exit repeat
                end if
            end repeat
    
        end if
    
      composed