How to get the active window title
Posted: Mon Jan 15, 2024 1:27 pm
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
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
video of working version
https://andypiddock.co.uk/activewindow/ ... -15-22.gif
Hope this is of use to someone.
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())
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
https://andypiddock.co.uk/activewindow/ ... -15-22.gif
Hope this is of use to someone.