Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ===================================================
- # AutoDisplayChanger.ps1, by Songbirb
- # ---------------------------------------------------
- # I run in the background and check the application ($appLaunch) status every $interval seconds.
- # When the application is detected, I save the current resolution and set it to $newWidth x $newHeight.
- # Within $interval seconds of $appLaunch closing, I restore the original resolution and exit.
- # ===================================================
- param(
- # The name of the application to monitor
- [string]$appLaunch = "Palworld-Win64-Shipping",
- # The interval for checking the application status
- [int]$interval = 5,
- # The desired resolution for the application
- [int]$newWidth = 1920,
- [int]$newHeight = 1080,
- # The normal desktop resolution
- [int]$oldWidth,
- [int]$oldHeight,
- # The switch for launching the application without Steam
- [switch]$steamless
- )
- # ===================================================
- # SCRIPT CONTENTS - DO NOT EDIT BELOW THIS LINE
- # ---------------------------------------------------
- # I require the DisplaySettings module from Powershell-Gallery
- Import-Module DisplaySettings
- # This is a flag.
- $changed = $false
- Clear-Host
- # If oldWidth and oldHeight are not specified, get them from the video controller
- if (!$oldWidth -or !$oldHeight) {
- Write-Host -NoNewline "Detected: "
- $videoController = Get-CimInstance -ClassName Win32_VideoController
- Write-Host $videoController.Name
- Write-Host -NoNewline "Current Mode? "
- # Most methods don't return the physical resolution, but gdi32.dll does!
- Add-Type @"
- using System;
- using System.Runtime.InteropServices;
- public class PInvoke {
- [DllImport("user32.dll")] public static extern IntPtr GetDC(IntPtr hwnd);
- [DllImport("gdi32.dll")] public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
- }
- "@
- $hdc = [PInvoke]::GetDC([IntPtr]::Zero)
- $oldWidth = [PInvoke]::GetDeviceCaps($hdc, 118) # width from gdi32
- $oldHeight = [PInvoke]::GetDeviceCaps($hdc, 117) # height from gdi32
- Write-Host -NoNewline "$oldWidth x "
- Write-Host -NoNewline "$oldHeight @"
- $oldRefresh = $videoController.CurrentRefreshRate
- Write-Host -NoNewline "$oldRefresh"
- Write-Host "Hz."
- Write-Host "If this is incorrect, specify -oldHeight and -oldWidth!"
- Write-Host
- Start-Sleep -Seconds (1)
- }
- # Launch the application and wait an interval for it to launch.
- # Check the value of the -steamless switch
- if ($steamless) {
- # Launch the application without Steam and let the user know
- Write-Host "Starting $appLaunch`.exe..."
- Write-Host " (Path: $PSScriptRoot)"
- Start-Process (Resolve-Path "$PSScriptRoot\$appLaunch.exe")
- }
- else {
- # Wait for Steam to launch the application
- Write-Host Waiting for Steam to start $appLaunch`.exe!
- }
- Start-Sleep -Seconds (1+$interval)
- # Define a loop to check the status every $interval seconds
- while ($true) {
- # Gets the process object for $appLaunch and stores it
- $process = Get-Process -Name $appLaunch -ErrorAction SilentlyContinue
- # Process exists? App running!
- if ($process) {
- # If the flag hasn't been set, we need to change the resolution.
- if (!$changed) {
- Write-Host "Detected $appLaunch`.exe running!"
- Write-Host "Setting desired video mode ($newWidth x $newHeight)..."
- Set-DisplayResolution -Width $newWidth -Height $newHeight
- Write-Host "Screen should be set to $newWidth, $newHeight now!"
- # Set the flag to true so we can cleanup later.
- $changed = $true
- }
- }
- # No process? App is closed.
- else {
- # If the flag is set, we have to clean up:
- if ($changed) {
- Write-Host "Setting original video mode ($oldWidth x $oldHeight)."
- Set-DisplayResolution -Width $oldWidth -Height $oldHeight
- $changed = $false
- Write-Host "All tasks complete - closing!"
- Start-Sleep -Seconds (1+$interval)
- # stop-process -Id $PID
- }
- }
- # wait a little while before testing again
- Start-Sleep -Seconds $interval
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement