Page 1 of 1

How to get the active window title

Posted: Mon Jan 15, 2024 1:27 pm
by AndyP
Ok, so I needed to be able to determine the active (non Livecode) window in Windows, Livecode has no way of doing this directly...hmmm.

But, you can do this with python using this little script

Code: Select all

import win32gui #part of the pywin library, pip install pywin

def get_active_window_title():
window = win32gui.GetForegroundWindow()
return win32gui.GetWindowText(window)

print(get_active_window_title())
and we can use the shell function in Livecode to get the contents of a print from python.

So putting this all together:

You need 1 button and 1 field "activeWindow" in this example

Code: Select all

on mouseUp pMouseButton
   
   put empty into fld "activeWindow"
   getWindows
   
end mouseUp


command getWindows

   --a way to exit the loop, allways a good idea!
   if the controlKey is down then
      exit to top
   end if
   
   --no ugly console windows
   set the hideConsoleWindows to true
   
   --path to your python file
   put "C:/Users/Andy/activewindow.py" into pythonScripFile 
   
   --shell out to the (cmd) to run the python file
   put shell("python " & pythonScripFile) into tResult
   
   --lets put any results in a field so that we can see it
   put tResult into fld "activeWindow"
   
   --run all of the getWindows handler again until the Ctrl key is pressed
   send getWindows to me in 1 seconds
   
end getWindows
video of working version
https://andypiddock.co.uk/activewindow/ ... -15-22.gif

Hope this is of use to someone.

Re: How to get the active window title

Posted: Tue Jan 23, 2024 10:15 pm
by mtalluto
Very cool! Thanks for sharing.

Re: How to get the active window title

Posted: Wed Jan 24, 2024 10:34 am
by AndyP
As a bonus this will also work with a generated exe file.
Which has the advantage that your user does not have to have python installed and no need for the user to have to be helped with installing the packages which they are sure not to have!

Amended code

Code: Select all

 on mouseUp pMouseButton
   
   getWindows
 
end mouseUp

--runs a compilled python script and returns the results
command getWindows
   
   --a way to escape the loop, always a good idea!
   if the controlKey is down then
      exit to top
   end if
   
   --no ugly console window please
   set the hideConsoleWindows to true
   
   --path to the python exe
   put "C:/Users/Andy/dist/activewindow.exe" into exeFile
   
   --run the exe and return the result
   put shell(exeFile) into tResult
   put tResult && the long date && the long time into fld "activeWindow"
   
   --just a delay loop to allow for constant monitoring of changes
   send getWindows to me in 1 seconds
   
end getWindows
I used PyInstaller to package the python.
https://pyinstaller.org/en/stable/#

This has an option to package the code using the --onefile option, all packages/libraries into a single exe, now that's cool!

Code: Select all

pyinstaller --onefile activewindow.py
Oh, and livecode doesn't care if the resulting file has a .exe suffix, you can change the extension to what you want, Windows (10) seems to be ok with the change as well. I standardize to .lcexec so that I can easily see that this file is to be called from livecode.

I find it easier to use pyinstaller from Visual Studio Code as it packages from the project folder.

Re: How to get the active window title

Posted: Wed Jan 24, 2024 8:51 pm
by mtalluto
You are on fire today Andy!

We have used this technique for auto updating our product's executables on both macOS and Windows. You are right that both systems are ok with so far.