MScholtes / PSVirtualDesktop

VirtualDesktop is a Powershell module that provides commandlets to manage virtual desktops of Windows 10.
MIT License
294 stars 18 forks source link

how do I get a "mainwindowhandle" for an electron app? #15

Closed seiyria closed 2 years ago

seiyria commented 2 years ago

I'm running multiple instances of VS Code, but I see this:

PS C:\WINDOWS\system32> Get-Process "code"

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    346      19    34004      82132       4.78   3284   1 Code
    182      13    19284      72712       0.23   3400   1 Code
    175      12    18808      72200       0.19   3688   1 Code
    387      24   568032     544560      11.17   3828   1 Code
    855      36    34464      83340       2.23   4352   1 Code
    303      12     9684      24924       0.00   7388   1 Code
    180      13    21512      71600       0.19   8040   1 Code
    170      13    58880     110720       2.05  12164   1 Code
    178      13    35220      87004       1.58  13584   1 Code
    291      20    12500      37916       0.06  14556   1 Code
    198      14   181940     233804      13.02  17004   1 Code
    466      15    32148      75732       0.38  18000   1 Code
    180      13    21464      72452       0.27  18044   1 Code
    190      13   200424     253308      13.61  18408   1 Code
    458      26   123692     156188       9.55  18456   1 Code
    357      30   502188     481352       9.33  19620   1 Code
    169      12    17696      69844       0.14  19960   1 Code
    671      36   190372     104872       4.72  20648   1 Code
    501      25   114704     158452       8.64  21320   1 Code
    184      13    34308      87296       9.41  21444   1 Code
    184      13    46372      93752      13.00  21480   1 Code
    171      12    17416      70808       0.16  23036   1 Code

Similarly, if I do this:

PS C:\WINDOWS\system32> (Get-Process "code").MainWindowHandle
0
0
0
0
1640298
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

Now, I've figured out that I can do something like this to load a main window handle from a window title name: (Get-Process | Where-Object { $_.MainWindowTitle -like '*code*' }).MainWindowHandle

However, if I run multiple VS Code instances, there's only ever one active at a time.

My goal is to script 5 VS Code instances across multiple virtual desktops (and ideally, chrome too, but I got stuck here), as well as pin a few applications.

Is there any way to do this?

MScholtes commented 2 years ago

Hello @seiyria,

this is a strange behaviour of Visual Studio Code. I managed to move all Visual Studio Code windows to desktop with number 1 with the following code:

Find-WindowHandle * | ? { $_.Title -match "Visual Studio Code" } | % { Move-Window -Desktop (Get-Desktop 1) -Hwnd $_.Handle }

I integrated the Find-WindowsHandle commandlet to PSVirtualDesktop in order to get Chrome tamed, but it might help here too.

Greetings

Markus

seiyria commented 2 years ago

Thank you! That looks so much better than what I tried to cobble together. Having this information I think I'll be able to write the script I was planning out.