1

I am using pyautogui to do some things in a window but I'm using mouse coordinates for it but I have a problem when the window starts minimized because then the script does things to the applications behind it but if I add code to maximize it (holding alt then spacebar then x) then if it starts maximized it minimizes it and does things it's not meant to anyway.

I am trying to make a script that you give it a search term or a date it will search in dropbox, onedrive and google drive at the same time. Code:

Sorry, the code is a bit of a mouthful

import pyautogui as gui


def getPos():
    gui.sleep(3)
    print(gui.position())


def dropbox(term):
    gui.moveTo(25, 1050)
    gui.sleep(0.5)
    gui.click()
    gui.sleep(0.5)
    gui.typewrite("google chrome")
    gui.sleep(0.5)
    gui.press("enter")
    gui.sleep(0.5)
    gui.keyDown("alt")
    gui.press(" ")
    gui.press("x")
    gui.keyUp("alt")
    gui.sleep(0.5)
    gui.moveTo(960, 50)
    gui.sleep(0.5)
    gui.typewrite("dropbox.com")
    gui.sleep(0.5)
    gui.press("enter")
    if term != "i":
        gui.sleep(1)
        gui.moveTo(1400, 95)
        gui.sleep(1)
        gui.typewrite(term)
        gui.sleep(0.5)
        gui.press("enter")


def drive(term):
    gui.moveTo(25, 1050)
    gui.sleep(0.5)
    gui.click()
    gui.sleep(0.5)
    gui.typewrite("google chrome")
    gui.sleep(0.5)
    gui.press("enter")
    gui.sleep(0.5)
    gui.keyDown("alt")
    gui.press(" ")
    gui.press("x")
    gui.keyUp("alt")
    gui.sleep(0.5)
    gui.moveTo(960, 50)
    gui.sleep(0.5)
    gui.typewrite("drive.google.com")
    gui.sleep(0.5)
    gui.press("enter")


def onedrive(term):
    gui.moveTo(25, 1050)
    gui.sleep(0.5)
    gui.click()
    gui.sleep(0.5)
    gui.typewrite("google chrome")
    gui.sleep(0.5)
    gui.press("enter")
    gui.sleep(0.5)
    gui.keyDown("alt")
    gui.press(" ")
    gui.press("x")
    gui.keyUp("alt")
    gui.sleep(0.5)
    gui.moveTo(960, 50)
    gui.sleep(0.5)
    gui.typewrite("onedrive.com")
    gui.sleep(0.5)
    gui.press("enter")


def openAll(term):
    drive(term)
    gui.sleep(0.5)
    gui.moveTo(268, 17)
    gui.click()
    gui.moveTo(960, 50)
    gui.sleep(0.5)
    gui.typewrite("onedrive.com")
    gui.sleep(0.5)
    gui.press("enter")
    gui.sleep(0.5)
    gui.moveTo(960, 50)
    gui.sleep(0.5)
    gui.moveTo(511, 14)
    gui.sleep(0.5)
    gui.click()
    gui.sleep(0.5)
    gui.moveTo(960, 50)
    gui.sleep(0.5)
    gui.typewrite("dropbox.com")
    gui.sleep(0.5)
    gui.press("enter")


print("1 = Google Drive, 2 = OneDrive, 3 = Dropbox, 4 = All of the above")
gui.sleep(0.5)
service = input("Which Service? ")
term = input("Enter a search term or enter 'i' for none ")

if service == "1":
    print("Opening: Google Drive")
    gui.sleep(1)
    drive(term)

elif service == "2":
    print("Opening: OneDrive")
    gui.sleep(1)
    onedrive(term)

elif service == "3":
    print("Opening: Dropbox")
    gui.sleep(1)
    dropbox(term)

elif service == "4":
    print("Opening: Google Drive, OneDrive and Dropbox")
    gui.sleep(1)
    openAll(term)

iamdeedz
  • 109
  • 8
  • 1
    could you provide a [mcve] code showing your issue? – Pac0 Nov 24 '22 at 21:16
  • One minute please – iamdeedz Nov 24 '22 at 21:19
  • I didn't know how much of it to post so I posted it all. sorry if it's too much – iamdeedz Nov 24 '22 at 21:23
  • My recommendation would be to remove bits until your problem doesn't happen anymore. It will help readers (and yourself!) to understand your code and the problem, increasing the chance to get better answers. – Pac0 Nov 24 '22 at 21:32
  • The thing is I don't think removing things from my code will fix it because the if window starts maximized my code will minimize it which will break it but if I remove the code that does this then if it starts minimized it it will break anyway so I don't know what to do – iamdeedz Nov 24 '22 at 21:35
  • Do you know anyway to check if a window is maximized as that would solve my issue? – iamdeedz Nov 24 '22 at 21:35
  • And I don't mean that my code isn't the problem saying that I could never make a mistake in this which is definitely not true – iamdeedz Nov 24 '22 at 21:38
  • 2
    Look at this Q&A: https://stackoverflow.com/questions/43785927/python-pyautogui-window-handle , this looks like a "cleaner" (more robust, less hardcoded coordinates) approach to handle the windows. – Pac0 Nov 24 '22 at 21:38
  • I guess we probably need to know which OS you use (Windows, MacOS, Linux...) – Pac0 Nov 24 '22 at 21:41
  • Oh yeah sorry. I'm on windows 10 although I think the Q&A you sent has solved it. Thanks so much! – iamdeedz Nov 24 '22 at 21:48
  • Yep! the Q&A solved it. Thank you so much! – iamdeedz Nov 24 '22 at 21:50
  • (above message is automated as I voted to close the question a with the other Q&A as answer) – Pac0 Nov 25 '22 at 06:48

0 Answers0