Monday, March 27, 2023

Launch applications from Touch Portal (or any other launcher) and force them to the front

Datatalk / Touch Portal.

No matter how hard you try, sometimes Windows 11 doesn't let you push a window to the front. It can drive me nuts!

Why doesn't Windows have a, per application, setting to allow that specific application to be brought to the front by other apps and itself? It would save a lot of headaches.


Touch Portal

If you're using Touch Portal, WallX, or in general want to force a window to the front, there simply is no clear 'one size fits all' solution... You have to experiment.

In Touch Portal, you have several options:

1. Launch the program directly

2. Launch the program, then give it focus

3. Start a batch file

4. Start multiple programs from a batch file

5. A combination of the above


Option 4 never worked reliably for me, so I use either Touch Portal's build-in 'Focus' command, or used batch files.

Update: this is caused by Windows only allowing a focus change ONCE. When an app gives another app focus, it can't change it again. (See also 'Two Stages' at the bottom of this post.)


Start and Activate Window

This seems to actually activate and bring the window forward. Some apps work perfectly fine using this approach, for example opening an Excel spreadsheet:


- Run & Open / Open File will let you open any file with the associated application

- Run & Open / Start Application will launch the specified application

- Utility / Focus Application will bring the first window with the given title to the front and give it focus


Unfortunately, that doesn't always work 😒 so I had to resort using a batch file (more on later).


Notes:

1. Open File and Start Application will not wait until the application has been launched. Windows may return control to Touch Portal before the app is done, or the app may be slow to update its title bar. One way around this is to add a Delay in between opening the file and activating the window.

2. You can find a list of open Windows using cmdow /T from the command line.

3. Although it seems the titlebar contains a name, sometimes the application window can't be activated using that name. Often simply shortening the name works. For example, trying to activate the window using 'outline.xlsx - excel' didn't work, but using 'outline.xlsx' did.

4. Some apps simply don't cooperate. I have no clue why. Typically, launching them using a batch file did get it to work...


Using Batch Files

Up front, any scripts I tried worked best when using the 'Script & Open' option to start a batch file. I have no clue why.

Inside the called batch file you have different ways to launch a program. Again, there's no perfect way that works for everything. Here are five different approaches. You have to try which one works best on a trial and error basis.


Tools used in the batch files

  1. waitwindow - wait for a window
  2. waitfile - wait for a file
  3. cmdow- manipulate windows
  4. nircmd - NirSoft's multi tool, can be used to send keystrokes, and many other things
  5. tar - included with Windows 10 /11
  6. start - included with Windows
  7. timeout - included with Windows
  8. immprep - HTML processor to strip the exported HTML file


Launch an application from a batch file

1. Direct

You can simply provide a direct path plus an executable name, like:

  1. c:\software\totalcmd\totalcmd64.exe

... but it doesn't always pop up in front, so I need to make another call to cmdow.


2. Change the path first

Sometimes you have to start the executable from within a specific folder, so

  1. c:
  2. cd c:\software\totalcmd
  3. totalcmd64.exe

3. Use start

Some executables do not return until they're done. In that case you can use the start command like this:

  1. start c:\software\totalcmd\totalcmd64.exe

Of course you could provide parameters

  1. start c:\software\totalcmd\totalcmd64.exe %1 %2

Or exit the command line when done, after forcing the window to the front, using first waitwindow, then cmdow:
  1. start C:\software\wincmd\TOTALCMD64.EXE %1 %2
  2. waitwindow "total*" forever
  3. cmdow "total*" /vis /ena /res/ /act
  4. exit

For this I use a little command line tool I wrote called waitwindow.

Note that start does misbehave sometimes with parameters / parenthesis. Also note that there are many additional options for start. To list them all use

  1. c:\> start /?

Here's how I launch chrome using a specific profile:
  1. start "Chrome" /B "C:\Program Files\Google\Chrome\Application\chrome.exe" --profile-directory="Profile 1" %1

4. Use waitwindow and cmdow

Touchportal behaves differently. It would start most of the time, but the most reliable way was this approach (which doesn't make any sense, alas):

  1. waitwindow "notgonnawait" nowait startifnotopen "C:\Program Files (x86)\Touch Portal\TouchPortal.exe"
  2. waitwindow "touch portal*" forever wait
  3. cmdow "touch portal*" /ena /vis /act
  4. exit

5. When the build-in command fails

I wasn't able to reliably open the excel file in the example above, so I had to resort to launching a script (batch file) from Touch Portal:


In the associated batch file:

  1. start c:\googledrive\mynovel\outline.xlsx
  2. waitwindow "outline.xlsx*" 4
  3. cmdow "outline.xlsx*" /vis /ena /act
  4. exit


The combo above works reliably.


Trial and error

There is no single reliable idiot-proof way, you'll simply have to try what combination / approach works best for you.

Again, any scripts I tried worked best when using the 'Script & Open' option to start a batch file.


Examples

1. Launching Chrome with a specific profile

  1. start "Chrome" /B "C:\Program Files\Google\Chrome\Application\chrome.exe" --profile-directory="Default" "%1"


2. TotalRecorder

  1. waitwindow "*- total recorder" nowait start "C:\Program Files (x86)\HighCriteria\TotalRecorder\TotalRecorder.exe"
  2. waitwindow "*- total recorder"
  3. cmdow "*total recorder" /vis /ena /res /act
  4. exit


3. TotalCommander

  1. start C:\software\wincmd\TOTALCMD64.EXE %1 %2
  2. waitwindow "total*" forever wait
  3. cmdow "total*" /vis /ena /res/ /act
  4. exit


Two stages: Immersive Reader launched from within Google Docs

Once you've activated another window, you can't change the focus to another window. This is a deliberate limitation in Windows 8 / 10 / 11.

So, what I wanted to accomplish was: start MS Edge Immersive reader from within GoogleDocs. I used a multi stage stage approach for that.

1. You need an application that runs in the background and doesn't steal focus. TouchPortal does that.

2. I use a line '<< immersive >>' anywhere in my GoogleDocs document to tell immprep where the text to read starts.

4. Whilst working in GoogleDocs I hit a TouchPortal button, and it sends the keypresses to GoogleDocs to export the current document as a HTML file.

4. Next Touch Portal launches a batch file.

5. The batchfile then uses immprep (a little DIY tool) to process the archive exported by Google Docs (it strips everything up to a specific point, and converts '--' to 'emdash').

6. When the file has been processed the batch file launches Edge with the processed HTML file. At this point Edge will get the focus.

7. I then send from the batch file the 'start reading' command to Edge, using nircmd.


Here's the TouchPortal script:



And here's the batch file:
  1. immprep downloads auto cleanup mdash unread
  2. if errorlevel 1 goto noedge
  3. nircmd sendkeypress ctrl+shift+u
  4. :noedge
  5. exit

Update: the new version of Immprep uses different command line options. See here.

In the second half of this video you see how that works in practice.

Drop me a line in the comments section if you want a copy of ImmPrep.


No comments:

Post a Comment