Ahoy Mac fans! One really great tool for automating repetitive tasks on my computer is Keyboard Mastro. I normally use it to automate inputting text (things like email signatures and addresses) but today I dug a bit deeper and in an effort to automate the tedious task of clicking on a series of links that I have to click each time that I want to fetch new POP email from my external accounts.
Before you dive in note that this will only work if you configure Safari to allow remote automation.
Set value Select tab
to 1
I have Gmail parked in my first tab of Safari so I use this action. You can replace this step with the "Set Safari URL" action.
This code clicks the "Settings" link...
document.querySelector('[aria-label="Settings"]').click();
Set value Pause for
to 1
second
This is not my preferred way to author browser automation. When using proper browser automation tools you often have access to code that will allow you to monitor the page for state changes before proceeding. We don't have that in Keyboard Maestro so we will have to settle for pausing.
Click the "See all settings" link document.querySelector('[aria-label="See all settings"]').click();
Set value Pause for
to 1
second
Click on the "Accounts and Import" link...
var xpath = "//a[text()='Accounts and Import']";
document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click();
Set value Pause for
to 1
second
Last we get all <span>
tags that are currently visable, loop through them all and if they are "Check mail now" links we click on them.
var links = document.getElementsByTagName("span");
for (var i = 0; i < links.length; i++) {
console.log(links[i].innerText);
if (links[i].innerText === "Check mail now"){
links[i].click();
}
}
- You can get Keyboard Maestro at https://www.keyboardmaestro.com/main/