0

Say there is a currently open application (like a specific instance of a word document, or a video game) and I want to use a python script to tab into that application. How would I achieve that?

Right now what I have is using pyautogui to click on the taskbar icon, but it occasionally doesn't work (no idea why) and it definitely wont work on different computers with a different coloured taskbar. Plus, I think this solution is a little janky, and I would like to figure out a better one if possible.

Any suggestions?

Nick Lai
  • 3
  • 4
  • Is this for a specific OS or do you want it to be cross-platform? I would recommend adding the appropriate tag. – Oli Mar 28 '21 at 20:00
  • With tab you mean tap? As in script your word processor? It depends on the environment (app, os etc). – Allan Wind Mar 28 '21 at 20:04
  • Tabbing between application windows is interacting with the operating system's GUI, and so what you want to do is platform dependent. Specify your OS in your question to get a useful answer. In Windows you would enumerate the active application windows, looking for the caption or main window class you are interested in, and foreground that window. – BoarGules Mar 28 '21 at 20:24
  • In this post they used a batch file to achieve what you want. Maybe you can invoke it from python. https://stackoverflow.com/a/35990933 (on windows) – K.Cl Mar 28 '21 at 20:25
  • Sorry, I should've added OS to the tabs. Currently I am working on windows, but it would be nice to have it work for multiple platforms. I am happy to implement it seperately. – Nick Lai Mar 28 '21 at 21:30
  • *"a specific instance of a word document, or a video game"* - Those are *vastly* different requirements. Office provides a rich automation interface that operates on the document data itself. Games, on the other hand, frequently reject input that is not the result of human interaction. – IInspectable Mar 29 '21 at 07:17

1 Answers1

1

Simply download this batch file. https://raw.githubusercontent.com/npocmaka/batch.scripts/master/hybrids/jscript/sendKeys.bat

after downloading so, you can execute this batch file using your python as such:

import os
os.system('call sendkeys.bat "window title here" ""')
U13-Forward
  • 69,221
  • 14
  • 89
  • 114