powershell脚本:打开应用程序并定位窗口



免责声明:我是PowerShell的新手-我在YouTube上看了一些教程。

问题:工作前,我需要做以下事情:(我有一个3屏幕/监视器设置(

  1. 打开VSCode
  2. 在我的中间屏幕上最大化VSCode
  3. 打开powershell的4(四(个实例
  4. 将powershell窗口停靠在我的左侧屏幕上并将其停靠(左上角、右上角、机器人程序左上角和机器人程序右上角(-我需要4个实例,因为我有多个不同的进程同时运行
  5. 打开Chrome
  6. 在我的右侧屏幕上最大化Chrome

我想好了如何在某个路径上打开我需要的应用程序,但仅此而已:(

Start-Process -FilePath 'C:Users...Code.exe' -ArgumentList 'C:repos'
Start-Process -FilePath 'C:Windows...powershell.exe' -ArgumentList 'C:repos'

我在PowerShell文档中找不到任何可以引用连接到我电脑的监视器的命令。

长话短说-这就是我需要做的,用伪代码写:

// $screenMid = get the mid screen object (reference)
// $vscodeIinstance = open VSCode at a path
// $vscodeIinstance.position = $screenMid.position.maximize()

// $screenLeft = get the left screen object (reference)
// $powershellWindowOne = open PS at a path
// $powershellWindowOne.position = $screenLeft.position.dockTopLeft()
// $powershellWindowTwo = open PS at a path
// $powershellWindowTwo.position = $screenLeft.position.dockTopRight()
// ...do the same for another two PS windows (dockBotLeft, dockBotRight)

// $screenRight = get the right screen object (reference)
// $browser = open browser with url
// $browser position = $screenRight.position.maximize()

一段时间以来,我每天都在做这件事,所以我正在努力实现整个过程的自动化。

我已经有几天没有开始寻找解决方案了。到目前为止运气不好。

谢谢大家的帮助!

查找监视器对象

要查找连接到您电脑的显示器列表,请尝试此

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Screen]::AllScreens

以下是它如何在我的系统中返回的示例,您可以在其中看到工作区域是监视器覆盖的实际X和Y坐标。

Add-Type -AssemblyName System.Windows.Forms
PS C:> [System.Windows.Forms.Screen]::AllScreens

BitsPerPixel : 32
Bounds       : {X=0,Y=0,Width=1920,Height=1080}
DeviceName   : \.DISPLAY9
Primary      : True
WorkingArea  : {X=0,Y=0,Width=1920,Height=1032}
BitsPerPixel : 32
Bounds       : {X=1920,Y=0,Width=1920,Height=1080}
DeviceName   : \.DISPLAY1
Primary      : False
WorkingArea  : {X=1920,Y=0,Width=1920,Height=1032}

起初,坐标似乎不是特别有用,但考虑一下监视器的排列是否不均匀——它们可能不是那么干净的数字。例如,用部分偏移的监视器拍摄这张图像。不容易排列的监视器的图像

它返回的简单/干净的数字比第一个例子要少得多

PS C:>
PS C:> [System.Windows.Forms.Screen]::AllScreens

BitsPerPixel : 32
Bounds       : {X=0,Y=0,Width=1920,Height=1080}
DeviceName   : \.DISPLAY9
Primary      : True
WorkingArea  : {X=0,Y=0,Width=1920,Height=1032}
BitsPerPixel : 32
Bounds       : {X=1920,Y=293,Width=1920,Height=1080}
DeviceName   : \.DISPLAY1
Primary      : False
WorkingArea  : {X=1920,Y=293,Width=1920,Height=1032}

注意:如果在PowerShell会话启动后更改监视器布局,则会话不会拾取它-这可能是关于System.Windows.Forms的实例被重用,并且仅在构建时生成,而不是按照您的要求动态生成。只需启动一个新的PowerShell会话即可对其进行检查。

哪台显示器在哪里

我们如何找到最左边、最右边等显示器?您可以使用WorkingArea来判断哪个监视器是最左边的

[System.Windows.Forms.Screen]::AllScreens | sort -Property {$_.WorkingArea.X} | select -First 1

对吗?使用-Descending翻转排序

[System.Windows.Forms.Screen]::AllScreens | sort -Property {$_.WorkingArea.X} -Descending | select -First 1

好吧,那么你有三台显示器,想要中间的吗?跳过第一个,取1获得第二个

[System.Windows.Forms.Screen]::AllScreens | sort -Property {$_.WorkingArea.X} -Descending | select -skip 1 -first 1

好的,好的,但是如果我有垂直显示器呢?如果你只有一个顶部或底部的,你可能会得到类似的东西,但只有$_.WorkingArea.Y,但如果没有,你可能需要先过滤。

#Simple Filter for top monitor
```powershell
[System.Windows.Forms.Screen]::AllScreens | sort -Property {$_.WorkingArea.Y} -Descending | select -first 1

但如果你有多个呢?底部有一台笔记本电脑显示器,三台显示器在上方水平排列

#Filter for top monitors, then select middle monitor
```powershell
#Get all of the Top Edge heights and finding the highest Top Edge 
$UpperBounds = [System.Windows.Forms.Screen]::AllScreens.WorkingArea.Top | select -Unique
#Find all monitors whose top-edge is on the highest physically (0,0 is the top,left) 
$HighestBound = $UpperBounds | Sort | select -first 1
$highLevelMonitors = [System.Windows.Forms.Screen]::AllScreens | where {$_.WorkingArea.Top -eq $HighestBound} 
#Find the middle monitor of the highest monitors, like above
$highLevelMonitors | sort -Property {$_.WorkingArea.X} | select -Skip 1 -first 1

最新更新