AppleScript and Gmail: How to Open Emails and Click Links

NEW! AppleScript Maker Beta: Hey Everyone, I am working on a new AppleScript tool that is going to BLOW YOUR MIND! After reading this tutorial use this tool to easily build your AppleScripts. Click Here!

I’ve been asked by a few people recently how to automate their responses to automated emails. I set up this code a little while back originally to work with an auto posting script I created to post to Craigslist. ( I wasn’t spamming I was renting an apartment)

This searches Gmail for an email, or subject line. If your search term matches then it opens the email.

After the email is open it will look for a link that you specify. For example if you are looking to click on something that says “Click here to confirm your post”, you could search for “click here” and it will click on that link.

Also, this is slightly advanced, and should be used in conjunction with my other AppleScript tutorials . If you have not already please check those out first.

to clicktagName(thetagName, elementnum)

tell application "Safari"

do JavaScript "document.getElementsByTagName('" & thetagName & "')[" & elementnum & "].click();" in document 1

end tell

end clicktagName



to getInputBytagName(thetag, num) -- defines a function with two inputs, thetag and num

tell application "Safari" --tells AS that we are going to use Safari

set input to do JavaScript "

document.getElementsByTagName('" & thetag & "')[" & num & "].innerHTML;" in document 1 -- uses JavaScript to set the variable input to the information we want

end tell

return input --tells the function to return the value of the variable input

end getInputBytagName





to clicktag(tagName, num)

tell application "Safari"

set input to do JavaScript "

var tagName = '" & tagName & "';

var num = '" & num & "';

var x = document.getElementsByTagName(tagName)[num].click();" in document 1

end tell

end clicktag





-- this searches for the email in Gmail

set x to 0 -- start number

set tagName to "span"

set searchTerm to "example@theDomainName.com" -- you have to change this to something that identifies the email you are looking for.
repeat 1000 times -- how many time you want to repeat this

set trialInnerHTML to getInputBytagName(tagName, x) -- pulls content from "a" tags

if trialInnerHTML = searchTerm then

clicktag(tagName, x)

exit repeat

end if

set x to x + 1 -- goes to the next "a" tag

end repeat

delay 5

-- this searches the email for the link

set x to 0 -- start number

set tagName to "a"

set searchTerm to "add your name to this Availability Request"

repeat 500 times -- how many time you want to repeat this

set trialInnerHTML to getInputBytagName(tagName, x) -- pulls content from "a" tags

if trialInnerHTML = searchTerm then

clicktag(tagName, x)

exit repeat

end if

set x to x + 1 -- goes to the next "a" tag

end repeat

Samuel

8 Responses to “AppleScript and Gmail: How to Open Emails and Click Links

  • This is great thanks!

  • This is great thanks!

  • Hi, thanks for all the examples and explanations. I’ve got a similar problem that I am STRUGGLING to solve. I receive a lot of Gmail notifications from google, as people set up new accounts and choose mine as their secondary account. I can’t keep up with all the mail. i’d like to make a script to automatically parse the emails from google, find the relevant link and click on the submit button. I Didn’t quite understand this script – does it iterate trough all the elements in gmail to find the search term? perhaps you could explain it more thoroughly? thanks a lot, you’re doing quite a nice job here.

    • Hey Jeferson! Thanks, yes it iterates over the page to find the search term and then clicks on it. The search term it is assuming is a link to open the email so when it finds it, the script clicks it.

  • Hi, thanks for all the examples and explanations. I’ve got a similar problem that I am STRUGGLING to solve. I receive a lot of Gmail notifications from google, as people set up new accounts and choose mine as their secondary account. I can’t keep up with all the mail. i’d like to make a script to automatically parse the emails from google, find the relevant link and click on the submit button. I Didn’t quite understand this script – does it iterate trough all the elements in gmail to find the search term? perhaps you could explain it more thoroughly? thanks a lot, you’re doing quite a nice job here.

    • Hey Jeferson! Thanks, yes it iterates over the page to find the search term and then clicks on it. The search term it is assuming is a link to open the email so when it finds it, the script clicks it.

Leave a Reply Text

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.