Advertisement
Songbirb-NewLeaf

AutoDisplayChanger.ps1

Jan 29th, 2024 (edited)
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # ===================================================
  2. #       AutoDisplayChanger.ps1, by Songbirb
  3. # ---------------------------------------------------
  4. # I run in the background and check the application ($appLaunch) status every $interval seconds.
  5. # When the application is detected, I save the current resolution and set it to $newWidth x $newHeight.
  6. # Within $interval seconds of $appLaunch closing, I restore the original resolution and exit.
  7. # ===================================================
  8. param(
  9.     # The name of the application to monitor
  10.     [string]$appLaunch = "Palworld-Win64-Shipping",
  11.     # The interval for checking the application status
  12.     [int]$interval = 5,
  13.     # The desired resolution for the application
  14.     [int]$newWidth = 1920,
  15.     [int]$newHeight = 1080,
  16.     # The normal desktop resolution
  17.     [int]$oldWidth,
  18.     [int]$oldHeight,
  19.     # The switch for launching the application without Steam
  20.     [switch]$steamless
  21. )
  22.  
  23.  
  24. # ===================================================
  25. #    SCRIPT CONTENTS - DO NOT EDIT BELOW THIS LINE
  26. # ---------------------------------------------------
  27. # I require the DisplaySettings module from Powershell-Gallery
  28. Import-Module DisplaySettings
  29. # This is a flag.
  30. $changed = $false
  31. Clear-Host
  32.  
  33. # If oldWidth and oldHeight are not specified, get them from the video controller
  34. if (!$oldWidth -or !$oldHeight) {
  35.     Write-Host -NoNewline "Detected: "
  36.     $videoController = Get-CimInstance -ClassName Win32_VideoController
  37.     Write-Host $videoController.Name
  38.     Write-Host -NoNewline "Current Mode? "
  39. # Most methods don't return the physical resolution, but gdi32.dll does!
  40. Add-Type @"
  41. using System;
  42. using System.Runtime.InteropServices;
  43. public class PInvoke {
  44.    [DllImport("user32.dll")] public static extern IntPtr GetDC(IntPtr hwnd);
  45.    [DllImport("gdi32.dll")] public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
  46. }
  47. "@
  48.     $hdc = [PInvoke]::GetDC([IntPtr]::Zero)
  49.     $oldWidth = [PInvoke]::GetDeviceCaps($hdc, 118) # width from gdi32
  50.     $oldHeight = [PInvoke]::GetDeviceCaps($hdc, 117) # height from gdi32
  51.     Write-Host -NoNewline "$oldWidth x "
  52.     Write-Host -NoNewline "$oldHeight @"
  53.     $oldRefresh = $videoController.CurrentRefreshRate
  54.     Write-Host -NoNewline "$oldRefresh"
  55.     Write-Host "Hz."
  56.     Write-Host "If this is incorrect, specify -oldHeight and -oldWidth!"
  57.     Write-Host
  58.     Start-Sleep -Seconds (1)
  59. }
  60.  
  61.  
  62. # Launch the application and wait an interval for it to launch.
  63. # Check the value of the -steamless switch
  64. if ($steamless) {
  65.     # Launch the application without Steam and let the user know
  66.     Write-Host "Starting $appLaunch`.exe..."
  67.     Write-Host "    (Path: $PSScriptRoot)"
  68.     Start-Process (Resolve-Path "$PSScriptRoot\$appLaunch.exe")
  69. }
  70. else {
  71.     # Wait for Steam to launch the application
  72.     Write-Host Waiting for Steam to start $appLaunch`.exe!
  73. }
  74. Start-Sleep -Seconds (1+$interval)
  75.  
  76.  
  77. # Define a loop to check the status every $interval seconds
  78. while ($true) {
  79.     # Gets the process object for $appLaunch and stores it
  80.     $process = Get-Process -Name $appLaunch -ErrorAction SilentlyContinue
  81.     # Process exists? App running!
  82.     if ($process) {
  83.         # If the flag hasn't been set, we need to change the resolution.
  84.         if (!$changed) {
  85.             Write-Host "Detected $appLaunch`.exe running!"
  86.             Write-Host "Setting desired video mode ($newWidth x $newHeight)..."
  87.             Set-DisplayResolution -Width $newWidth -Height $newHeight
  88.             Write-Host "Screen should be set to $newWidth, $newHeight now!"
  89.             # Set the flag to true so we can cleanup later.
  90.             $changed = $true
  91.         }
  92.     }
  93.     # No process? App is closed.
  94.     else {
  95.         # If the flag is set, we have to clean up:
  96.         if ($changed) {
  97.             Write-Host "Setting original video mode ($oldWidth x $oldHeight)."
  98.             Set-DisplayResolution -Width $oldWidth -Height $oldHeight
  99.             $changed = $false
  100.             Write-Host "All tasks complete - closing!"
  101.             Start-Sleep -Seconds (1+$interval)            
  102.         #   stop-process -Id $PID
  103.         }
  104.     }
  105.     # wait a little while before testing again
  106.     Start-Sleep -Seconds $interval
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement