【问题标题】:powershell script: open apps and position windowspowershell 脚本:打开应用程序并定位窗口
【发布时间】:2021-12-20 16:19:42
【问题描述】:

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

问题: 在工作之前,我需要做以下事情: (我有一个 3 屏幕/显示器设置)

  1. 打开 VSCode
  2. 在我的中间屏幕上最大化 VSCode
  3. 打开 4(四)个 powershell 实例
  4. 将 powershell 窗口停靠在我的左侧屏幕上并将它们停靠(左上角、右上角、bot-left、bot-right) - 我需要 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 文档中没有找到任何可以引用连接到我的 PC 的监视器的命令。

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

// $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()

我已经在每个工作日都这样做了一段时间,所以我正在尝试自动化整个过程。

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

谢谢大家的帮助!

【问题讨论】:

  • 我很好奇自己是否可以完全使用 powershell 来完成。但如果没有,powershell 确实可以让您访问整个 .NET 框架,因此您可以考虑尝试将同一事物的 C# 解决方案改编为 powershell 脚本。例如 - stackoverflow.com/questions/1538602/… 。尽管我怀疑这是否适合您,但您是否看过微软的“powertoys”实用程序?它有一个名为 Fancy Zones 的功能,可让您设计和保存自定义窗口布局/工作区
  • 嗨@diopside - Ihe fancyzones 应用程序不提供打开应用程序然后将应用程序窗口停靠到多个显示器上的不同位置的功能。还是我错过了什么?
  • 不,你没有错过任何东西,它基本上只是一个窗口安排器。只是想我会把它扔在那里,因为布置你的窗户似乎是你想做的事情的一小部分,可能微不足道
  • 我确实需要安排这些窗口,但我需要先自动打开这些应用程序:)

标签: powershell


【解决方案1】:

查找监控对象

要查找连接到您 PC 的显示器列表,请尝试此操作

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}

最初,坐标可能看起来不是特别有用,但考虑一下显示器是否排列不均匀 - 它们可能不是那么干净的数字。 例如,在监视器部分偏移的情况下拍摄此图像Images of monitors that don't line up easily

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

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

但是如果你有多个呢? One Laptop monitor on the bottom, with three monitors arranged on equal horizontally up above

#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

【讨论】:

  • 嘿@PsychoData,感谢您的回答,我还没有实现它。我会尽力在即将到来的周末完成它,然后我会接受它。
猜你喜欢
  • 1970-01-01
  • 2012-10-28
  • 1970-01-01
  • 1970-01-01
  • 2013-11-18
  • 1970-01-01
  • 1970-01-01
  • 2021-11-17
  • 1970-01-01
相关资源
最近更新 更多