How to run macro in the background

I have a very basic macro, which clicks a button on a webpage every thirty seconds. I’m trying to figure out how to leave the macro running all day while getting other things done on my computer? How do I keep it running while I get other things done in other browser tabs and windows?

Fyi I’m using Chrome.

Thanks so much!

I would think you need to use one of the Chrome actions to achieve this.
Which one probably depends on the website.

Remember that Keyboard Maestro drives the user interface. So if you use the mouse click action this takes control of the mouse when it clicks.

Thanks Jimmy. I got the Click Link action working, but unfortunately if I start doing something in a different chrome tab or window it can’t find the link anymore and repeat the action. Any other thoughts on this? Is there any way to get the macro running within a specific tab for Chrome?

Then I think you need to use AppleScript?
And in that regard I can not help.

Sounds like you're clicking the button via the UI. I can't provide the details, but I think the basic approach would be to run a JavaScript (via KM) that calls the click method of the button every 30 sec.

Probably a google search on "JavaScript click button" would turn up some same code"

HTH.

Thanks Michael. Clicking the button with Javascript works, but when I move to a different browser tab or window, it no longer can find the link. Any ideas keeping the macro running on one Chrome tab?

Hey Jeff,

That's because the Keyboard Maestro action operates on the frontmost window/tab.

If you want to do differently you'll have to roll-your-own.

------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2015/05/14 15:27
# dMod: 2016/02/25 16:50
# Appl: Google Chrome
# Task: Execute a JavaScript in the Active Tab of the Front Window
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Google_Chrome, @Execute, @JavaScript
------------------------------------------------------------

set js to "document.title"

tell application "Google Chrome"
  tell front window
    tell (first tab whose title is "Apple")
      execute javascript js
    end tell
  end tell
end tell

------------------------------------------------------------

You can ID the tab by its URL if that's more convenient:

tell (first tab whose URL is "http://www.apple.com/")

-Chris

Hey Jeff,

Whups.

I flubbed that first script.

It’s still looking at only the front window.

This one will find the titled tab in any window.

It does NOT however contain error-handling for cases in which the given title appears more than once.

The JavaScript is an approximation. You’ll have to discover the correct syntax for the link you’re clicking.

-Chris

------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2015/05/14 15:27
# dMod: 2016/02/27 17:29
# Appl: Google Chrome
# Task: Execute a JavaScript in the Named Tab.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Google_Chrome, @Execute, @JavaScript
------------------------------------------------------------

set tabName to "Apple"
set javascriptStr to "document.getElementsByClassName('Your_Class_Name')[0].click()"

tell application "Google Chrome"
  
  set myTab to tabs of windows whose title is tabName
  
  repeat with i in myTab
    if (contents of i) = {} then set contents of i to 0
  end repeat
  
  set myTab to lists of myTab
  
  if length of myTab = 1 then
    set myTab to item 1 of myTab
    if length of myTab = 1 then
      set myTab to item 1 of myTab
      tell myTab to execute javascript javascriptStr
    end if
  end if
end tell

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

"flubbed" -- is that a technical term? :astonished:

This is so close! It’s not working for me though.

I made sure the javascriptStr works on its own, and it does. I also made sure that tabName was correct by copying it from the page source. But when I try to run the full script, nothing’s happening. I’ve tried running it while on an off the tab.

Any ideas why it’s not working?

Hey Jeff,

Nope.

No way to tell without testing, and my JavaScript-Fu is not all that strong yet.

What do you get if you use outerHTML instead of .click():

document.getElementsByClassName('YourClassName')[0].outerHTML

-Chris

Hi, I was looking for how to do this and found a workaround using another app. BetterTouchTool allows you to create a shortcut that sends a specific key combo to a specific application. You could highlight the button you want clicked and then have Keyboard Maestro send the Enter key to that application on a regular interval. It’s a bit of a clunky setup and Keyboard Maestro should really have this “send ui action to application” baked in. Having an action repeat in a background app so you can use your computer seems obviously very useful.