Skip to content

Instantly share code, notes, and snippets.

@christophervigliotti
Last active January 27, 2022 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christophervigliotti/130f492241e7d314ad334e5477a2143c to your computer and use it in GitHub Desktop.
Save christophervigliotti/130f492241e7d314ad334e5477a2143c to your computer and use it in GitHub Desktop.
Refresh POP Accounts in GMail using Keyboard Maestro

Refresh POP Accounts in GMail using Keyboard Maestro

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.

1. Action: Select Safari Tab

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.

2. Action: Execute Javascript In Safari

This code clicks the "Settings" link...

document.querySelector('[aria-label="Settings"]').click();

3. Action: Pause

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.

4. Action: Execute Javascript In Safari

Click the "See all settings" link document.querySelector('[aria-label="See all settings"]').click();

5. Action: Pause

Set value Pause for to 1 second

6. Action: Execute Javascript In Safari

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();

7. Action: Pause

Set value Pause for to 1 second

8. Action: Execute Javascript In Safari

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();
    }
}

More Info

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment