How to get the active window title

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
AndyP
Posts: 615
Joined: Wed Aug 27, 2008 12:57 pm
Location: Seeheim, Germany (ex UK)
Contact:

How to get the active window title

Post by AndyP » 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

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.
Andy Piddock
https://livecode1001.blogspot.com Built with LiveCode
https://github.com/AndyPiddock/TinyIDE Mini IDE alternative
https://github.com/AndyPiddock/Seth Editor color theming
http://livecodeshare.runrev.com/stack/897/ LiveCode-Multi-Search

mtalluto
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 125
Joined: Tue Apr 11, 2006 7:02 pm
Location: Seattle, WA
Contact:

Re: How to get the active window title

Post by mtalluto » Tue Jan 23, 2024 10:15 pm

Very cool! Thanks for sharing.
Mark Talluto
--
Canela
design - develop - deploy: https://appli.io
Database and Cloud for LiveCode Developers: https://livecloud.io
Company: https://canelasoftware.com

AndyP
Posts: 615
Joined: Wed Aug 27, 2008 12:57 pm
Location: Seeheim, Germany (ex UK)
Contact:

Re: How to get the active window title

Post by AndyP » Wed Jan 24, 2024 10:34 am

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.
Andy Piddock
https://livecode1001.blogspot.com Built with LiveCode
https://github.com/AndyPiddock/TinyIDE Mini IDE alternative
https://github.com/AndyPiddock/Seth Editor color theming
http://livecodeshare.runrev.com/stack/897/ LiveCode-Multi-Search

mtalluto
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 125
Joined: Tue Apr 11, 2006 7:02 pm
Location: Seattle, WA
Contact:

Re: How to get the active window title

Post by mtalluto » Wed Jan 24, 2024 8:51 pm

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.
Mark Talluto
--
Canela
design - develop - deploy: https://appli.io
Database and Cloud for LiveCode Developers: https://livecloud.io
Company: https://canelasoftware.com

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”