Foreground browser window

0

I'm trying to write a script that can do a few basic keyboard and mouse events, and then 'open' the current Chrome window, and continue from there.

The only problem is that I cannot find a way to bring the Chrome window to the foreground. The code works with simple things, such as Notepad, but it simply cannot find Chrome, no matter how many variations of the name I've tried putting in.

I've looked around the web and couldn't find a solution to this problem.

My current code:

Add-Type @"
using System;
using System.Runtime.InteropServices;
public class Tricks {
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool SetForegroundWindow(IntPtr hWnd);
}
"@
sleep -sec 2
$h = (Get-Process Chrome).MainWindowHandle
[void] [Tricks]::SetForegroundWindow($h)

If I use Get-Process Chrome this error is thrown:

Cannot convert argument "hWnd", with value: "System.Object[]", for
"SetForegroundWindow" to type "System.IntPtr": "Cannot convert the
"System.Object[]" value of type "System.Object[]" to type "System.IntPtr"."
At line:12 char:1
+ [void] [Tricks]::SetForegroundWindow($h)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

If I use any thing else, this error is thrown:

Get-Process : Cannot find a process with the name "Google Chrome". Verify the
process name and call the cmdlet again.
At line:11 char:7
+ $h = (Get-Process "Google Chrome").MainWindowHandle
+       ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Google Chrome:String) [Get-Process], ProcessCommandException
    + FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand

along with the previous error.

However, if I use something like "Notepad", no error is thrown and the program works as expected.

EDIT:

I've even managed to find a shortened version of the script:

(New-Object -ComObject WScript.Shell).AppActivate((get-process Notepad).MainWindowTitle)

However, as before, this works with something like Notepad, but the moment I give try to use "Chrome" I get this:

Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))
At line:1 char:1
+ (New-Object -ComObject WScript.Shell).AppActivate((get-process Chrome ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

The same goes for Firefox.

powershell
asked on Stack Overflow Jun 21, 2019 by Charon • edited Jun 21, 2019 by Ansgar Wiechers

1 Answer

1

I haven't got Chrome but with Firefox (you said it was the same)

PS C:\Users\peter> Get-Process firefox

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    503      46   101704     131672      48.55  11500   1 firefox
    906      61    36184      65604       0.72  25312   1 firefox
    907      74    74264     122448       9.81  35828   1 firefox
   1730     116   172892     241800      36.11  36044   1 firefox
    697      59    73552     126796      15.77  39864   1 firefox
    520      33    20632      45052       0.25  41996   1 firefox


PS C:\Users\peter> Get-Process notepad

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    230      13     2812      14044       0.09  50796   1 notepad

i.e there are multiple Firefox processes.

Unfortunately it's not clear to me how to pick the 'right one'. Try this:

 Get-Process firefox | %{ (New-Object -ComObject WScript.Shell).AppActivate($_.MainWindowTitle)}

which just tries to bring all processes to the front. It worked for me but is not efficient - I'm sure you could improve.

answered on Stack Overflow Jun 21, 2019 by Peter Hull

User contributions licensed under CC BY-SA 3.0