Search code examples
pythonpython-2.7pywinauto

Getting x , y Co-ordinates of the text present on the screen using python


I am doing windows automation with python and trying to right click on an audio file and select the Open with menu item to play the audio file with Windows media player.

For that I am trying to get the text coordinates, and prviding those text coordinates i am planning to click on it as follow.

import pywinauto
import SendKeys

# getting instance of previously opened window 
app = pywinauto.application.Application().window_(title = "My Documents").Wait('visible', timeout=20, retry_interval=0.5)

# focus the existing window
app.SetFocus()

# get the co-ordinates of "audio.mp3"
app.RightClickInput(coords = (x,y))

# get the co-ordinates of "Open with"
app.ClickInput(coords = (x1,y1))

# get the co-ordinates of "Windows Media Player"
app.ClickInput(coords = (x2, y2))

So, how to get the particular text coordinates present on the screen ?

OR

Instead of getting co-ordinates and clicing on them can we do it solely by pywinauto ?


Solution

  • There is a simpler way to do such things without pywinauto or any other GUI automation. Windows Media Player has command line arguments that can be used to open a particular file.

    MSDN article describes the following parameter you probably need:

    "path\filename"
    (For example: wmplayer "c:\filename.wma")
    

    It's easy to run in pure Python by using subprocess.Popen('wmplayer "c:\filename.wma"') call.