Execute PyAutoGUI code on second MacBook Pro desktop

516 views Asked by At

I have the following code, which matches a screenshot of the "Tech" header/button from the Wall Street Journal's front page, and then clicks on it -

import pyautogui as pya
import time

# timing start
start = time.time()

# center of screen
pya.moveTo(840, 525, 1)

# navigate to "Tech" button
p = pya.locateOnScreen('wsj-tech.png', confidence = 0.8, grayscale=False)
print(p)
x, y = pya.center(p)
x, y = x/2, y/2
d = pya.moveTo(x, y, 1)
pya.doubleClick(d)

# end timer
end = time.time()
print(end - start)

To execute the code, I have to set the IDE (spyder) above (below also works) the browser:

enter image description here

Is there any way I can leave the browser on another desktop? E.g. execute the script in spyder (on "Desktop 2"), and then swipe over to "Desktop 1" to see the autoclicker work on the WSJ page?

FYI the computer is not connected to any external desktops.

enter image description here

1

There are 1 answers

1
yona On BEST ANSWER

Great question! Unfortunately, it is not possible to do what you're asking with pyautogui. This is because pyautogui can only work on foreground objects/windows and not minimized objects/windows in the background or in another workspace. It is a noted feature request, who knows if or when it might get implemented.

Possible Solutions:

  • If you don't need the GUI, just use the python library selenium to find the WSJ banner and click on it with a headless chrome browser instance (browser will be hidden)

  • Have your code & browser run in a virtual machine, so it can then execute without having to be in the foreground

  • Try python library atomacos which provides automation of macOS GUI applications

  • Use a macOS automation tool like Hammerspoon which allows you to move windows, talk to other applications, etc